Browse Source

Renamed FilterPathToken to PredicatePathToken

pull/48/head
Kalle Stenflo 10 years ago
parent
commit
01b979be98
  1. 3
      json-path/src/main/java/com/jayway/jsonpath/Criteria.java
  2. 2
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  3. 8
      json-path/src/main/java/com/jayway/jsonpath/internal/PathCompiler.java
  4. 1
      json-path/src/main/java/com/jayway/jsonpath/internal/compiler/PathToken.java
  5. 35
      json-path/src/main/java/com/jayway/jsonpath/internal/compiler/PredicatePathToken.java
  6. 14
      json-path/src/main/java/com/jayway/jsonpath/internal/compiler/ScanPathToken.java
  7. 2
      json-path/src/test/java/com/jayway/jsonpath/old/internal/PredicatePathTokenTest.java

3
json-path/src/main/java/com/jayway/jsonpath/Criteria.java

@ -281,13 +281,10 @@ public class Criteria implements Predicate {
if (CriteriaType.EXISTS == criteriaType) {
boolean exists = ((Boolean) expected);
try {
//path.evaluate(model, configuration.options(Option.THROW_ON_MISSING_PROPERTY)).getValue();
Configuration c = ctx.configuration();
if(c.containsOption(Option.ALWAYS_RETURN_LIST) || c.containsOption(Option.SUPPRESS_EXCEPTIONS)){
c = c.options();
}
path.evaluate(ctx.target(), c).getValue();
return exists;
} catch (PathNotFoundException e) {

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

@ -170,7 +170,6 @@ public class JsonPath {
boolean optAsPathList = configuration.containsOption(Option.AS_PATH_LIST);
boolean optAlwaysReturnList = configuration.containsOption(Option.ALWAYS_RETURN_LIST);
boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);
//boolean optThrowOnMissingProperty = configuration.containsOption(Option.THROW_ON_MISSING_PROPERTY);
try {
if(optAsPathList){
@ -186,7 +185,6 @@ public class JsonPath {
}
}
} catch (RuntimeException e){
//if(optThrowOnMissingProperty || !optSuppressExceptions){
if(!optSuppressExceptions){
throw e;
}

8
json-path/src/main/java/com/jayway/jsonpath/internal/PathCompiler.java

@ -5,7 +5,7 @@ import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.internal.compiler.ArrayPathToken;
import com.jayway.jsonpath.internal.compiler.FilterPathToken;
import com.jayway.jsonpath.internal.compiler.PredicatePathToken;
import com.jayway.jsonpath.internal.compiler.PathToken;
import com.jayway.jsonpath.internal.compiler.PropertyPathToken;
import com.jayway.jsonpath.internal.compiler.RootPathToken;
@ -207,7 +207,7 @@ public class PathCompiler {
else if ("..".equals(pathFragment)) return new ScanPathToken();
else if ("[*]".equals(pathFragment)) return new WildcardPathToken();
else if (".*".equals(pathFragment)) return new WildcardPathToken();
else if ("[?]".equals(pathFragment)) return new FilterPathToken(filterList.poll());
else if ("[?]".equals(pathFragment)) return new PredicatePathToken(filterList.poll());
else if (FILTER_PATTERN.matcher(pathFragment).matches()) {
final int criteriaCount = Utils.countMatches(pathFragment, "?");
@ -215,7 +215,7 @@ public class PathCompiler {
for (int i = 0; i < criteriaCount; i++) {
filters.add(filterList.poll());
}
return new FilterPathToken(filters);
return new PredicatePathToken(filters);
}
this.chars = pathFragment.toCharArray();
@ -332,7 +332,7 @@ public class PathCompiler {
Filter filter2 = Filter.filter(criteria);
return new FilterPathToken(filter2);
return new PredicatePathToken(filter2);
}
private Criteria createCriteria(StringBuilder pathBuffer, StringBuilder operatorBuffer, StringBuilder valueBuffer) {

1
json-path/src/main/java/com/jayway/jsonpath/internal/compiler/PathToken.java

@ -77,6 +77,7 @@ public abstract class PathToken {
return ctx.jsonProvider().getMapValue(model, property);
}
void handleArrayIndex(int index, String currentPath, Object json, EvaluationContextImpl ctx) {
String evalPath = currentPath + "[" + index + "]";
try {

35
json-path/src/main/java/com/jayway/jsonpath/internal/compiler/FilterPathToken.java → json-path/src/main/java/com/jayway/jsonpath/internal/compiler/PredicatePathToken.java

@ -12,7 +12,7 @@ import static java.util.Arrays.asList;
/**
*
*/
public class FilterPathToken extends PathToken {
public class PredicatePathToken extends PathToken {
private static final String[] FRAGMENTS = {
"[?]",
@ -24,27 +24,36 @@ public class FilterPathToken extends PathToken {
private final Collection<Predicate> filters;
public FilterPathToken(Predicate filter) {
public PredicatePathToken(Predicate filter) {
this.filters = asList(filter);
}
public FilterPathToken(Collection<Predicate> filters) {
public PredicatePathToken(Collection<Predicate> filters) {
this.filters = filters;
}
@Override
public void evaluate(String currentPath, Object model, EvaluationContextImpl ctx) {
if (!ctx.jsonProvider().isArray(model)) {
throw new InvalidPathException(format("Filter: %s can only be applied to arrays. Current context is: %s", toString(), model));
}
int idx = 0;
Iterable<Object> objects = ctx.jsonProvider().toIterable(model);
for (Object idxModel : objects) {
if (accept(idxModel, ctx.configuration())) {
handleArrayIndex(idx, currentPath, model, ctx);
if (ctx.jsonProvider().isMap(model)) {
if (accept(model, ctx.configuration())) {
if (isLeaf()) {
ctx.addResult(currentPath, model);
} else {
next().evaluate(currentPath, model, ctx);
}
}
} else if (ctx.jsonProvider().isArray(model)){
int idx = 0;
Iterable<Object> objects = ctx.jsonProvider().toIterable(model);
for (Object idxModel : objects) {
if (accept(idxModel, ctx.configuration())) {
handleArrayIndex(idx, currentPath, model, ctx);
}
idx++;
}
idx++;
} else {
throw new InvalidPathException(format("Filter: %s can not be applied to primitives. Current context is: %s", toString(), model));
}
}

14
json-path/src/main/java/com/jayway/jsonpath/internal/compiler/ScanPathToken.java

@ -25,7 +25,7 @@ public class ScanPathToken extends PathToken {
//Filters has already been evaluated
PathToken next = next();
if (next instanceof FilterPathToken) {
if (next instanceof PredicatePathToken) {
if (next.isLeaf()) {
for (Map.Entry<String, Object> match : predicateMatches.entrySet()) {
ctx.addResult(match.getKey(), match.getValue());
@ -60,7 +60,7 @@ public class ScanPathToken extends PathToken {
for (Object evalModel : models) {
String evalPath = currentPath + "[" + idx + "]";
if (predicate.clazz().equals(FilterPathToken.class)) {
if (predicate.clazz().equals(PredicatePathToken.class)) {
if (predicate.matches(evalModel)) {
predicateMatches.put(evalPath, evalModel);
}
@ -95,7 +95,7 @@ public class ScanPathToken extends PathToken {
return new ArrayPathTokenPredicate(ctx);
} else if (target instanceof WildcardPathToken) {
return new WildcardPathTokenPredicate();
} else if (target instanceof FilterPathToken) {
} else if (target instanceof PredicatePathToken) {
return new FilterPathTokenPredicate(target, ctx);
} else {
return FALSE_PREDICATE;
@ -133,21 +133,21 @@ public class ScanPathToken extends PathToken {
private static final class FilterPathTokenPredicate implements Predicate {
private final EvaluationContextImpl ctx;
private FilterPathToken filterPathToken;
private PredicatePathToken predicatePathToken;
private FilterPathTokenPredicate(PathToken target, EvaluationContextImpl ctx) {
this.ctx = ctx;
filterPathToken = (FilterPathToken) target;
predicatePathToken = (PredicatePathToken) target;
}
@Override
public Class<?> clazz() {
return FilterPathToken.class;
return PredicatePathToken.class;
}
@Override
public boolean matches(Object model) {
return filterPathToken.accept(model, ctx.configuration());
return predicatePathToken.accept(model, ctx.configuration());
}
}

2
json-path/src/test/java/com/jayway/jsonpath/old/internal/FilterPathTokenTest.java → json-path/src/test/java/com/jayway/jsonpath/old/internal/PredicatePathTokenTest.java

@ -11,7 +11,7 @@ import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
public class FilterPathTokenTest {
public class PredicatePathTokenTest {
private static final Object ARRAY = JsonProviderFactory.createProvider().parse(
"[" +
Loading…
Cancel
Save