Search in sources :

Example 11 with ASTNumberLiteral

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

the class JexlStringBuildingVisitor method visit.

@Override
public Object visit(ASTMethodNode node, Object data) {
    StringBuilder sb = (StringBuilder) data;
    StringBuilder methodStringBuilder = new StringBuilder();
    StringBuilder argumentStringBuilder = new StringBuilder();
    int kidCount = node.jjtGetNumChildren();
    for (int i = 0; i < kidCount; i++) {
        if (i == 0) {
            JexlNode methodNode = node.jjtGetChild(i);
            methodStringBuilder.append(".");
            if (allowedMethods.contains(methodNode.image) == false) {
                QueryException qe = new QueryException(DatawaveErrorCode.METHOD_COMPOSITION_ERROR, MessageFormat.format("{0}", methodNode.image));
                throw new DatawaveFatalQueryException(qe);
            }
            methodStringBuilder.append(methodNode.image);
            // parens are open. don't forget to close
            methodStringBuilder.append("(");
        } else {
            // adding any method arguments
            JexlNode argumentNode = node.jjtGetChild(i);
            if (argumentNode instanceof ASTReference) {
                // a method may have an argument that is another method. In this case, descend the visit tree for it
                if (JexlASTHelper.HasMethodVisitor.hasMethod(argumentNode)) {
                    this.visit((ASTReference) argumentNode, argumentStringBuilder);
                } else {
                    for (int j = 0; j < argumentNode.jjtGetNumChildren(); j++) {
                        JexlNode argKid = argumentNode.jjtGetChild(j);
                        if (argKid instanceof ASTFunctionNode) {
                            this.visit((ASTFunctionNode) argKid, argumentStringBuilder);
                        } else {
                            if (argumentStringBuilder.length() > 0) {
                                argumentStringBuilder.append(",");
                            }
                            if (argKid instanceof ASTStringLiteral) {
                                argumentStringBuilder.append("'");
                            }
                            argumentStringBuilder.append(argKid.image);
                            if (argKid instanceof ASTStringLiteral) {
                                argumentStringBuilder.append("'");
                            }
                        }
                    }
                }
            } else if (argumentNode instanceof ASTNumberLiteral) {
                if (argumentStringBuilder.length() > 0) {
                    argumentStringBuilder.append(",");
                }
                argumentStringBuilder.append(argumentNode.image);
            }
        }
    }
    methodStringBuilder.append(argumentStringBuilder);
    // close parens in method
    methodStringBuilder.append(")");
    sb.append(methodStringBuilder);
    return sb;
}
Also used : ASTStringLiteral(org.apache.commons.jexl2.parser.ASTStringLiteral) DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) QueryException(datawave.webservice.query.exception.QueryException) ASTFunctionNode(org.apache.commons.jexl2.parser.ASTFunctionNode) DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) JexlNode(org.apache.commons.jexl2.parser.JexlNode) ASTReference(org.apache.commons.jexl2.parser.ASTReference) ASTNumberLiteral(org.apache.commons.jexl2.parser.ASTNumberLiteral)

Example 12 with ASTNumberLiteral

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

the class RebuildingVisitor method visit.

@Override
public Object visit(ASTIntegerLiteral node, Object data) {
    ASTNumberLiteral newNode = new ASTNumberLiteral(ParserTreeConstants.JJTNUMBERLITERAL);
    newNode.setNatural(node.getLiteral().toString());
    newNode.jjtSetParent(node.jjtGetParent());
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        newNode.jjtAddChild((Node) node.jjtGetChild(i).jjtAccept(this, data), i);
    }
    return newNode;
}
Also used : ASTNumberLiteral(org.apache.commons.jexl2.parser.ASTNumberLiteral)

Example 13 with ASTNumberLiteral

use of org.apache.commons.jexl3.parser.ASTNumberLiteral 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 14 with ASTNumberLiteral

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

the class JexlNodeFactory method buildNewLiteralNode.

