use of org.apache.commons.jexl3.parser.ASTReference in project datawave by NationalSecurityAgency.
the class QueryModelVisitorTest method testModelApplication.
@Test
public void testModelApplication() throws ParseException {
model.addTermToModel("FOO1", "1BAR");
String original = "FOO1 == 'baz'";
ASTJexlScript groomed = JexlASTHelper.InvertNodeVisitor.invertSwappedNodes(JexlASTHelper.parseJexlQuery(original));
String expected = "$1BAR == 'baz'";
ASTJexlScript actualScript = assertResult(JexlStringBuildingVisitor.buildQuery(groomed), expected);
List<ASTEQNode> actualNodes = JexlASTHelper.getEQNodes(actualScript);
for (ASTEQNode node : actualNodes) {
assertTrue(node.jjtGetChild(0) instanceof ASTReference);
assertTrue(node.jjtGetChild(1) instanceof ASTReference);
}
}
use of org.apache.commons.jexl3.parser.ASTReference in project datawave by NationalSecurityAgency.
the class ExecutableDeterminationVisitorTest method wrap.
/**
* Copied from JexlNodeFactory without the AND/OR restrictions
*
* @param toWrap
* @return
*/
private JexlNode wrap(JexlNode toWrap) {
ASTReference reference = new ASTReference(ParserTreeConstants.JJTREFERENCE);
ASTReferenceExpression parens = new ASTReferenceExpression(ParserTreeConstants.JJTREFERENCEEXPRESSION);
parens.jjtAddChild(toWrap, 0);
toWrap.jjtSetParent(parens);
reference.jjtAddChild(parens, 0);
parens.jjtSetParent(reference);
return reference;
}
use of org.apache.commons.jexl3.parser.ASTReference in project datawave by NationalSecurityAgency.
the class QueryJexl method normalizeScript.
/**
* Normalizes all of the {@link ASTIdentifier}, {@link ASTStringLiteral}, and {@link ASTNumberLiteral} entries that exist in a {@link ASTJexlScript}.
*
* @param node
* current node for normalization
* @param nodes
* queue of nodes used as a stack for normalization
*/
private void normalizeScript(final SimpleNode node, final Deque<SimpleNode> nodes) {
int num = node.jjtGetNumChildren();
for (int n = 0; n < num; n++) {
SimpleNode child = node.jjtGetChild(n);
if (0 < child.jjtGetNumChildren()) {
if (!(child instanceof ASTReference || child instanceof ASTReferenceExpression)) {
// this may be an op node (e.g. ASTEQNode) or some other node
nodes.addFirst(child);
}
// recursive processing of child nodes
normalizeScript(child, nodes);
} else {
// the order for the identifier and literal nodes may vary
if (child instanceof ASTIdentifier) {
ASTIdentifier id = (ASTIdentifier) child;
// change identifier to lower case
id.image = id.image.toLowerCase();
// check for string or numeric literal on node stack
SimpleNode entry = nodes.removeFirst();
Assert.assertNotNull(entry);
if (entry instanceof ASTStringLiteral || entry instanceof ASTNumberLiteral) {
// exp is "value OP field"
// remove op node
SimpleNode opNode = nodes.removeFirst();
normalizeField(id.image, (JexlNode) entry, opNode);
} else {
// push entry back on stack and add identifier
nodes.addFirst(entry);
nodes.addFirst(child);
}
} else if (child instanceof ASTStringLiteral || child instanceof ASTNumberLiteral) {
// check for identifier on node stack
SimpleNode entry = nodes.removeFirst();
Assert.assertNotNull(entry);
if (entry instanceof ASTIdentifier) {
// exp is "field OP value"
SimpleNode opNode = nodes.removeFirst();
normalizeField(((JexlNode) entry).image, (JexlNode) child, opNode);
} else {
// push entry back on stack and add literal
nodes.addFirst(entry);
nodes.addFirst(child);
}
}
}
}
}
use of org.apache.commons.jexl3.parser.ASTReference in project datawave by NationalSecurityAgency.
the class JexlASTHelper method parseQueryWithBackslashes.
// we need to replace double backslashes in the query with a placeholder value
// before parsing in order to prevent the parser from interpreting doubles as singles
private static ASTJexlScript parseQueryWithBackslashes(String query, Parser parser) throws Exception {
// determine how many doubles need to be replaced
int numFound = StringUtils.countMatches(query, DOUBLE_BACKSLASH);
// replace the doubles with a unique placeholder
String placeholder = generatePlaceholder(query);
query = query.replace(DOUBLE_BACKSLASH, placeholder);
// Parse the query with the placeholders
ASTJexlScript jexlScript;
try {
jexlScript = parser.parse(new StringReader(query), null);
} catch (TokenMgrError e) {
throw new ParseException(e.getMessage());
}
Deque<JexlNode> workingStack = new LinkedList<>();
workingStack.push(jexlScript);
int numReplaced = 0;
// iteratively traverse the tree, and replace the placeholder with single or double backslashes
while (!workingStack.isEmpty()) {
JexlNode node = workingStack.pop();
if (node.image != null) {
int numToReplace = StringUtils.countMatches(node.image, placeholder);
if (numToReplace > 0) {
// get the parent node (skipping references)
JexlNode parent = node;
do {
parent = parent.jjtGetParent();
} while (parent instanceof ASTReference);
// use the pre-compiled form of the string literal.
if (!(parent instanceof ASTERNode || parent instanceof ASTNRNode))
node.image = node.image.replace(placeholder, SINGLE_BACKSLASH);
else
node.image = node.image.replace(placeholder, DOUBLE_BACKSLASH);
numReplaced += numToReplace;
}
}
if (node.jjtGetNumChildren() > 0) {
for (JexlNode child : children(node)) {
if (child != null) {
workingStack.push(child);
}
}
}
}
if (numFound != numReplaced)
throw new ParseException("Did not find the expected number of backslash placeholders in the query. Expected: " + numFound + ", Actual: " + numReplaced);
return jexlScript;
}
use of org.apache.commons.jexl3.parser.ASTReference in project datawave by NationalSecurityAgency.
the class JexlASTHelper method getFunctionArguments.
public static List<JexlNode> getFunctionArguments(ASTFunctionNode function) {
List<JexlNode> args = Lists.newArrayList();
for (int i = 0; i < function.jjtGetNumChildren(); i++) {
JexlNode child = function.jjtGetChild(i);
// Arguments for the function are inside of an ASTReference
if (child.getClass().equals(ASTReference.class) && child.jjtGetNumChildren() == 1) {
JexlNode grandchild = child.jjtGetChild(0);
args.add(grandchild);
}
}
return args;
}
Aggregations