Search in sources :

Example 16 with ASTStringLiteral

use of org.apache.commons.jexl3.parser.ASTStringLiteral in project datawave by NationalSecurityAgency.

the class JexlNodeFactory method getLiteral.

private static ASTStringLiteral getLiteral(String fieldValue) {
    ASTStringLiteral literal = new ASTStringLiteral(ParserTreeConstants.JJTSTRINGLITERAL);
    literal.image = fieldValue;
    return literal;
}
Also used : ASTStringLiteral(org.apache.commons.jexl2.parser.ASTStringLiteral)

Example 17 with ASTStringLiteral

use of org.apache.commons.jexl3.parser.ASTStringLiteral in project commons-jexl by apache.

the class Engine method getVariables.

/**
 * Fills up the list of variables accessed by a node.
 * @param script the owning script
 * @param node the node
 * @param collector the variable collector
 */
protected void getVariables(final ASTJexlScript script, final JexlNode node, final VarCollector collector) {
    if (node instanceof ASTIdentifier) {
        final JexlNode parent = node.jjtGetParent();
        if (parent instanceof ASTMethodNode || parent instanceof ASTFunctionNode) {
            // skip identifiers for methods and functions
            collector.collect(null);
            return;
        }
        final ASTIdentifier identifier = (ASTIdentifier) node;
        final int symbol = identifier.getSymbol();
        // symbols that are captured are considered "global" variables
        if (symbol >= 0 && script != null && !script.isCapturedSymbol(symbol)) {
            collector.collect(null);
        } else {
            // start collecting from identifier
            collector.collect(identifier);
            collector.add(identifier.getName());
        }
    } else if (node instanceof ASTIdentifierAccess) {
        final JexlNode parent = node.jjtGetParent();
        if (parent instanceof ASTMethodNode || parent instanceof ASTFunctionNode) {
            // skip identifiers for methods and functions
            collector.collect(null);
            return;
        }
        // belt and suspender since an identifier should have been seen first
        if (collector.isCollecting()) {
            collector.add(((ASTIdentifierAccess) node).getName());
        }
    } else if (node instanceof ASTArrayAccess && collector.mode > 0) {
        final int num = node.jjtGetNumChildren();
        // collect only if array access is const and follows an identifier
        boolean collecting = collector.isCollecting();
        for (int i = 0; i < num; ++i) {
            final JexlNode child = node.jjtGetChild(i);
            if (collecting && child.isConstant()) {
                // collect all constants or only string and number literals
                final boolean collect = collector.mode > 1 || (child instanceof ASTStringLiteral || child instanceof ASTNumberLiteral);
                if (collect) {
                    final String image = child.toString();
                    collector.add(image);
                }
            } else {
                collecting = false;
                collector.collect(null);
                getVariables(script, child, collector);
                collector.collect(null);
            }
        }
    } else {
        final int num = node.jjtGetNumChildren();
        for (int i = 0; i < num; ++i) {
            getVariables(script, node.jjtGetChild(i), collector);
        }
        collector.collect(null);
    }
}
Also used : ASTStringLiteral(org.apache.commons.jexl3.parser.ASTStringLiteral) ASTFunctionNode(org.apache.commons.jexl3.parser.ASTFunctionNode) ASTIdentifier(org.apache.commons.jexl3.parser.ASTIdentifier) JexlNode(org.apache.commons.jexl3.parser.JexlNode) ASTMethodNode(org.apache.commons.jexl3.parser.ASTMethodNode) ASTIdentifierAccess(org.apache.commons.jexl3.parser.ASTIdentifierAccess) ASTArrayAccess(org.apache.commons.jexl3.parser.ASTArrayAccess) ASTNumberLiteral(org.apache.commons.jexl3.parser.ASTNumberLiteral)

Example 18 with ASTStringLiteral

use of org.apache.commons.jexl3.parser.ASTStringLiteral in project nexus-public by sonatype.

the class DatastoreCselToSql method transformMatchesOperator.

protected SelectorSqlBuilder transformMatchesOperator(final JexlNode node, final String operator, final SelectorSqlBuilder builder) {
    JexlNode leftChild = node.jjtGetChild(LEFT);
    JexlNode rightChild = node.jjtGetChild(RIGHT);
    leftChild.jjtAccept(this, builder);
    builder.appendOperator(operator);
    if (rightChild instanceof ASTStringLiteral) {
        String pattern = ((ASTStringLiteral) rightChild).getLiteral();
        if (pattern.charAt(0) != '^') {
            // match entire string
            pattern = "^(" + pattern + ")$";
        }
        builder.appendLiteral(pattern);
    } else {
        throw new JexlException(node, EXPECTED_STRING_LITERAL);
    }
    return builder;
}
Also used : ASTStringLiteral(org.apache.commons.jexl3.parser.ASTStringLiteral) JexlException(org.apache.commons.jexl3.JexlException) JexlNode(org.apache.commons.jexl3.parser.JexlNode)

