Search in sources :

Example 21 with ASTReference

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);
    }
}
Also used : ASTEQNode(org.apache.commons.jexl2.parser.ASTEQNode) ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) ASTReference(org.apache.commons.jexl2.parser.ASTReference) Test(org.junit.Test)

Example 22 with 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;
}
Also used : ASTReferenceExpression(org.apache.commons.jexl2.parser.ASTReferenceExpression) ASTReference(org.apache.commons.jexl2.parser.ASTReference)

Example 23 with ASTReference

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);
                }
            }
        }
    }
}
Also used : ASTReferenceExpression(org.apache.commons.jexl2.parser.ASTReferenceExpression) ASTStringLiteral(org.apache.commons.jexl2.parser.ASTStringLiteral) ASTIdentifier(org.apache.commons.jexl2.parser.ASTIdentifier) JexlNode(org.apache.commons.jexl2.parser.JexlNode) ASTReference(org.apache.commons.jexl2.parser.ASTReference) SimpleNode(org.apache.commons.jexl2.parser.SimpleNode) ASTNumberLiteral(org.apache.commons.jexl2.parser.ASTNumberLiteral)

Example 24 with ASTReference

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;
}
Also used : ASTERNode(org.apache.commons.jexl2.parser.ASTERNode) ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) ASTNRNode(org.apache.commons.jexl2.parser.ASTNRNode) StringReader(java.io.StringReader) JexlNode(org.apache.commons.jexl2.parser.JexlNode) TokenMgrError(org.apache.commons.jexl2.parser.TokenMgrError) ParseException(org.apache.commons.jexl2.parser.ParseException) LinkedList(java.util.LinkedList) ASTReference(org.apache.commons.jexl2.parser.ASTReference)

Example 25 with ASTReference

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;
}
Also used : JexlNode(org.apache.commons.jexl2.parser.JexlNode) ASTReference(org.apache.commons.jexl2.parser.ASTReference)

Aggregations

ASTReference (org.apache.commons.jexl2.parser.ASTReference)28 JexlNode (org.apache.commons.jexl2.parser.JexlNode)17 ASTReferenceExpression (org.apache.commons.jexl2.parser.ASTReferenceExpression)11 ASTIdentifier (org.apache.commons.jexl2.parser.ASTIdentifier)8 ASTAndNode (org.apache.commons.jexl2.parser.ASTAndNode)7 ASTOrNode (org.apache.commons.jexl2.parser.ASTOrNode)6 ASTStringLiteral (org.apache.commons.jexl2.parser.ASTStringLiteral)6 ASTEQNode (org.apache.commons.jexl2.parser.ASTEQNode)5 ArrayList (java.util.ArrayList)4 ASTERNode (org.apache.commons.jexl2.parser.ASTERNode)4 ASTNRNode (org.apache.commons.jexl2.parser.ASTNRNode)4 ASTIdentifierAccess (org.apache.commons.jexl3.parser.ASTIdentifierAccess)4 ExceededValueThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode)3 List (java.util.List)3 ASTAssignment (org.apache.commons.jexl2.parser.ASTAssignment)3 ASTFunctionNode (org.apache.commons.jexl2.parser.ASTFunctionNode)3 ASTJexlScript (org.apache.commons.jexl2.parser.ASTJexlScript)3 ASTNumberLiteral (org.apache.commons.jexl2.parser.ASTNumberLiteral)3 ASTIdentifier (org.apache.commons.jexl3.parser.ASTIdentifier)3 ASTReference (org.apache.commons.jexl3.parser.ASTReference)3