use of org.apache.commons.jexl2.parser.ASTIdentifier in project datawave by NationalSecurityAgency.
the class JexlNodeFactory method buildNullNode.
public static JexlNode buildNullNode(JexlNode original, ASTIdentifier identifier) {
JexlNode copy;
if (original instanceof ASTEQNode) {
copy = new ASTEQNode(ParserTreeConstants.JJTEQNODE);
} else if (original instanceof ASTNENode) {
copy = new ASTNENode(ParserTreeConstants.JJTNENODE);
} else if (original instanceof ASTERNode) {
copy = new ASTERNode(ParserTreeConstants.JJTERNODE);
} else if (original instanceof ASTNRNode) {
copy = new ASTNRNode(ParserTreeConstants.JJTNRNODE);
} else if (original instanceof ASTGTNode) {
copy = new ASTGTNode(ParserTreeConstants.JJTGTNODE);
} else if (original instanceof ASTGENode) {
copy = new ASTGENode(ParserTreeConstants.JJTGENODE);
} else if (original instanceof ASTLTNode) {
copy = new ASTLTNode(ParserTreeConstants.JJTLTNODE);
} else if (original instanceof ASTLENode) {
copy = new ASTLENode(ParserTreeConstants.JJTLENODE);
} else {
throw new UnsupportedOperationException("Cannot handle " + original);
}
copy.jjtSetParent(original.jjtGetParent());
ASTNullLiteral literalNode = new ASTNullLiteral(ParserTreeConstants.JJTNULLLITERAL);
return buildUntypedNewLiteralNode(copy, identifier, literalNode);
}
use of org.apache.commons.jexl2.parser.ASTIdentifier in project datawave by NationalSecurityAgency.
the class JexlNodeFactory method buildNode.
/**
* Create a new JexlNode from the given node (possible an OR Node) and value
*
* @param original
* @param node
* @param fieldValue
* @return
*/
public static JexlNode buildNode(JexlNode original, JexlNode node, Number fieldValue) {
JexlNode newNode = JexlNodeFactory.shallowCopy(original);
if (null != original) {
newNode.jjtSetParent(original.jjtGetParent());
}
List<JexlNode> list = Lists.newArrayList();
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
JexlNode kid = node.jjtGetChild(i);
list.add(buildUntypedNewNode(newNode, (ASTIdentifier) JexlNodeFactory.shallowCopy(kid), fieldValue));
}
if (!list.isEmpty()) {
return createOrNode(list);
}
return null;
}
use of org.apache.commons.jexl2.parser.ASTIdentifier in project datawave by NationalSecurityAgency.
the class JexlNodeFactory method buildUntypedNewLiteralNode.
/**
* Like {@link #buildUntypedNewNode(JexlNode, ASTIdentifier, String)} except it does not wrap {@code literal} in an {@link ASTReference}
*
* @param newNode
* @param identifier
* @param literal
* @return
*/
protected static JexlNode buildUntypedNewLiteralNode(JexlNode newNode, ASTIdentifier identifier, JexlNode literal) {
ASTReference identifierReference = new ASTReference(ParserTreeConstants.JJTREFERENCE);
identifierReference.jjtAddChild(identifier, 0);
identifier.jjtSetParent(identifierReference);
newNode.jjtAddChild(identifierReference, 0);
newNode.jjtAddChild(literal, 1);
identifierReference.jjtSetParent(newNode);
literal.jjtSetParent(newNode);
return newNode;
}
use of org.apache.commons.jexl2.parser.ASTIdentifier in project datawave by NationalSecurityAgency.
the class FunctionJexlNodeVisitor method makeFunctionFrom.
public static ASTFunctionNode makeFunctionFrom(String ns, String functionName, JexlNode... arguments) {
ASTFunctionNode fn = new ASTFunctionNode(ParserTreeConstants.JJTFUNCTIONNODE);
ASTIdentifier namespace = JexlNodes.makeIdentifierWithImage(ns);
ASTIdentifier function = JexlNodes.makeIdentifierWithImage(functionName);
ArrayList<JexlNode> nodes = Lists.newArrayList();
nodes.add(namespace);
nodes.add(function);
Collections.addAll(nodes, arguments);
return JexlNodes.children(fn, nodes.toArray(new JexlNode[nodes.size()]));
}
use of org.apache.commons.jexl2.parser.ASTIdentifier in project commons-jexl by apache.
the class Interpreter method visit.
@Override
protected Object visit(final ASTForeachStatement node, final Object data) {
Object result = null;
/* first objectNode is the loop variable */
final ASTReference loopReference = (ASTReference) node.jjtGetChild(0);
final ASTIdentifier loopVariable = (ASTIdentifier) loopReference.jjtGetChild(0);
final int symbol = loopVariable.getSymbol();
// && node.getSymbolCount() > 0;
final boolean lexical = options.isLexical();
final LexicalFrame locals = lexical ? new LexicalFrame(frame, block) : null;
final boolean loopSymbol = symbol >= 0 && loopVariable instanceof ASTVar;
if (lexical) {
// 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 */
final 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 */
final 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);
final Iterator<?> itemsIterator = forEach instanceof Iterator ? (Iterator<?>) forEach : uberspect.getIterator(iterableValue);
if (itemsIterator != null) {
int cnt = 0;
while (itemsIterator.hasNext()) {
cancelCheck(node);
// reset loop variable
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
final 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 (final JexlException.Break stmtBreak) {
break;
} catch (final JexlException.Continue stmtContinue) {
// continue;
}
}
}
}
}
} finally {
// closeable iterator handling
closeIfSupported(forEach);
// restore lexical frame
if (lexical) {
block = block.pop();
}
}
return result;
}
Aggregations