|
|
@ -29,23 +29,24 @@ import java.util.regex.Pattern; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class ArrayEvalFilter extends PathTokenFilter { |
|
|
|
public class ArrayEvalFilter extends PathTokenFilter { |
|
|
|
|
|
|
|
|
|
|
|
private static final Pattern PATTERN = Pattern.compile("\\[\\s?\\?\\(\\s?(@.*?)\\s?([!=<>]+)\\s?(.*?)\\s?\\)\\s?\\]"); |
|
|
|
private static final Pattern PATTERN = Pattern.compile("\\[\\s?\\?\\(\\s?(@.*?)\\s?([!=<>]+)\\s?(.*?)\\s?\\)\\s?]"); |
|
|
|
|
|
|
|
|
|
|
|
private final ConditionStatement conditionStatement; |
|
|
|
private final ConditionStatement conditionStatement; |
|
|
|
|
|
|
|
|
|
|
|
public ArrayEvalFilter(String condition) { |
|
|
|
public ArrayEvalFilter(ConditionStatement statement) { |
|
|
|
super(condition); |
|
|
|
super(statement.condition); |
|
|
|
//[?(@.isbn == 10)]
|
|
|
|
this.conditionStatement = statement; |
|
|
|
this.conditionStatement = createConditionStatement(condition); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public Object filter(Object obj, JsonProvider jsonProvider) { |
|
|
|
public Object filter(Object obj, JsonProvider jsonProvider) { |
|
|
|
Iterable<Object> src = null; |
|
|
|
Iterable<Object> src = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
src = jsonProvider.toIterable(obj); |
|
|
|
src = jsonProvider.toIterable(obj); |
|
|
|
} catch (ClassCastException e){ |
|
|
|
} catch (ClassCastException e){ |
|
|
|
throw new InvalidPathException("The path fragment '" + this.condition + "' can not be applied to a JSON object only a JSON array.", e); |
|
|
|
throw new PathNotFoundException("The path fragment '" + this.condition + "' can not be applied to a JSON object only a JSON array.", e); |
|
|
|
} |
|
|
|
} |
|
|
|
Object result = jsonProvider.createArray(); |
|
|
|
Object result = jsonProvider.createArray(); |
|
|
|
for (Object item : src) { |
|
|
|
for (Object item : src) { |
|
|
@ -78,27 +79,29 @@ public class ArrayEvalFilter extends PathTokenFilter { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static ConditionStatement createConditionStatement(String str) { |
|
|
|
static ConditionStatement createConditionStatement(String condition) { |
|
|
|
Matcher matcher = PATTERN.matcher(str); |
|
|
|
Matcher matcher = PATTERN.matcher(condition); |
|
|
|
if (matcher.matches()) { |
|
|
|
if (matcher.matches()) { |
|
|
|
String property = matcher.group(1).trim(); |
|
|
|
String property = matcher.group(1).trim(); |
|
|
|
String operator = matcher.group(2).trim(); |
|
|
|
String operator = matcher.group(2).trim(); |
|
|
|
String expected = matcher.group(3).trim(); |
|
|
|
String expected = matcher.group(3).trim(); |
|
|
|
|
|
|
|
|
|
|
|
return new ConditionStatement(property, operator, expected); |
|
|
|
return new ConditionStatement(condition, property, operator, expected); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
throw new InvalidPathException("Invalid match " + str); |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static class ConditionStatement { |
|
|
|
static class ConditionStatement { |
|
|
|
|
|
|
|
private final String condition; |
|
|
|
private final String field; |
|
|
|
private final String field; |
|
|
|
private final String operator; |
|
|
|
private final String operator; |
|
|
|
private final String expected; |
|
|
|
private final String expected; |
|
|
|
private final JsonPath path; |
|
|
|
private final JsonPath path; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ConditionStatement(String field, String operator, String expected) { |
|
|
|
ConditionStatement(String condition, String field, String operator, String expected) { |
|
|
|
|
|
|
|
this.condition = condition; |
|
|
|
this.field = field; |
|
|
|
this.field = field; |
|
|
|
this.operator = operator; |
|
|
|
this.operator = operator; |
|
|
|
|
|
|
|
|
|
|
@ -114,7 +117,13 @@ public class ArrayEvalFilter extends PathTokenFilter { |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
this.path = JsonPath.compile(this.field.replace("@", "$")); |
|
|
|
this.path = JsonPath.compile(this.field.replace("@", "$")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ConditionStatement(String field, String operator, String expected) { |
|
|
|
|
|
|
|
this(null, field, operator, expected); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String getCondition() { |
|
|
|
|
|
|
|
return condition; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public JsonPath getJsonPath() { |
|
|
|
public JsonPath getJsonPath() { |
|
|
|