use of org.apache.commons.jexl2.parser.ASTNumberLiteral in project datawave by NationalSecurityAgency.
the class FixNegativeNumbersVisitor method visit.
@Override
public Object visit(ASTUnaryMinusNode astumn, Object data) {
if (astumn.jjtGetNumChildren() == 1 && astumn.jjtGetChild(0) instanceof ASTNumberLiteral) {
ASTNumberLiteral node = (ASTNumberLiteral) astumn.jjtGetChild(0);
ASTNumberLiteral newNode = new ASTNumberLiteral(ParserTreeConstants.JJTNUMBERLITERAL);
String value = "-" + node.image;
newNode.image = value;
newNode.jjtSetParent(node.jjtGetParent());
if (JexlNodeFactory.NATURAL_NUMBERS.contains(node.getLiteralClass())) {
newNode.setNatural(value);
} else if (JexlNodeFactory.REAL_NUMBERS.contains(node.getLiteralClass())) {
newNode.setReal(value);
} else {
throw new IllegalArgumentException("Could not ascertain type of ASTNumberLiteral: " + node);
}
newNode.jjtSetValue(value);
return newNode;
} else {
return super.visit(astumn, data);
}
}
use of org.apache.commons.jexl2.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;
}
use of org.apache.commons.jexl2.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;
}
use of org.apache.commons.jexl2.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);
}
}
}
}
}
use of org.apache.commons.jexl2.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;
}
Aggregations