Browse Source

minor refactorings

pull/1/merge
kalle 14 years ago
parent
commit
ef00f81fc8
  1. 26
      json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterFactory.java
  2. 2
      json-path/src/main/java/com/jayway/jsonpath/filter/ListFilter.java
  3. 19
      json-path/src/main/java/com/jayway/jsonpath/filter/PropertyFilter.java
  4. 5
      json-path/src/main/java/com/jayway/jsonpath/filter/TraverseFilter.java

26
json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterFactory.java

@ -9,26 +9,30 @@ import java.util.regex.Pattern;
*/
public class JsonPathFilterFactory {
private static final Pattern ROOT_FILTER = Pattern.compile("\\$");
private static final Pattern PROPERTY_FILTER = Pattern.compile("\\w+");
private static final Pattern WILDCARD_PROPERTY_FILTER = Pattern.compile("\\*");
private static final Pattern LIST_FILTER = Pattern.compile("\\[.*?\\]");
private static final Pattern TRAVERSE_FILTER = Pattern.compile("\\.\\.");
private final static Pattern ROOT_FILTER_PATTERN = Pattern.compile("\\$");
private final static Pattern PROPERTY_FILTER_PATTERN = Pattern.compile("\\w+");
private final static Pattern WILDCARD_PROPERTY_FILTER_PATTERN = Pattern.compile("\\*");
private final static Pattern LIST_FILTER_PATTERN = Pattern.compile("\\[.*?\\]");
private final static Pattern TRAVERSE_FILTER_PATTERN = Pattern.compile("\\.\\.");
private final static RootFilter ROOT_FILTER = new RootFilter();
private final static TraverseFilter TRAVERSE_FILTER = new TraverseFilter();
public static JsonPathFilterBase createFilter(String pathFragment){
if(ROOT_FILTER.matcher(pathFragment).matches()){
return new RootFilter();
if(ROOT_FILTER_PATTERN.matcher(pathFragment).matches()){
return ROOT_FILTER;
}
else if(PROPERTY_FILTER.matcher(pathFragment).matches() || WILDCARD_PROPERTY_FILTER.matcher(pathFragment).matches() ){
else if(PROPERTY_FILTER_PATTERN.matcher(pathFragment).matches() || WILDCARD_PROPERTY_FILTER_PATTERN.matcher(pathFragment).matches() ){
return new PropertyFilter(pathFragment);
}
else if(LIST_FILTER.matcher(pathFragment).matches()){
else if(LIST_FILTER_PATTERN.matcher(pathFragment).matches()){
return new ListFilter(pathFragment);
}
else if(TRAVERSE_FILTER.matcher(pathFragment).matches()){
return new TraverseFilter(pathFragment);
else if(TRAVERSE_FILTER_PATTERN.matcher(pathFragment).matches()){
return TRAVERSE_FILTER;
}
return null;

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

@ -22,7 +22,7 @@ public class ListFilter extends JsonPathFilterBase {
private static ScriptEngine SCRIPT_ENGINE = new ScriptEngineManager().getEngineByName("js");
private static final Pattern LIST_INDEX_PATTERN = Pattern.compile("\\[(\\s?\\d+\\s?,?)+\\]");
private static final Pattern LIST_INDEX_PATTERN = Pattern.compile("\\[(\\s?\\d+\\s?,?)+\\]"); //[1] OR [1,2,3]
private static final Pattern LIST_PULL_PATTERN = Pattern.compile("\\[\\s?:(\\d+)\\s?\\]"); //[ :2 ]
private static final Pattern LIST_WILDCARD_PATTERN = Pattern.compile("\\[\\*\\]");
private static final Pattern LIST_TAIL_PATTERN_SHORT = Pattern.compile("\\[\\s*-\\s*(\\d+):\\s*\\]"); // [(@.length - 12)] OR [-13:]

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

@ -12,6 +12,9 @@ import java.util.List;
* Time: 2:32 PM
*/
public class PropertyFilter extends JsonPathFilterBase {
private final static String WILDCARD = "*";
private final String pathFragment;
public PropertyFilter(String pathFragment) {
@ -20,22 +23,24 @@ public class PropertyFilter extends JsonPathFilterBase {
@Override
public List<Object> apply(List<Object> filter) {
List<Object> result = new JSONArray();
for (Object current : filter) {
if ("*".equals(pathFragment)) {
List<Object> result = new JSONArray();
if (WILDCARD.equals(pathFragment)) {
for (Object current : filter) {
for (Object value : PathUtil.toDocument(current).values()) {
result.add(value);
result.add(value);
}
} else {
}
}
else {
for (Object current : filter) {
if (PathUtil.toDocument(current).containsKey(pathFragment)) {
result.add(PathUtil.toDocument(current).get(pathFragment));
}
}
}
return result;
}
}

5
json-path/src/main/java/com/jayway/jsonpath/filter/TraverseFilter.java

@ -12,11 +12,6 @@ import java.util.List;
* Time: 2:33 PM
*/
public class TraverseFilter extends JsonPathFilterBase {
private final String pathFragment;
public TraverseFilter(String pathFragment) {
this.pathFragment = pathFragment;
}
@Override
public List<Object> apply(List<Object> filter) {

Loading…
Cancel
Save