Example 19 with ASTStringLiteral

use of org.apache.commons.jexl3.parser.ASTStringLiteral in project nexus-public by sonatype.

the class DatastoreCselToSql method visit.

/**
 * Transform `a != b` into `(a is null or a <> b)`
 */
@Override
protected Object visit(final ASTNENode node, final Object data) {
    JexlNode leftChild = node.jjtGetChild(LEFT);
    JexlNode rightChild = node.jjtGetChild(RIGHT);
    if (rightChild instanceof ASTStringLiteral) {
        transformNotEqualsOperator(leftChild, (ASTStringLiteral) rightChild, (SelectorSqlBuilder) data);
    } else if (leftChild instanceof ASTStringLiteral) {
        transformNotEqualsOperator(rightChild, (ASTStringLiteral) leftChild, (SelectorSqlBuilder) data);
    } else {
        throw new JexlException(node, EXPECTED_STRING_LITERAL);
    }
    return data;
}
Also used : ASTStringLiteral(org.apache.commons.jexl3.parser.ASTStringLiteral) SelectorSqlBuilder(org.sonatype.nexus.selector.SelectorSqlBuilder) JexlException(org.apache.commons.jexl3.JexlException) JexlNode(org.apache.commons.jexl3.parser.JexlNode)

Example 20 with ASTStringLiteral

use of org.apache.commons.jexl3.parser.ASTStringLiteral in project nexus-public by sonatype.

the class LeadingSlashScriptTransformer method doVisit.

@Override
protected Object doVisit(final JexlNode node, final Object data) {
    if (node.jjtGetNumChildren() == 2) {
        Node leftChild = node.jjtGetChild(LEFT);
        Node rightChild = node.jjtGetChild(RIGHT);
        if (isPathExpression(leftChild, rightChild)) {
            transformPathLiteral((ASTStringLiteral) rightChild);
        } else if (isPathExpression(rightChild, leftChild)) {
            transformPathLiteral((ASTStringLiteral) leftChild);
        } else {
            leftChild.jjtAccept(this, data);
            rightChild.jjtAccept(this, data);
        }
    } else {
        node.childrenAccept(this, data);
    }
    return data;
}
Also used : ASTStringLiteral(org.apache.commons.jexl3.parser.ASTStringLiteral) Node(org.apache.commons.jexl3.parser.Node) JexlNode(org.apache.commons.jexl3.parser.JexlNode)

Aggregations

ASTStringLiteral (org.apache.commons.jexl2.parser.ASTStringLiteral)13 ASTStringLiteral (org.apache.commons.jexl3.parser.ASTStringLiteral)7 JexlNode (org.apache.commons.jexl3.parser.JexlNode)7 ASTIdentifier (org.apache.commons.jexl2.parser.ASTIdentifier)6 ASTReference (org.apache.commons.jexl2.parser.ASTReference)6 JexlNode (org.apache.commons.jexl2.parser.JexlNode)5 ASTFunctionNode (org.apache.commons.jexl2.parser.ASTFunctionNode)3 ASTNumberLiteral (org.apache.commons.jexl2.parser.ASTNumberLiteral)3 JexlException (org.apache.commons.jexl3.JexlException)3 ASTAssignment (org.apache.commons.jexl2.parser.ASTAssignment)2 ASTEQNode (org.apache.commons.jexl2.parser.ASTEQNode)2 ASTReferenceExpression (org.apache.commons.jexl2.parser.ASTReferenceExpression)2 DatawaveFatalQueryException (datawave.query.exceptions.DatawaveFatalQueryException)1 FunctionJexlNodeVisitor (datawave.query.jexl.functions.FunctionJexlNodeVisitor)1 JexlArgumentDescriptor (datawave.query.jexl.functions.arguments.JexlArgumentDescriptor)1 ExceededTermThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededTermThresholdMarkerJexlNode)1 ExceededValueThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode)1 QueryException (datawave.webservice.query.exception.QueryException)1 JexlContext (org.apache.commons.jexl2.JexlContext)1 JexlEngine (org.apache.commons.jexl2.JexlEngine)1