Browse Source

implement not (!) operator in filter expressions

pull/283/head
Ari Fogel 8 years ago
parent
commit
38c7e791f6
  1. 15
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java
  2. 10
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/LogicalExpressionNode.java
  3. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/LogicalOperator.java

15
json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java

@ -120,7 +120,7 @@ public class FilterCompiler {
/*
* LogicalOR = LogicalAND { '||' LogicalAND }
* LogicalAND = LogicalANDOperand { '&&' LogicalANDOperand }
* LogicalANDOperand = RelationalExpression | '(' LogicalOR ')'
* LogicalANDOperand = RelationalExpression | '(' LogicalOR ')' | '!' LogicalANDOperand
* RelationalExpression = Value [ RelationalOperator Value ]
*/
@ -164,6 +164,19 @@ public class FilterCompiler {
}
private ExpressionNode readLogicalANDOperand() {
int savepoint = filter.skipBlanks().position();
if (filter.skipBlanks().currentCharIs(NOT)) {
filter.readSignificantChar(NOT);
switch (filter.skipBlanks().currentChar()) {
case DOC_CONTEXT:
case EVAL_CONTEXT:
filter.setPosition(savepoint);
break;
default:
final ExpressionNode op = readLogicalANDOperand();
return LogicalExpressionNode.createLogicalNot(op);
}
}
if (filter.skipBlanks().currentCharIs(OPEN_PARENTHESIS)) {
filter.readSignificantChar(OPEN_PARENTHESIS);
final ExpressionNode op = readLogicalOR();

10
json-path/src/main/java/com/jayway/jsonpath/internal/filter/LogicalExpressionNode.java

@ -10,6 +10,10 @@ public class LogicalExpressionNode extends ExpressionNode {
protected List<ExpressionNode> chain = new ArrayList<ExpressionNode>();
private final LogicalOperator operator;
public static ExpressionNode createLogicalNot(ExpressionNode op) {
return new LogicalExpressionNode(op, LogicalOperator.NOT, null);
}
public static LogicalExpressionNode createLogicalOr(ExpressionNode left,ExpressionNode right){
return new LogicalExpressionNode(left, LogicalOperator.OR, right);
}
@ -68,13 +72,17 @@ public class LogicalExpressionNode extends ExpressionNode {
}
}
return false;
} else {
} else if (operator == LogicalOperator.AND) {
for (ExpressionNode expression : chain) {
if(!expression.apply(ctx)){
return false;
}
}
return true;
} else {
ExpressionNode expression = chain.get(0);
return !expression.apply(ctx);
}
}
}

2
json-path/src/main/java/com/jayway/jsonpath/internal/filter/LogicalOperator.java

@ -5,6 +5,7 @@ import com.jayway.jsonpath.InvalidPathException;
public enum LogicalOperator {
AND("&&"),
NOT("!"),
OR("||");
private final String operatorString;
@ -24,6 +25,7 @@ public enum LogicalOperator {
public static LogicalOperator fromString(String operatorString){
if(AND.operatorString.equals(operatorString)) return AND;
else if(NOT.operatorString.equals(operatorString)) return NOT;
else if(OR.operatorString.equals(operatorString)) return OR;
else throw new InvalidPathException("Failed to parse operator " + operatorString);
}

Loading…
Cancel
Save