public static JexlNode buildNewLiteralNode(JexlNode original, String fieldName, Number fieldValue) {
    ASTReference literalReference = new ASTReference(ParserTreeConstants.JJTREFERENCE);
    ASTNumberLiteral literal = getLiteral(fieldValue);
    literalReference.jjtAddChild(literal, 0);
    literal.jjtSetParent(literalReference);
    // we don't know whether the left or right side is the literal to replace. find it.
    if (original.jjtGetChild(0) instanceof ASTReference && original.jjtGetChild(0).jjtGetChild(0) instanceof ASTIdentifier) {
        // replace the original reference/literal (on left) with new reference/literal
        original.jjtAddChild(literalReference, 1);
    }
    if (original.jjtGetChild(1) instanceof ASTReference && original.jjtGetChild(1).jjtGetChild(0) instanceof ASTIdentifier) {
        // replace the original reference/literal (on right) with new reference/literal
        original.jjtAddChild(literalReference, 0);
    }
    literalReference.jjtSetParent(original);
    return original;
}
Also used : ASTIdentifier(org.apache.commons.jexl2.parser.ASTIdentifier) ASTReference(org.apache.commons.jexl2.parser.ASTReference) ASTNumberLiteral(org.apache.commons.jexl2.parser.ASTNumberLiteral)

Example 15 with ASTNumberLiteral

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

the class JexlNodeFactory method buildUntypedNewNode.

/**
 * Build a JexlNode with a Number literal
 *
 * @param fieldName
 * @param fieldValue
 * @return
 */
protected static JexlNode buildUntypedNewNode(JexlNode newNode, ASTIdentifier fieldName, Number fieldValue) {
    ASTNumberLiteral literal = new ASTNumberLiteral(ParserTreeConstants.JJTNUMBERLITERAL);
    literal.image = fieldValue.toString();
    if (NATURAL_NUMBERS.contains(fieldValue.getClass())) {
        literal.setNatural(fieldValue.toString());
    } else if (REAL_NUMBERS.contains(fieldValue.getClass())) {
        literal.setReal(fieldValue.toString());
    } else {
        QueryException qe = new QueryException(DatawaveErrorCode.ASTNUMBERLITERAL_TYPE_ASCERTAIN_ERROR, MessageFormat.format("{0}", literal));
        throw new DatawaveFatalQueryException(qe);
    }
    return buildUntypedNewNode(newNode, fieldName, literal);
}
Also used : DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) QueryException(datawave.webservice.query.exception.QueryException) DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) ASTNumberLiteral(org.apache.commons.jexl2.parser.ASTNumberLiteral)

Aggregations

ASTNumberLiteral (org.apache.commons.jexl2.parser.ASTNumberLiteral)10 ASTNumberLiteral (org.apache.commons.jexl3.parser.ASTNumberLiteral)5 JexlNode (org.apache.commons.jexl3.parser.JexlNode)5 ASTReference (org.apache.commons.jexl2.parser.ASTReference)4 JexlNode (org.apache.commons.jexl2.parser.JexlNode)4 DatawaveFatalQueryException (datawave.query.exceptions.DatawaveFatalQueryException)3 QueryException (datawave.webservice.query.exception.QueryException)3 ASTIdentifier (org.apache.commons.jexl2.parser.ASTIdentifier)3 ASTStringLiteral (org.apache.commons.jexl2.parser.ASTStringLiteral)3 ASTFunctionNode (org.apache.commons.jexl3.parser.ASTFunctionNode)3 ASTIdentifier (org.apache.commons.jexl3.parser.ASTIdentifier)3 ASTFunctionNode (org.apache.commons.jexl2.parser.ASTFunctionNode)2 ASTReferenceExpression (org.apache.commons.jexl2.parser.ASTReferenceExpression)2 JexlException (org.apache.commons.jexl3.JexlException)2 JexlMethod (org.apache.commons.jexl3.introspection.JexlMethod)2 ExceededTermThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededTermThresholdMarkerJexlNode)1 ExceededValueThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode)1 ASTAdditiveNode (org.apache.commons.jexl2.parser.ASTAdditiveNode)1 ASTAndNode (org.apache.commons.jexl2.parser.ASTAndNode)1 ASTAssignment (org.apache.commons.jexl2.parser.ASTAssignment)1