Skip to content

Commit

Permalink
Filter implementation reworked.
Browse files Browse the repository at this point in the history
  • Loading branch information
kallestenflo committed Nov 11, 2015
1 parent 6bc270a commit 1a72fc0
Show file tree
Hide file tree
Showing 26 changed files with 2,210 additions and 1,208 deletions.
856 changes: 89 additions & 767 deletions json-path/src/main/java/com/jayway/jsonpath/Criteria.java

Large diffs are not rendered by default.

79 changes: 9 additions & 70 deletions json-path/src/main/java/com/jayway/jsonpath/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
*/
package com.jayway.jsonpath;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jayway.jsonpath.internal.filter.FilterCompiler;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Stack;
import java.util.regex.Pattern;

import static java.util.Arrays.asList;

Expand All @@ -30,11 +27,6 @@
*/
public abstract class Filter implements Predicate {

private static final Logger logger = LoggerFactory.getLogger(Filter.class);
private static final Pattern OPERATOR_SPLIT = Pattern.compile("((?<=&&|\\|\\|)|(?=&&|\\|\\|))");
private static final String AND = "&&";
private static final String OR = "||";

/**
* Creates a new Filter based on given criteria
* @param predicate criteria
Expand Down Expand Up @@ -82,7 +74,12 @@ public boolean apply(PredicateContext ctx) {

@Override
public String toString() {
return "[?(" + predicate.toString() + ")]";
String predicateString = predicate.toString();
if(predicateString.startsWith("(")){
return "[?" + predicateString + "]";
} else {
return "[?(" + predicateString + ")]";
}
}
}

Expand Down Expand Up @@ -181,67 +178,9 @@ public String toString() {


public static Filter parse(String filter){
filter = filter.trim();
if(!filter.startsWith("[") || !filter.endsWith("]")){
throw new InvalidPathException("Filter must start with '[' and end with ']'. " + filter);
}
filter = filter.substring(1, filter.length()-1).trim();
if(!filter.startsWith("?")){
throw new InvalidPathException("Filter must start with '[?' and end with ']'. " + filter);
}
filter = filter.substring(1).trim();
if(!filter.startsWith("(") || !filter.endsWith(")")){
throw new InvalidPathException("Filter must start with '[?(' and end with ')]'. " + filter);
}
filter = filter.substring(1, filter.length()-1).trim();

String[] split = OPERATOR_SPLIT.split(filter);
Stack<String> operators = new Stack<String>();
Stack<Criteria> criteria = new Stack<Criteria>();

for (String exp : split) {
exp = exp.trim();
if(AND.equals(exp) || OR.equals(exp)){
operators.push(exp);
}
else {
criteria.push(Criteria.parse(cleanCriteria(exp)));
}
}
Filter root = new SingleFilter(criteria.pop());
while(!operators.isEmpty()) {
String operator = operators.pop();
if (AND.equals(operator)) {
root = root.and(criteria.pop());

} else {
if(criteria.isEmpty()){
throw new InvalidPathException("Invalid operators " + filter);
}
root = root.or(criteria.pop());
}
}
if(!operators.isEmpty() || !criteria.isEmpty()){
throw new InvalidPathException("Invalid operators " + filter);
}

if(logger.isDebugEnabled()) logger.debug("Parsed filter: " + root.toString());
return root;
Predicate f = FilterCompiler.compile(filter);
return new SingleFilter(f);
}

private static String cleanCriteria(String filter){
int begin = 0;
int end = filter.length() -1;

char c = filter.charAt(begin);
while(c == '[' || c == '?' || c == '(' || c == ' '){
c = filter.charAt(++begin);
}

c = filter.charAt(end);
while( c == ')' || c == ' '){
c = filter.charAt(--end);
}
return filter.substring(begin, end+1);
}
}
109 changes: 0 additions & 109 deletions json-path/src/main/java/com/jayway/jsonpath/internal/Cache.java

This file was deleted.

Loading

1 comment on commit 1a72fc0

@zline
Copy link
Contributor

@zline zline commented on 1a72fc0 Dec 4, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congrats, filtering now is stronger than ever!

Please sign in to comment.