Search in sources :

Example 1 with CollectionParser

use of org.mvel2.util.CollectionParser in project mvel by mikebrock.

the class InlineCollectionNode method parseGraph.

private void parseGraph(boolean compile, Class type, ParserContext pCtx) {
    CollectionParser parser = new CollectionParser();
    if (type == null) {
        collectionGraph = ((List) parser.parseCollection(expr, start, offset, compile, pCtx)).get(0);
    } else {
        collectionGraph = ((List) parser.parseCollection(expr, start, offset, compile, type, pCtx)).get(0);
    }
    trailingStart = parser.getCursor() + 2;
    trailingOffset = offset - (trailingStart - start);
    if (this.egressType == null)
        this.egressType = collectionGraph.getClass();
}
Also used : CollectionParser(org.mvel2.util.CollectionParser)

Example 2 with CollectionParser

use of org.mvel2.util.CollectionParser in project mvel by mikebrock.

the class CollectionParser method parseCollection.

private Object parseCollection(boolean subcompile) {
    if (end - start == 0) {
        if (type == LIST)
            return new ArrayList();
        else
            return EMPTY_ARRAY;
    }
    Map<Object, Object> map = null;
    List<Object> list = null;
    int st = start;
    if (type != -1) {
        switch(type) {
            case ARRAY:
            case LIST:
                list = new ArrayList<Object>();
                break;
            case MAP:
                map = new HashMap<Object, Object>();
                break;
        }
    }
    Object curr = null;
    int newType = -1;
    for (; cursor < end; cursor++) {
        switch(property[cursor]) {
            case '{':
                if (newType == -1) {
                    newType = ARRAY;
                }
            case '[':
                if (cursor > start && isIdentifierPart(property[cursor - 1]))
                    continue;
                if (newType == -1) {
                    newType = LIST;
                }
                /**
           * Sub-parse nested collections.
           */
                Object o = new CollectionParser(newType).parseCollection(property, (st = cursor) + 1, (cursor = balancedCapture(property, st, end, property[st])) - st - 1, subcompile, colType, pCtx);
                if (type == MAP) {
                    map.put(curr, o);
                } else {
                    list.add(curr = o);
                }
                cursor = skipWhitespace(property, ++cursor);
                if ((st = cursor) < end && property[cursor] == ',') {
                    st = cursor + 1;
                } else if (cursor < end) {
                    if (ParseTools.opLookup(property[cursor]) == -1) {
                        throw new CompileException("unterminated collection element", property, cursor);
                    }
                }
                continue;
            case '(':
                cursor = balancedCapture(property, cursor, end, '(');
                break;
            case '\"':
            case '\'':
                cursor = balancedCapture(property, cursor, end, property[cursor]);
                break;
            case ',':
                if (type != MAP) {
                    list.add(new String(property, st, cursor - st).trim());
                } else {
                    map.put(curr, createStringTrimmed(property, st, cursor - st));
                }
                if (subcompile) {
                    subCompile(st, cursor - st);
                }
                st = cursor + 1;
                break;
            case ':':
                if (type != MAP) {
                    map = new HashMap<Object, Object>();
                    type = MAP;
                }
                curr = createStringTrimmed(property, st, cursor - st);
                if (subcompile) {
                    subCompile(st, cursor - st);
                }
                st = cursor + 1;
                break;
            case '.':
                cursor++;
                cursor = skipWhitespace(property, cursor);
                if (cursor != end && property[cursor] == '{') {
                    cursor = balancedCapture(property, cursor, '{');
                }
                break;
        }
    }
    if (st < end && isWhitespace(property[st])) {
        st = skipWhitespace(property, st);
    }
    if (st < end) {
        if (cursor < (end - 1))
            cursor++;
        if (type == MAP) {
            map.put(curr, createStringTrimmed(property, st, cursor - st));
        } else {
            if (cursor < end)
                cursor++;
            list.add(createStringTrimmed(property, st, cursor - st));
        }
        if (subcompile)
            subCompile(st, cursor - st);
    }
    switch(type) {
        case MAP:
            return map;
        case ARRAY:
            return list.toArray();
        default:
            return list;
    }
}
Also used : ArrayList(java.util.ArrayList) CompileException(org.mvel2.CompileException)

Aggregations

ArrayList (java.util.ArrayList)1 CompileException (org.mvel2.CompileException)1 CollectionParser (org.mvel2.util.CollectionParser)1