org.mvel2.util.CollectionParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of virtdata-lib-realer Show documentation
Show all versions of virtdata-lib-realer Show documentation
With inspiration from other libraries
/**
* MVEL 2.0
* Copyright (C) 2007 The Codehaus
* Mike Brock, Dhanji Prasanna, John Graham, Mark Proctor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mvel2.util;
import org.mvel2.CompileException;
import org.mvel2.DataConversion;
import org.mvel2.ParserContext;
import org.mvel2.compiler.ExecutableStatement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.mvel2.util.ParseTools.*;
import static org.mvel2.util.ReflectionUtil.isAssignableFrom;
/**
* This is the inline collection sub-parser. It produces a skeleton model of the collection which is in turn translated
* into a sequenced AST to produce the collection efficiently at runtime, and passed off to one of the JIT's if
* configured.
*
* @author Christopher Brock
*/
public class CollectionParser {
private char[] property;
private int cursor;
private int start;
private int end;
private int type;
public static final int LIST = 0;
public static final int ARRAY = 1;
public static final int MAP = 2;
private Class colType;
private ParserContext pCtx;
private static final Object[] EMPTY_ARRAY = new Object[0];
public CollectionParser() {
}
public CollectionParser(int type) {
this.type = type;
}
public Object parseCollection(char[] property, int start, int offset, boolean subcompile, ParserContext pCtx) {
this.property = property;
this.pCtx = pCtx;
this.end = start + offset;
while (start < end && isWhitespace(property[start])) {
start++;
}
this.start = this.cursor = start;
return parseCollection(subcompile);
}
public Object parseCollection(char[] property, int start, int offset, boolean subcompile, Class colType, ParserContext pCtx) {
if (colType != null) this.colType = getBaseComponentType(colType);
this.property = property;
this.end = start + offset;
while (start < end && isWhitespace(property[start])) {
start++;
}
this.start = this.cursor = start;
this.pCtx = pCtx;
return parseCollection(subcompile);
}
private Object parseCollection(boolean subcompile) {
if (end - start == 0) {
if (type == LIST) return new ArrayList();
else return EMPTY_ARRAY;
}
Map