Browse Source

* do some calculations once in the constructor instead of for every evaluation of the filter

* make ArrayEvalFilter.ConditionStatement and PathTokenFilter static
pull/21/head
Jochen Berger 12 years ago
parent
commit
6320ac5c6c
  1. 27
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayEvalFilter.java
  2. 25
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayIndexFilter.java
  3. 6
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java
  4. 19
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/HasFieldFilter.java
  5. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/PathTokenFilter.java

27
json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayEvalFilter.java

@ -29,16 +29,11 @@ import java.util.regex.Pattern;
public class ArrayEvalFilter extends PathTokenFilter {
private static final Pattern PATTERN = Pattern.compile("(.*?)\\s?([=<>]+)\\s?(.*)");
private final ConditionStatement conditionStatement;
public ArrayEvalFilter(String condition) {
super(condition);
}
@Override
public Object filter(Object obj, JsonProvider jsonProvider) {
//[?(@.isbn == 10)]
List<Object> src = jsonProvider.toList(obj);
List<Object> result = jsonProvider.createList();
String trimmedCondition = condition;
@ -49,7 +44,13 @@ public class ArrayEvalFilter extends PathTokenFilter {
trimmedCondition = trim(trimmedCondition, 5, 2);
ConditionStatement conditionStatement = createConditionStatement(trimmedCondition);
this.conditionStatement = createConditionStatement(trimmedCondition);
}
@Override
public Object filter(Object obj, JsonProvider jsonProvider) {
List<Object> src = jsonProvider.toList(obj);
List<Object> result = jsonProvider.createList();
for (Object item : src) {
if (isMatch(item, conditionStatement, jsonProvider)) {
@ -101,18 +102,20 @@ public class ArrayEvalFilter extends PathTokenFilter {
}
}
private class ConditionStatement {
private static class ConditionStatement {
private final String field;
private final String operator;
private String expected;
private final String expected;
private ConditionStatement(String field, String operator, String expected) {
this.field = field;
this.operator = operator.trim();
this.expected = expected;
if(this.expected.startsWith("'")){
this.expected = trim(this.expected, 1, 1);
if(expected.startsWith("'")){
this.expected = trim(expected, 1, 1);
}else{
this.expected = expected;
}
}

25
json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayIndexFilter.java

@ -25,9 +25,22 @@ import java.util.regex.Pattern;
public class ArrayIndexFilter extends PathTokenFilter {
private static final Pattern SINGLE_ARRAY_INDEX_PATTERN = Pattern.compile("\\[\\d+\\]");
private static final Pattern COMMA = Pattern.compile(",");
private static final Pattern SPACE = Pattern.compile(" ");
private final String trimmedCondition;
public ArrayIndexFilter(String condition) {
super(condition);
String trimmedCondition = trim(condition, 1, 1);
if(trimmedCondition.contains("@.length")){
trimmedCondition = trim(trimmedCondition, 1, 1);
trimmedCondition = trimmedCondition.replace("@.length", "");
trimmedCondition = trimmedCondition + ":";
}
this.trimmedCondition = trimmedCondition;
}
@Override
@ -36,17 +49,11 @@ public class ArrayIndexFilter extends PathTokenFilter {
List<Object> src = jsonProvider.toList(obj);
List<Object> result = jsonProvider.createList();
String trimmedCondition = trim(condition, 1, 1);
if(trimmedCondition.contains("@.length")){
trimmedCondition = trim(trimmedCondition, 1, 1);
trimmedCondition = trimmedCondition.replace("@.length", "");
trimmedCondition = trimmedCondition + ":";
}
if (trimmedCondition.startsWith(":")) {
trimmedCondition = trim(trimmedCondition, 1, 0);
String trimmedCondition = trim(this.trimmedCondition, 1, 0);
int get = Integer.parseInt(trimmedCondition);
for (int i = 0; i < get; i++) {
result.add(src.get(i));
@ -54,12 +61,12 @@ public class ArrayIndexFilter extends PathTokenFilter {
return result;
} else if (trimmedCondition.endsWith(":")) {
trimmedCondition = trim(trimmedCondition.replace(" ", ""), 1, 1);
String trimmedCondition = trim(SPACE.matcher(this.trimmedCondition).replaceAll(""), 1, 1);
int get = Integer.parseInt(trimmedCondition);
return src.get(src.size() - get);
} else {
String[] indexArr = trimmedCondition.split(",");
String[] indexArr = COMMA.split(trimmedCondition);
if(src.isEmpty()){
return result;

6
json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java

@ -27,8 +27,11 @@ import java.util.Map;
*/
public class FieldFilter extends PathTokenFilter {
private final String[] split;
public FieldFilter(String condition) {
super(condition);
this.split = condition.split("','");
}
@Override
@ -43,7 +46,7 @@ public class FieldFilter extends PathTokenFilter {
Map<String, Object> map = jsonProvider.toMap(current);
String[] split = condition.split("','");
if(split.length == 1){
if (map.containsKey(condition)) {
Object o = map.get(condition);
@ -67,7 +70,6 @@ public class FieldFilter extends PathTokenFilter {
return result;
}
} else {
String[] split = condition.split("','");
Map<String, Object> map = jsonProvider.toMap(obj);
if(!map.containsKey(condition) && split.length == 1){

19
json-path/src/main/java/com/jayway/jsonpath/internal/filter/HasFieldFilter.java

@ -24,8 +24,18 @@ import java.util.Map;
*/
public class HasFieldFilter extends PathTokenFilter {
private final String trimmedCondition;
public HasFieldFilter(String condition) {
super(condition);
String trimmedCondition = condition;
if(condition.contains("['")){
trimmedCondition = trimmedCondition.replace("['", ".");
trimmedCondition = trimmedCondition.replace("']", "");
}
this.trimmedCondition = trim(trimmedCondition, 5, 2);
}
@Override
@ -35,15 +45,6 @@ public class HasFieldFilter extends PathTokenFilter {
List<Object> src = jsonProvider.toList(obj);
List<Object> result = jsonProvider.createList();
String trimmedCondition = condition;
if(condition.contains("['")){
trimmedCondition = trimmedCondition.replace("['", ".");
trimmedCondition = trimmedCondition.replace("']", "");
}
trimmedCondition = trim(trimmedCondition, 5, 2);
for (Object item : src) {
if(jsonProvider.isMap(item)){
Map<String, Object> map = jsonProvider.toMap(item);

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

@ -34,7 +34,7 @@ public abstract class PathTokenFilter {
return condition;
}
String trim(String str, int front, int end) {
static String trim(String str, int front, int end) {
String res = str;
if (front > 0) {

Loading…
Cancel
Save