Java Code Examples for org.apache.commons.jexl3.JexlException#Break

The following examples show how to use org.apache.commons.jexl3.JexlException#Break . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Interpreter.java    From commons-jexl with Apache License 2.0 6 votes vote down vote up
@Override
protected Object visit(ASTWhileStatement node, Object data) {
    Object result = null;
    /* first objectNode is the condition */
    Node condition = node.jjtGetChild(0);
    while (arithmetic.toBoolean(condition.jjtAccept(this, data))) {
        cancelCheck(node);
        if (node.jjtGetNumChildren() > 1) {
            try {
                // execute statement
                result = node.jjtGetChild(1).jjtAccept(this, data);
            } catch (JexlException.Break stmtBreak) {
                break;
            } catch (JexlException.Continue stmtContinue) {
                //continue;
            }
        }
    }
    return result;
}
 
Example 2
Source File: Interpreter.java    From commons-jexl with Apache License 2.0 6 votes vote down vote up
@Override
protected Object visit(ASTDoWhileStatement node, Object data) {
    Object result = null;
    int nc = node.jjtGetNumChildren();
    /* last objectNode is the condition */
    Node condition = node.jjtGetChild(nc - 1);
    do {
        cancelCheck(node);
        if (nc > 1) {
            try {
                // execute statement
                result = node.jjtGetChild(0).jjtAccept(this, data);
            } catch (JexlException.Break stmtBreak) {
                break;
            } catch (JexlException.Continue stmtContinue) {
                //continue;
            }
        }
    } while (arithmetic.toBoolean(condition.jjtAccept(this, data)));
    return result;
}
 
Example 3
Source File: Interpreter.java    From commons-jexl with Apache License 2.0 4 votes vote down vote up
@Override
protected Object visit(ASTBreak node, Object data) {
    throw new JexlException.Break(node);
}
 
Example 4
Source File: Interpreter.java    From commons-jexl with Apache License 2.0 4 votes vote down vote up
@Override
protected Object visit(ASTForeachStatement node, Object data) {
    Object result = null;
    /* first objectNode is the loop variable */
    ASTReference loopReference = (ASTReference) node.jjtGetChild(0);
    ASTIdentifier loopVariable = (ASTIdentifier) loopReference.jjtGetChild(0);
    final int symbol = loopVariable.getSymbol();
    final boolean lexical = options.isLexical();// && node.getSymbolCount() > 0;
    final LexicalFrame locals = lexical? new LexicalFrame(frame, block) : null;
    final boolean loopSymbol = symbol >= 0 && loopVariable instanceof ASTVar;
    if (lexical) {
        // create lexical frame
        // it may be a local previously declared
        if (loopSymbol && !defineVariable((ASTVar) loopVariable, locals)) {
            return redefinedVariable(node, loopVariable.getName());
        }
        block = locals;
    }
    Object forEach = null;
    try {
        /* second objectNode is the variable to iterate */
        Object iterableValue = node.jjtGetChild(1).jjtAccept(this, data);
        // make sure there is a value to iterate upon
        if (iterableValue != null) {
            /* third objectNode is the statement to execute */
            JexlNode statement = node.jjtGetNumChildren() >= 3 ? node.jjtGetChild(2) : null;
            // get an iterator for the collection/array etc via the introspector.
            forEach = operators.tryOverload(node, JexlOperator.FOR_EACH, iterableValue);
            Iterator<?> itemsIterator = forEach instanceof Iterator
                    ? (Iterator<?>) forEach
                    : uberspect.getIterator(iterableValue);
            if (itemsIterator != null) {
                int cnt = 0;
                while (itemsIterator.hasNext()) {
                    cancelCheck(node);
                    // reset loop varaible
                    if (lexical && cnt++ > 0) {
                        // clean up but remain current
                        block.pop();
                        // unlikely to fail 
                        if (loopSymbol && !defineVariable((ASTVar) loopVariable, locals)) {
                            return redefinedVariable(node, loopVariable.getName());
                        }
                    }
                    // set loopVariable to value of iterator
                    Object value = itemsIterator.next();
                    if (symbol < 0) {
                        setContextVariable(node, loopVariable.getName(), value);
                    } else {
                        frame.set(symbol, value);
                    }
                    if (statement != null) {
                        try {
                            // execute statement
                            result = statement.jjtAccept(this, data);
                        } catch (JexlException.Break stmtBreak) {
                            break;
                        } catch (JexlException.Continue stmtContinue) {
                            //continue;
                        }
                    }
                }
            }
        }
    } finally {
        //  closeable iterator handling
        closeIfSupported(forEach);
        // restore lexical frame
        if (lexical) {
            block = block.pop();
        }
    }
    return result;
}