|
|
@ -14,81 +14,142 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
package com.jayway.jsonpath.internal.filter.eval; |
|
|
|
package com.jayway.jsonpath.internal.filter.eval; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Collections; |
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @author Kalle Stenflo |
|
|
|
* @author Kalle Stenflo |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class ExpressionEvaluator { |
|
|
|
public class ExpressionEvaluator { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public enum Operator { |
|
|
|
|
|
|
|
equal("=="), not_equal("!="), less_or_greater_than("<>"),greater_than(">"), greater_than_or_equal(">="), less_than("<"), less_than_or_equal("<="); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final String representation; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Operator(String representation) { |
|
|
|
|
|
|
|
this.representation = representation; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getRepresentation() { |
|
|
|
|
|
|
|
return representation; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static Map<String, Operator> operatorsByRepresentation; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static { |
|
|
|
|
|
|
|
Map<String, Operator> map = new HashMap<String, Operator>(); |
|
|
|
|
|
|
|
for (Operator op : Operator.values()){ |
|
|
|
|
|
|
|
map.put(op.getRepresentation(), op); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ExpressionEvaluator.operatorsByRepresentation = Collections.unmodifiableMap(map); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static <T> boolean eval(T actual, String comparator, String expected) { |
|
|
|
public static <T> boolean eval(T actual, String comparator, String expected) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Operator operator = operatorsByRepresentation.get(comparator); |
|
|
|
|
|
|
|
if (operator == null){ |
|
|
|
|
|
|
|
throw new IllegalArgumentException("Unsupported operator " + comparator); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (actual instanceof Long) { |
|
|
|
if (actual instanceof Long) { |
|
|
|
|
|
|
|
|
|
|
|
Long a = (Long) actual; |
|
|
|
Long a = (Long) actual; |
|
|
|
Long e = Long.parseLong(expected.trim()); |
|
|
|
Long e = Long.parseLong(expected.trim()); |
|
|
|
|
|
|
|
switch (operator){ |
|
|
|
if ("==".equals(comparator)) { |
|
|
|
case equal: |
|
|
|
return a.longValue() == e.longValue(); |
|
|
|
return a.longValue() == e.longValue(); |
|
|
|
} else if ("!=".equals(comparator) || "<>".equals(comparator)) { |
|
|
|
case not_equal: |
|
|
|
|
|
|
|
case less_or_greater_than: |
|
|
|
return a.longValue() != e.longValue(); |
|
|
|
return a.longValue() != e.longValue(); |
|
|
|
} else if (">".equals(comparator)) { |
|
|
|
case greater_than: |
|
|
|
return a > e; |
|
|
|
return a > e; |
|
|
|
} else if (">=".equals(comparator)) { |
|
|
|
case greater_than_or_equal: |
|
|
|
return a >= e; |
|
|
|
return a >= e; |
|
|
|
} else if ("<".equals(comparator)) { |
|
|
|
case less_than: |
|
|
|
return a < e; |
|
|
|
return a < e; |
|
|
|
} else if ("<=".equals(comparator)) { |
|
|
|
case less_than_or_equal: |
|
|
|
return a <= e; |
|
|
|
return a <= e; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
throw new UnsupportedOperationException("Cannot handle operator " + operator); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else if (actual instanceof Integer) { |
|
|
|
} else if (actual instanceof Integer) { |
|
|
|
Integer a = (Integer) actual; |
|
|
|
Integer a = (Integer) actual; |
|
|
|
Integer e = Integer.parseInt(expected.trim()); |
|
|
|
Integer e = Integer.parseInt(expected.trim()); |
|
|
|
|
|
|
|
|
|
|
|
if ("==".equals(comparator)) { |
|
|
|
switch (operator){ |
|
|
|
|
|
|
|
case equal: |
|
|
|
return a.intValue() == e.intValue(); |
|
|
|
return a.intValue() == e.intValue(); |
|
|
|
} else if ("!=".equals(comparator) || "<>".equals(comparator)) { |
|
|
|
case not_equal: |
|
|
|
|
|
|
|
case less_or_greater_than: |
|
|
|
return a.intValue() != e.intValue(); |
|
|
|
return a.intValue() != e.intValue(); |
|
|
|
} else if (">".equals(comparator)) { |
|
|
|
case greater_than: |
|
|
|
return a > e; |
|
|
|
return a > e; |
|
|
|
} else if (">=".equals(comparator)) { |
|
|
|
case greater_than_or_equal: |
|
|
|
return a >= e; |
|
|
|
return a >= e; |
|
|
|
} else if ("<".equals(comparator)) { |
|
|
|
case less_than: |
|
|
|
return a < e; |
|
|
|
return a < e; |
|
|
|
} else if ("<=".equals(comparator)) { |
|
|
|
case less_than_or_equal: |
|
|
|
return a <= e; |
|
|
|
return a <= e; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
throw new UnsupportedOperationException("Cannot handle operator " + operator); |
|
|
|
} |
|
|
|
} |
|
|
|
} else if (actual instanceof Double) { |
|
|
|
} else if (actual instanceof Double) { |
|
|
|
|
|
|
|
|
|
|
|
Double a = (Double) actual; |
|
|
|
Double a = (Double) actual; |
|
|
|
Double e = Double.parseDouble(expected.trim()); |
|
|
|
Double e = Double.parseDouble(expected.trim()); |
|
|
|
|
|
|
|
|
|
|
|
if ("==".equals(comparator)) { |
|
|
|
switch (operator){ |
|
|
|
|
|
|
|
case equal: |
|
|
|
return a.doubleValue() == e.doubleValue(); |
|
|
|
return a.doubleValue() == e.doubleValue(); |
|
|
|
} else if ("!=".equals(comparator) || "<>".equals(comparator)) { |
|
|
|
case not_equal: |
|
|
|
|
|
|
|
case less_or_greater_than: |
|
|
|
return a.doubleValue() != e.doubleValue(); |
|
|
|
return a.doubleValue() != e.doubleValue(); |
|
|
|
} else if (">".equals(comparator)) { |
|
|
|
case greater_than: |
|
|
|
return a > e; |
|
|
|
return a > e; |
|
|
|
} else if (">=".equals(comparator)) { |
|
|
|
case greater_than_or_equal: |
|
|
|
return a >= e; |
|
|
|
return a >= e; |
|
|
|
} else if ("<".equals(comparator)) { |
|
|
|
case less_than: |
|
|
|
return a < e; |
|
|
|
return a < e; |
|
|
|
} else if ("<=".equals(comparator)) { |
|
|
|
case less_than_or_equal: |
|
|
|
return a <= e; |
|
|
|
return a <= e; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
throw new UnsupportedOperationException("Cannot handle operator " + operator); |
|
|
|
} |
|
|
|
} |
|
|
|
} else if (actual instanceof String) { |
|
|
|
} else if (actual instanceof String) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (operator){ |
|
|
|
|
|
|
|
case greater_than: |
|
|
|
|
|
|
|
case greater_than_or_equal: |
|
|
|
|
|
|
|
case less_than: |
|
|
|
|
|
|
|
case less_than_or_equal: |
|
|
|
|
|
|
|
// we might want to throw an exception here
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
case equal: |
|
|
|
|
|
|
|
case not_equal: |
|
|
|
|
|
|
|
case less_or_greater_than: |
|
|
|
String a = (String)actual; |
|
|
|
String a = (String)actual; |
|
|
|
expected = expected.trim(); |
|
|
|
String expectedTrimmed = expected.trim(); |
|
|
|
if(expected.startsWith("'")) { |
|
|
|
if(expectedTrimmed.startsWith("'")) { |
|
|
|
expected = expected.substring(1); |
|
|
|
expectedTrimmed = expectedTrimmed.substring(1); |
|
|
|
} |
|
|
|
} |
|
|
|
if(expected.endsWith("'")){ |
|
|
|
if(expectedTrimmed.endsWith("'")){ |
|
|
|
expected = expected.substring(0, expected.length()-1); |
|
|
|
expectedTrimmed = expectedTrimmed.substring(0, expected.length()-1); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ("==".equals(comparator)) { |
|
|
|
if (operator == Operator.equal) { |
|
|
|
return a.equals(expected); |
|
|
|
return a.equals(expectedTrimmed); |
|
|
|
} else if ("!=".equals(comparator) || "<>".equals(comparator)) { |
|
|
|
} else if (operator == Operator.not_equal || operator == Operator.less_or_greater_than) { |
|
|
|
return !a.equals(expected); |
|
|
|
return !a.equals(expectedTrimmed); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
throw new UnsupportedOperationException("Cannot handle operator " + operator); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|