Kalle Stenflo
9 years ago
25 changed files with 1487 additions and 749 deletions
@ -1,505 +1,567 @@
|
||||
/* |
||||
* Copyright 2011 the original author or authors. |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package com.jayway.jsonpath.internal; |
||||
|
||||
import com.jayway.jsonpath.Filter; |
||||
import com.jayway.jsonpath.InvalidPathException; |
||||
import com.jayway.jsonpath.Predicate; |
||||
import com.jayway.jsonpath.internal.token.ArrayPathToken; |
||||
import com.jayway.jsonpath.internal.token.PathToken; |
||||
import com.jayway.jsonpath.internal.token.PredicatePathToken; |
||||
import com.jayway.jsonpath.internal.token.PropertyPathToken; |
||||
import com.jayway.jsonpath.internal.token.ArrayIndexOperation; |
||||
import com.jayway.jsonpath.internal.token.ArraySliceOperation; |
||||
import com.jayway.jsonpath.internal.token.PathTokenAppender; |
||||
import com.jayway.jsonpath.internal.token.PathTokenFactory; |
||||
import com.jayway.jsonpath.internal.token.RootPathToken; |
||||
import com.jayway.jsonpath.internal.token.ScanPathToken; |
||||
import com.jayway.jsonpath.internal.token.WildcardPathToken; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collection; |
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
import java.util.regex.Pattern; |
||||
|
||||
import static com.jayway.jsonpath.internal.Utils.notEmpty; |
||||
import static java.lang.Character.isDigit; |
||||
import static java.lang.Math.min; |
||||
import static java.util.Arrays.asList; |
||||
|
||||
public class PathCompiler { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(PathCompiler.class); |
||||
|
||||
private static final String PROPERTY_OPEN = "['"; |
||||
private static final String PROPERTY_CLOSE = "']"; |
||||
private static final char DOCUMENT = '$'; |
||||
private static final char ANY = '*'; |
||||
private static final char DOC_CONTEXT = '$'; |
||||
private static final char EVAL_CONTEXT = '@'; |
||||
private static final char OPEN_SQUARE_BRACKET = '['; |
||||
private static final char CLOSE_SQUARE_BRACKET = ']'; |
||||
private static final char OPEN_BRACKET = '('; |
||||
private static final char CLOSE_BRACKET = ')'; |
||||
private static final char WILDCARD = '*'; |
||||
private static final char PERIOD = '.'; |
||||
private static final char BRACKET_OPEN = '['; |
||||
private static final char BRACKET_CLOSE = ']'; |
||||
private static final char SPACE = ' '; |
||||
private static final Cache cache = new Cache(200); |
||||
private static final char QUESTIONMARK = '?'; |
||||
private static final char COMMA = ','; |
||||
private static final char SPLIT = ':'; |
||||
private static final char MINUS = '-'; |
||||
private static final char ESCAPE = '\\'; |
||||
private static final char TICK = '\''; |
||||
|
||||
private static final Cache cache = new Cache(200); |
||||
|
||||
public static Path compile(final String path, final Predicate... filters) { |
||||
private final LinkedList<Predicate> filterStack; |
||||
private final CharacterIndex path; |
||||
|
||||
notEmpty(path, "Path may not be null empty"); |
||||
try { |
||||
String trimmedPath = path.trim(); |
||||
private PathCompiler(String path, LinkedList<Predicate> filterStack) { |
||||
this.filterStack = filterStack; |
||||
this.path = new CharacterIndex(path); |
||||
} |
||||
|
||||
if (trimmedPath.endsWith("..")) { |
||||
throw new InvalidPathException("A path can not end with a scan."); |
||||
private Path compile() { |
||||
RootPathToken root = readContextToken(); |
||||
return new CompiledPath(root, root.getPathFragment().equals("$")); |
||||
} |
||||
|
||||
LinkedList<Predicate> filterList = new LinkedList<Predicate>(asList(filters)); |
||||
public static Path compile(String path, final Predicate... filters) { |
||||
try { |
||||
path = path.trim(); |
||||
|
||||
if (trimmedPath.charAt(0) != '$' && trimmedPath.charAt(0) != '@') { |
||||
trimmedPath = Utils.concat("$.", trimmedPath); |
||||
if(!path.startsWith("$") && !path.startsWith("@")){ |
||||
path = "$." + path; |
||||
} |
||||
if(path.endsWith("..")){ |
||||
fail("Path must not end wid a scan operation '..'"); |
||||
} |
||||
LinkedList filterStack = new LinkedList<Predicate>(asList(filters)); |
||||
String cacheKey = Utils.concat(path, filterStack.toString()); |
||||
Path p = cache.get(cacheKey); |
||||
if (p == null) { |
||||
p = new PathCompiler(path.trim(), filterStack).compile(); |
||||
cache.put(cacheKey, p); |
||||
} |
||||
return p; |
||||
} catch (Exception e) { |
||||
InvalidPathException ipe; |
||||
if (e instanceof InvalidPathException) { |
||||
ipe = (InvalidPathException) e; |
||||
} else { |
||||
ipe = new InvalidPathException(e); |
||||
} |
||||
throw ipe; |
||||
} |
||||
} |
||||
|
||||
boolean isRootPath = (trimmedPath.charAt(0) == '$'); |
||||
//[$ | @]
|
||||
private RootPathToken readContextToken() { |
||||
|
||||
if (trimmedPath.charAt(0) == '@') { |
||||
trimmedPath = Utils.concat("$", trimmedPath.substring(1)); |
||||
if (!path.currentCharIs(DOC_CONTEXT) && !path.currentCharIs(EVAL_CONTEXT)) { |
||||
throw new InvalidPathException("Path must start with '$' or '@'"); |
||||
} |
||||
|
||||
if (trimmedPath.length() > 1 && |
||||
trimmedPath.charAt(1) != '.' && |
||||
trimmedPath.charAt(1) != '[') { |
||||
throw new InvalidPathException("Invalid path " + trimmedPath); |
||||
} |
||||
RootPathToken pathToken = PathTokenFactory.createRootPathToken(path.currentChar()); |
||||
PathTokenAppender appender = pathToken.getPathTokenAppender(); |
||||
|
||||
String cacheKey = Utils.concat(trimmedPath, Boolean.toString(isRootPath), filterList.toString()); |
||||
Path p = cache.get(cacheKey); |
||||
if (p != null) { |
||||
if (logger.isDebugEnabled()) logger.debug("Using cached path: {}", cacheKey); |
||||
return p; |
||||
if (path.currentIsTail()) { |
||||
return pathToken; |
||||
} |
||||
|
||||
RootPathToken root = null; |
||||
path.incrementPosition(1); |
||||
|
||||
if(path.currentChar() != PERIOD && path.currentChar() != OPEN_SQUARE_BRACKET){ |
||||
fail("Illegal character at position " + path.position + " expected '.' or '["); |
||||
} |
||||
|
||||
int i = 0; |
||||
int positions; |
||||
String fragment = ""; |
||||
readNextToken(appender); |
||||
|
||||
do { |
||||
char current = trimmedPath.charAt(i); |
||||
return pathToken; |
||||
} |
||||
|
||||
switch (current) { |
||||
case SPACE: |
||||
throw new InvalidPathException("Space not allowed in path"); |
||||
case DOCUMENT: |
||||
fragment = "$"; |
||||
i++; |
||||
break; |
||||
case BRACKET_OPEN: |
||||
positions = fastForwardUntilClosed(trimmedPath, i); |
||||
fragment = trimmedPath.substring(i, i + positions); |
||||
i += positions; |
||||
break; |
||||
//
|
||||
//
|
||||
//
|
||||
private boolean readNextToken(PathTokenAppender appender) { |
||||
|
||||
char c = path.currentChar(); |
||||
|
||||
switch (c) { |
||||
case OPEN_SQUARE_BRACKET: |
||||
return readBracketPropertyToken(appender) || |
||||
readArrayToken(appender) || |
||||
readWildCardToken(appender) || |
||||
readFilterToken(appender) || |
||||
readPlaceholderToken(appender) || |
||||
fail("Could not parse bracket statement at position " + path.position); |
||||
case PERIOD: |
||||
i++; |
||||
if ( i < trimmedPath.length() && trimmedPath.charAt(i) == PERIOD) { |
||||
//This is a deep scan
|
||||
fragment = ".."; |
||||
i++; |
||||
} else { |
||||
positions = fastForward(trimmedPath, i); |
||||
if (positions == 0) { |
||||
continue; |
||||
return readDotSeparatorToken(appender) || |
||||
readScanToken(appender) || |
||||
fail("Could not parse token at position " + path.position); |
||||
case WILDCARD: |
||||
return readWildCardToken(appender) || |
||||
fail("Could not parse token at position " + path.position); |
||||
default: |
||||
return readPropertyToken(appender) || |
||||
fail("Could not parse token at position " + path.position); |
||||
} |
||||
} |
||||
|
||||
} else if (positions == 1 && trimmedPath.charAt(i) == '*') { |
||||
fragment = new String("[*]"); |
||||
} else { |
||||
assertValidFieldChars(trimmedPath, i, positions); |
||||
//
|
||||
// .
|
||||
//
|
||||
private boolean readDotSeparatorToken(PathTokenAppender appender) { |
||||
if (!path.currentCharIs('.') || path.nextCharIs('.')) { |
||||
return false; |
||||
} |
||||
if (!path.hasMoreCharacters()) { |
||||
throw new InvalidPathException("Path must not end with a '."); |
||||
} |
||||
// if (path.nextSignificantCharIs('[')) {
|
||||
// throw new InvalidPathException("A bracket may not follow a '.");
|
||||
// }
|
||||
|
||||
fragment = Utils.concat(PROPERTY_OPEN, trimmedPath.substring(i, i + positions), PROPERTY_CLOSE); |
||||
path.incrementPosition(1); |
||||
|
||||
return readNextToken(appender); |
||||
} |
||||
i += positions; |
||||
|
||||
//
|
||||
// fooBar
|
||||
//
|
||||
private boolean readPropertyToken(PathTokenAppender appender) { |
||||
if (path.currentCharIs(OPEN_SQUARE_BRACKET) || path.currentCharIs(WILDCARD) || path.currentCharIs(PERIOD) || path.currentCharIs(SPACE)) { |
||||
return false; |
||||
} |
||||
break; |
||||
case ANY: |
||||
fragment = new String("[*]"); |
||||
i++; |
||||
break; |
||||
default: |
||||
positions = fastForward(trimmedPath, i); |
||||
int startPosition = path.position; |
||||
int readPosition = startPosition; |
||||
int endPosition = 0; |
||||
|
||||
fragment = Utils.concat(PROPERTY_OPEN, trimmedPath.substring(i, i + positions), PROPERTY_CLOSE); |
||||
i += positions; |
||||
while (path.inBounds(readPosition)) { |
||||
char c = path.charAt(readPosition); |
||||
if (c == SPACE) { |
||||
throw new InvalidPathException("Use bracket notion ['my prop'] if your property contains blank characters. position: " + path.position); |
||||
} |
||||
if (c == PERIOD || c == OPEN_SQUARE_BRACKET) { |
||||
endPosition = readPosition; |
||||
break; |
||||
} |
||||
if (root == null) { |
||||
root = (RootPathToken) PathComponentAnalyzer.analyze(fragment, filterList); |
||||
} else { |
||||
root.append(PathComponentAnalyzer.analyze(fragment, filterList)); |
||||
readPosition++; |
||||
} |
||||
if (endPosition == 0) { |
||||
endPosition = path.length(); |
||||
} |
||||
|
||||
} while (i < trimmedPath.length()); |
||||
|
||||
Path pa = new CompiledPath(root, isRootPath); |
||||
path.setPosition(endPosition); |
||||
|
||||
cache.put(cacheKey, pa); |
||||
String property = path.subSequence(startPosition, endPosition).toString(); |
||||
|
||||
return pa; |
||||
appender.appendPathToken(PathTokenFactory.createSinglePropertyPathToken(property)); |
||||
|
||||
} catch (Exception ex){ |
||||
throw new InvalidPathException(ex); |
||||
} |
||||
return path.currentIsTail() || readNextToken(appender); |
||||
} |
||||
|
||||
private static void assertValidFieldChars(String s, int start, int positions) { |
||||
/* |
||||
int i = start; |
||||
while (i < start + positions) { |
||||
char c = s.charAt(i); |
||||
//
|
||||
// [?], [?,?, ..]
|
||||
//
|
||||
private boolean readPlaceholderToken(PathTokenAppender appender) { |
||||
|
||||
if (!Character.isLetterOrDigit(c) && c != '-' && c != '_' && c != '$' && c != '@') { |
||||
throw new InvalidPathException("Invalid field name! Use bracket notation if your filed names does not match pattern: ([a-zA-Z@][a-zA-Z0-9@\\$_\\-]*)$"); |
||||
if (!path.currentCharIs(OPEN_SQUARE_BRACKET)) { |
||||
return false; |
||||
} |
||||
i++; |
||||
int questionmarkIndex = path.indexOfNextSignificantChar(QUESTIONMARK); |
||||
if (questionmarkIndex == -1) { |
||||
return false; |
||||
} |
||||
*/ |
||||
char nextSignificantChar = path.nextSignificantChar(questionmarkIndex); |
||||
if (nextSignificantChar != CLOSE_SQUARE_BRACKET && nextSignificantChar != COMMA) { |
||||
return false; |
||||
} |
||||
|
||||
private static int fastForward(String s, int index) { |
||||
int skipCount = 0; |
||||
while (index < s.length()) { |
||||
char current = s.charAt(index); |
||||
if (current == PERIOD || current == BRACKET_OPEN || current == SPACE) { |
||||
break; |
||||
} |
||||
index++; |
||||
skipCount++; |
||||
int expressionBeginIndex = path.position + 1; |
||||
int expressionEndIndex = path.nextIndexOf(expressionBeginIndex, CLOSE_SQUARE_BRACKET); |
||||
|
||||
if (expressionEndIndex == -1) { |
||||
return false; |
||||
} |
||||
return skipCount; |
||||
|
||||
String expression = path.subSequence(expressionBeginIndex, expressionEndIndex).toString(); |
||||
|
||||
String[] tokens = expression.split(","); |
||||
|
||||
if (filterStack.size() < tokens.length) { |
||||
throw new InvalidPathException("Not enough predicates supplied for filter [" + expression + "] at position " + path.position); |
||||
} |
||||
|
||||
private static int fastForwardUntilClosed(String s, int index) { |
||||
int skipCount = 0; |
||||
int nestedBrackets = 0; |
||||
Collection<Predicate> predicates = new ArrayList<Predicate>(); |
||||
for (String token : tokens) { |
||||
token = token != null ? token.trim() : token; |
||||
if (!"?".equals(token == null ? "" : token)) { |
||||
throw new InvalidPathException("Expected '?' but found " + token); |
||||
} |
||||
predicates.add(filterStack.pop()); |
||||
} |
||||
|
||||
//First char is always '[' no need to check it
|
||||
index++; |
||||
skipCount++; |
||||
appender.appendPathToken(PathTokenFactory.createPredicatePathToken(predicates)); |
||||
|
||||
while (index < s.length()) { |
||||
char current = s.charAt(index); |
||||
path.setPosition(expressionEndIndex + 1); |
||||
|
||||
index++; |
||||
skipCount++; |
||||
return path.currentIsTail() || readNextToken(appender); |
||||
} |
||||
|
||||
if (current == BRACKET_CLOSE && nestedBrackets == 0) { |
||||
break; |
||||
//
|
||||
// [?(...)]
|
||||
//
|
||||
private boolean readFilterToken(PathTokenAppender appender) { |
||||
if (!path.currentCharIs(OPEN_SQUARE_BRACKET) && !path.nextSignificantCharIs(QUESTIONMARK)) { |
||||
return false; |
||||
} |
||||
if (current == BRACKET_OPEN) { |
||||
nestedBrackets++; |
||||
|
||||
int openStatementBracketIndex = path.position; |
||||
int questionMarkIndex = path.indexOfNextSignificantChar(QUESTIONMARK); |
||||
if (questionMarkIndex == -1) { |
||||
return false; |
||||
} |
||||
if (current == BRACKET_CLOSE) { |
||||
nestedBrackets--; |
||||
int openBracketIndex = path.indexOfNextSignificantChar(questionMarkIndex, OPEN_BRACKET); |
||||
if (openBracketIndex == -1) { |
||||
return false; |
||||
} |
||||
int closeBracketIndex = path.indexOfClosingBracket(openBracketIndex + 1, true); |
||||
if (closeBracketIndex == -1) { |
||||
return false; |
||||
} |
||||
return skipCount; |
||||
if (!path.nextSignificantCharIs(closeBracketIndex, CLOSE_SQUARE_BRACKET)) { |
||||
return false; |
||||
} |
||||
int closeStatementBracketIndex = path.indexOfNextSignificantChar(closeBracketIndex, CLOSE_SQUARE_BRACKET); |
||||
|
||||
String criteria = path.subSequence(openStatementBracketIndex, closeStatementBracketIndex + 1).toString(); |
||||
|
||||
//---------------------------------------------
|
||||
//
|
||||
//
|
||||
//
|
||||
//---------------------------------------------
|
||||
static class PathComponentAnalyzer { |
||||
appender.appendPathToken(PathTokenFactory.createPredicatePathToken(Filter.parse(criteria))); |
||||
|
||||
private static final Pattern FILTER_PATTERN = Pattern.compile("^\\[\\s*\\?\\s*[,\\s*\\?]*?\\s*]$"); //[?] or [?, ?, ...]
|
||||
private int i; |
||||
private char current; |
||||
path.setPosition(closeStatementBracketIndex + 1); |
||||
|
||||
private final LinkedList<Predicate> filterList; |
||||
private final String pathFragment; |
||||
return path.currentIsTail() || readNextToken(appender); |
||||
|
||||
PathComponentAnalyzer(String pathFragment, LinkedList<Predicate> filterList) { |
||||
this.pathFragment = pathFragment; |
||||
this.filterList = filterList; |
||||
} |
||||
|
||||
static PathToken analyze(String pathFragment, LinkedList<Predicate> filterList) { |
||||
return new PathComponentAnalyzer(pathFragment, filterList).analyze(); |
||||
} |
||||
|
||||
public PathToken analyze() { |
||||
//
|
||||
// [*]
|
||||
// *
|
||||
//
|
||||
private boolean readWildCardToken(PathTokenAppender appender) { |
||||
|
||||
if ("$".equals(pathFragment)) return new RootPathToken(); |
||||
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 PredicatePathToken(filterList.poll()); |
||||
boolean inBracket = path.currentCharIs(OPEN_SQUARE_BRACKET); |
||||
|
||||
else if (FILTER_PATTERN.matcher(pathFragment).matches()) { |
||||
final int criteriaCount = Utils.countMatches(pathFragment, "?"); |
||||
List<Predicate> filters = new ArrayList<Predicate>(criteriaCount); |
||||
for (int i = 0; i < criteriaCount; i++) { |
||||
filters.add(filterList.poll()); |
||||
if (inBracket && !path.nextSignificantCharIs(WILDCARD)) { |
||||
return false; |
||||
} |
||||
return new PredicatePathToken(filters); |
||||
if (!path.currentCharIs(WILDCARD) && path.isOutOfBounds(path.position + 1)) { |
||||
return false; |
||||
} |
||||
if (inBracket) { |
||||
int wildCardIndex = path.indexOfNextSignificantChar(WILDCARD); |
||||
if (!path.nextSignificantCharIs(wildCardIndex, CLOSE_SQUARE_BRACKET)) { |
||||
throw new InvalidPathException("Expected wildcard token to end with ']' on position " + wildCardIndex + 1); |
||||
} |
||||
int bracketCloseIndex = path.indexOfNextSignificantChar(wildCardIndex, CLOSE_SQUARE_BRACKET); |
||||
path.setPosition(bracketCloseIndex + 1); |
||||
} else { |
||||
path.incrementPosition(1); |
||||
} |
||||
|
||||
this.i = 0; |
||||
do { |
||||
current = pathFragment.charAt(i); |
||||
appender.appendPathToken(PathTokenFactory.createWildCardPathToken()); |
||||
|
||||
switch (current) { |
||||
case '?': |
||||
return analyzeCriteriaSequence4(); |
||||
case '\'': |
||||
return analyzeProperty(); |
||||
default: |
||||
if (Character.isDigit(current) || current == ':' || current == '-' || current == '@') { |
||||
return analyzeArraySequence(); |
||||
return path.currentIsTail() || readNextToken(appender); |
||||
} |
||||
i++; |
||||
break; |
||||
|
||||
//
|
||||
// [1], [1,2, n], [1:], [1:2], [:2]
|
||||
//
|
||||
private boolean readArrayToken(PathTokenAppender appender) { |
||||
|
||||
if (!path.currentCharIs(OPEN_SQUARE_BRACKET)) { |
||||
return false; |
||||
} |
||||
char nextSignificantChar = path.nextSignificantChar(); |
||||
if (!isDigit(nextSignificantChar) && nextSignificantChar != MINUS && nextSignificantChar != SPLIT) { |
||||
return false; |
||||
} |
||||
|
||||
int expressionBeginIndex = path.position + 1; |
||||
int expressionEndIndex = path.nextIndexOf(expressionBeginIndex, CLOSE_SQUARE_BRACKET); |
||||
|
||||
if (expressionEndIndex == -1) { |
||||
return false; |
||||
} |
||||
|
||||
} while (i < pathFragment.length()); |
||||
String expression = path.subSequence(expressionBeginIndex, expressionEndIndex).toString().replace(" ", ""); |
||||
|
||||
throw new InvalidPathException("Could not analyze path component: " + pathFragment); |
||||
if ("*".equals(expression)) { |
||||
return false; |
||||
} |
||||
|
||||
//check valid chars
|
||||
for (int i = 0; i < expression.length(); i++) { |
||||
char c = expression.charAt(i); |
||||
if (!isDigit(c) && c != COMMA && c != MINUS && c != SPLIT) { |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
public PathToken analyzeCriteriaSequence4() { |
||||
int[] bounds = findFilterBounds(); |
||||
i = bounds[1]; |
||||
boolean isSliceOperation = expression.contains(":"); |
||||
|
||||
return new PredicatePathToken(Filter.parse(pathFragment.substring(bounds[0], bounds[1]))); |
||||
if (isSliceOperation) { |
||||
ArraySliceOperation arraySliceOperation = ArraySliceOperation.parse(expression); |
||||
appender.appendPathToken(PathTokenFactory.createSliceArrayPathToken(arraySliceOperation)); |
||||
} else { |
||||
ArrayIndexOperation arrayIndexOperation = ArrayIndexOperation.parse(expression); |
||||
appender.appendPathToken(PathTokenFactory.createIndexArrayPathToken(arrayIndexOperation)); |
||||
} |
||||
|
||||
int[] findFilterBounds(){ |
||||
int end = 0; |
||||
int start = i; |
||||
path.setPosition(expressionEndIndex + 1); |
||||
|
||||
while(pathFragment.charAt(start) != '['){ |
||||
start--; |
||||
return path.currentIsTail() || readNextToken(appender); |
||||
} |
||||
|
||||
int mem = ' '; |
||||
int curr = start; |
||||
boolean inProp = false; |
||||
int openSquareBracket = 0; |
||||
int openBrackets = 0; |
||||
while(end == 0){ |
||||
char c = pathFragment.charAt(curr); |
||||
switch (c){ |
||||
case '(': |
||||
if(!inProp) openBrackets++; |
||||
break; |
||||
case ')': |
||||
if(!inProp) openBrackets--; |
||||
break; |
||||
case '[': |
||||
if(!inProp) openSquareBracket++; |
||||
break; |
||||
case ']': |
||||
if(!inProp){ |
||||
openSquareBracket--; |
||||
if(openBrackets == 0){ |
||||
end = curr + 1; |
||||
//
|
||||
// ['foo']
|
||||
//
|
||||
private boolean readBracketPropertyToken(PathTokenAppender appender) { |
||||
if (!path.currentCharIs(OPEN_SQUARE_BRACKET) || !path.nextSignificantCharIs(TICK)) { |
||||
return false; |
||||
} |
||||
|
||||
List<String> properties = new ArrayList<String>(); |
||||
|
||||
int startPosition = path.position + 1; |
||||
int readPosition = startPosition; |
||||
int endPosition = 0; |
||||
boolean inProperty = false; |
||||
|
||||
while (path.inBounds(readPosition)) { |
||||
char c = path.charAt(readPosition); |
||||
|
||||
if (c == CLOSE_SQUARE_BRACKET) { |
||||
if (inProperty) { |
||||
throw new InvalidPathException("Expected property to be closed at position " + readPosition); |
||||
} |
||||
break; |
||||
case '\'': |
||||
if(mem == '\\') { |
||||
break; |
||||
} else if (c == TICK) { |
||||
if (inProperty) { |
||||
endPosition = readPosition; |
||||
properties.add(path.subSequence(startPosition, endPosition).toString()); |
||||
inProperty = false; |
||||
} else { |
||||
startPosition = readPosition + 1; |
||||
inProperty = true; |
||||
} |
||||
inProp = !inProp; |
||||
break; |
||||
default: |
||||
break; |
||||
} |
||||
mem = c; |
||||
curr++; |
||||
readPosition++; |
||||
} |
||||
if(openBrackets != 0 || openSquareBracket != 0){ |
||||
throw new InvalidPathException("Filter brackets are not balanced"); |
||||
|
||||
int endBracketIndex = path.indexOfNextSignificantChar(endPosition, CLOSE_SQUARE_BRACKET) + 1; |
||||
|
||||
path.setPosition(endBracketIndex); |
||||
|
||||
appender.appendPathToken(PathTokenFactory.createPropertyPathToken(properties)); |
||||
|
||||
return path.currentIsTail() || readNextToken(appender); |
||||
} |
||||
|
||||
//
|
||||
// ..
|
||||
//
|
||||
private boolean readScanToken(PathTokenAppender appender) { |
||||
if (!path.currentCharIs(PERIOD) || !path.nextCharIs(PERIOD)) { |
||||
return false; |
||||
} |
||||
return new int[]{start, end}; |
||||
appender.appendPathToken(PathTokenFactory.crateScanToken()); |
||||
path.incrementPosition(2); |
||||
|
||||
return readNextToken(appender); |
||||
} |
||||
|
||||
|
||||
public static boolean fail(String message) { |
||||
throw new InvalidPathException(message); |
||||
} |
||||
|
||||
|
||||
//"['foo']"
|
||||
private PathToken analyzeProperty() { |
||||
List<String> properties = new ArrayList<String>(); |
||||
StringBuilder buffer = new StringBuilder(); |
||||
private static class CharacterIndex { |
||||
|
||||
boolean propertyIsOpen = false; |
||||
private final CharSequence charSequence; |
||||
private int position; |
||||
|
||||
while (current != ']') { |
||||
switch (current) { |
||||
case '\'': |
||||
if (propertyIsOpen) { |
||||
properties.add(buffer.toString()); |
||||
buffer.setLength(0); |
||||
propertyIsOpen = false; |
||||
} else { |
||||
propertyIsOpen = true; |
||||
private CharacterIndex(CharSequence charSequence) { |
||||
this.charSequence = charSequence; |
||||
this.position = 0; |
||||
} |
||||
break; |
||||
default: |
||||
if (propertyIsOpen) { |
||||
buffer.append(current); |
||||
} |
||||
break; |
||||
|
||||
private int length() { |
||||
return charSequence.length(); |
||||
} |
||||
current = pathFragment.charAt(++i); |
||||
|
||||
private char charAt(int idx) { |
||||
return charSequence.charAt(idx); |
||||
} |
||||
return new PropertyPathToken(properties); |
||||
|
||||
private char currentChar() { |
||||
return charSequence.charAt(position); |
||||
} |
||||
|
||||
private boolean currentCharIs(char c) { |
||||
return (charSequence.charAt(position) == c); |
||||
} |
||||
|
||||
//"[-1:]" sliceFrom
|
||||
//"[:1]" sliceTo
|
||||
//"[0:5]" sliceBetween
|
||||
//"[1]"
|
||||
//"[1,2,3]"
|
||||
//"[(@.length - 1)]"
|
||||
private PathToken analyzeArraySequence() { |
||||
StringBuilder buffer = new StringBuilder(); |
||||
List<Integer> numbers = new ArrayList<Integer>(); |
||||
private boolean nextCharIs(char c) { |
||||
return inBounds(position + 1) && (charSequence.charAt(position + 1) == c); |
||||
} |
||||
|
||||
boolean contextSize = (current == '@'); |
||||
boolean sliceTo = false; |
||||
boolean sliceFrom = false; |
||||
boolean sliceBetween = false; |
||||
boolean indexSequence = false; |
||||
boolean singleIndex = false; |
||||
private int incrementPosition(int charCount) { |
||||
return setPosition(position + charCount); |
||||
} |
||||
|
||||
if (contextSize) { |
||||
private int setPosition(int newPosition) { |
||||
position = min(newPosition, charSequence.length() - 1); |
||||
return position; |
||||
} |
||||
|
||||
current = pathFragment.charAt(++i); |
||||
current = pathFragment.charAt(++i); |
||||
while (current != '-') { |
||||
if (current == ' ' || current == '(' || current == ')') { |
||||
current = pathFragment.charAt(++i); |
||||
continue; |
||||
private int indexOfClosingBracket(int startPosition, boolean skipStrings) { |
||||
int opened = 1; |
||||
int readPosition = startPosition; |
||||
while (inBounds(readPosition)) { |
||||
if (skipStrings) { |
||||
if (charAt(readPosition) == TICK) { |
||||
while (inBounds(readPosition)) { |
||||
readPosition++; |
||||
if (charAt(readPosition) == TICK && charAt(readPosition - 1) != ESCAPE) { |
||||
readPosition++; |
||||
break; |
||||
} |
||||
buffer.append(current); |
||||
current = pathFragment.charAt(++i); |
||||
} |
||||
String function = buffer.toString(); |
||||
buffer.setLength(0); |
||||
if (!function.equals("size") && !function.equals("length")) { |
||||
throw new InvalidPathException("Invalid function: @." + function + ". Supported functions are: [(@.length - n)] and [(@.size() - n)]"); |
||||
} |
||||
while (current != ')') { |
||||
if (current == ' ') { |
||||
current = pathFragment.charAt(++i); |
||||
continue; |
||||
} |
||||
buffer.append(current); |
||||
current = pathFragment.charAt(++i); |
||||
if (charAt(readPosition) == OPEN_BRACKET) { |
||||
opened++; |
||||
} |
||||
if (charAt(readPosition) == CLOSE_BRACKET) { |
||||
opened--; |
||||
if(opened == 0){ |
||||
return readPosition; |
||||
} |
||||
} |
||||
readPosition++; |
||||
} |
||||
return -1; |
||||
} |
||||
|
||||
} else { |
||||
|
||||
|
||||
while (Character.isDigit(current) || current == ',' || current == ' ' || current == ':' || current == '-') { |
||||
public int indexOfNextSignificantChar(char c) { |
||||
return indexOfNextSignificantChar(position, c); |
||||
} |
||||
|
||||
switch (current) { |
||||
case ' ': |
||||
break; |
||||
case ':': |
||||
if (buffer.length() == 0) { |
||||
//this is a tail slice [:12]
|
||||
sliceTo = true; |
||||
current = pathFragment.charAt(++i); |
||||
while (Character.isDigit(current) || current == ' ' || current == '-') { |
||||
if (current != ' ') { |
||||
buffer.append(current); |
||||
} |
||||
current = pathFragment.charAt(++i); |
||||
} |
||||
numbers.add(Integer.parseInt(buffer.toString())); |
||||
buffer.setLength(0); |
||||
public int indexOfNextSignificantChar(int startPosition, char c) { |
||||
int readPosition = startPosition + 1; |
||||
while (!isOutOfBounds(readPosition) && charAt(readPosition) == SPACE) { |
||||
readPosition++; |
||||
} |
||||
if (charAt(readPosition) == c) { |
||||
return readPosition; |
||||
} else { |
||||
//we now this starts with [12:???
|
||||
numbers.add(Integer.parseInt(buffer.toString())); |
||||
buffer.setLength(0); |
||||
current = pathFragment.charAt(++i); |
||||
return -1; |
||||
} |
||||
} |
||||
|
||||
//this is a tail slice [:12]
|
||||
while (Character.isDigit(current) || current == ' ' || current == '-') { |
||||
if (current != ' ') { |
||||
buffer.append(current); |
||||
public int nextIndexOf(int startPosition, char c) { |
||||
int readPosition = startPosition; |
||||
while (!isOutOfBounds(readPosition)) { |
||||
if (charAt(readPosition) == c) { |
||||
return readPosition; |
||||
} |
||||
readPosition++; |
||||
} |
||||
current = pathFragment.charAt(++i); |
||||
return -1; |
||||
} |
||||
|
||||
if (buffer.length() == 0) { |
||||
sliceFrom = true; |
||||
} else { |
||||
sliceBetween = true; |
||||
numbers.add(Integer.parseInt(buffer.toString())); |
||||
buffer.setLength(0); |
||||
public boolean nextSignificantCharIs(int startPosition, char c) { |
||||
int readPosition = startPosition + 1; |
||||
while (!isOutOfBounds(readPosition) && charAt(readPosition) == SPACE) { |
||||
readPosition++; |
||||
} |
||||
return !isOutOfBounds(readPosition) && charAt(readPosition) == c; |
||||
} |
||||
break; |
||||
case ',': |
||||
numbers.add(Integer.parseInt(buffer.toString())); |
||||
buffer.setLength(0); |
||||
indexSequence = true; |
||||
break; |
||||
default: |
||||
buffer.append(current); |
||||
break; |
||||
|
||||
public boolean nextSignificantCharIs(char c) { |
||||
return nextSignificantCharIs(position, c); |
||||
} |
||||
if (current == ']') { |
||||
break; |
||||
|
||||
public char nextSignificantChar() { |
||||
return nextSignificantChar(position); |
||||
} |
||||
current = pathFragment.charAt(++i); |
||||
|
||||
public char nextSignificantChar(int startPosition) { |
||||
int readPosition = startPosition + 1; |
||||
while (!isOutOfBounds(readPosition) && charAt(readPosition) == SPACE) { |
||||
readPosition++; |
||||
} |
||||
if (!isOutOfBounds(readPosition)) { |
||||
return charAt(readPosition); |
||||
} else { |
||||
return ' '; |
||||
} |
||||
if (buffer.length() > 0) { |
||||
numbers.add(Integer.parseInt(buffer.toString())); |
||||
} |
||||
singleIndex = (numbers.size() == 1) && !sliceTo && !sliceFrom && !contextSize; |
||||
|
||||
if (logger.isTraceEnabled()) { |
||||
logger.debug("numbers are : {}", numbers.toString()); |
||||
logger.debug("sequence is singleNumber : {}", singleIndex); |
||||
logger.debug("sequence is numberSequence : {}", indexSequence); |
||||
logger.debug("sequence is sliceFrom : {}", sliceFrom); |
||||
logger.debug("sequence is sliceTo : {}", sliceTo); |
||||
logger.debug("sequence is sliceBetween : {}", sliceBetween); |
||||
logger.debug("sequence is contextFetch : {}", contextSize); |
||||
logger.debug("---------------------------------------------"); |
||||
private boolean currentIsTail() { |
||||
return isOutOfBounds(position + 1); |
||||
} |
||||
ArrayPathToken.Operation operation = null; |
||||
|
||||
if (singleIndex) operation = ArrayPathToken.Operation.SINGLE_INDEX; |
||||
else if (indexSequence) operation = ArrayPathToken.Operation.INDEX_SEQUENCE; |
||||
else if (sliceFrom) operation = ArrayPathToken.Operation.SLICE_FROM; |
||||
else if (sliceTo) operation = ArrayPathToken.Operation.SLICE_TO; |
||||
else if (sliceBetween) operation = ArrayPathToken.Operation.SLICE_BETWEEN; |
||||
else if (contextSize) operation = ArrayPathToken.Operation.CONTEXT_SIZE; |
||||
private boolean hasMoreCharacters() { |
||||
return inBounds(position + 1); |
||||
} |
||||
|
||||
assert operation != null; |
||||
private boolean inBounds(int idx) { |
||||
return (idx >= 0) && (idx < charSequence.length()); |
||||
} |
||||
|
||||
return new ArrayPathToken(numbers, operation); |
||||
private boolean isOutOfBounds(int idx) { |
||||
return !inBounds(idx); |
||||
} |
||||
|
||||
private CharSequence subSequence(int start, int end) { |
||||
return charSequence.subSequence(start, end); |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
|
||||
} |
@ -0,0 +1,63 @@
|
||||
package com.jayway.jsonpath.internal.token; |
||||
|
||||
import com.jayway.jsonpath.InvalidPathException; |
||||
import com.jayway.jsonpath.internal.Utils; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
import static java.lang.Character.isDigit; |
||||
|
||||
public class ArrayIndexOperation { |
||||
|
||||
private final List<Integer> indexes; |
||||
|
||||
private ArrayIndexOperation(List<Integer> indexes) { |
||||
this.indexes = Collections.unmodifiableList(indexes); |
||||
} |
||||
|
||||
public List<Integer> indexes() { |
||||
return indexes; |
||||
} |
||||
|
||||
public boolean isSingleIndexOperation(){ |
||||
return indexes.size() == 1; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
StringBuilder sb = new StringBuilder(); |
||||
sb.append("["); |
||||
sb.append(Utils.join(",", indexes)); |
||||
sb.append("]"); |
||||
|
||||
return sb.toString(); |
||||
} |
||||
|
||||
public static ArrayIndexOperation parse(String operation) { |
||||
//check valid chars
|
||||
for (int i = 0; i < operation.length(); i++) { |
||||
char c = operation.charAt(i); |
||||
if (!isDigit(c) && c != ',') { |
||||
throw new InvalidPathException("Failed to parse ArrayIndexOperation: " + operation); |
||||
} |
||||
} |
||||
String[] tokens = operation.split(","); |
||||
|
||||
List<Integer> tempIndexes = new ArrayList<Integer>(); |
||||
for (String token : tokens) { |
||||
tempIndexes.add(parseInteger(token)); |
||||
} |
||||
|
||||
return new ArrayIndexOperation(tempIndexes); |
||||
} |
||||
|
||||
private static Integer parseInteger(String token) { |
||||
try { |
||||
return Integer.parseInt(token); |
||||
} catch (Exception e){ |
||||
throw new InvalidPathException("Failed to parse token in ArrayIndexOperation: " + token, e); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,86 @@
|
||||
package com.jayway.jsonpath.internal.token; |
||||
|
||||
import com.jayway.jsonpath.InvalidPathException; |
||||
|
||||
import static java.lang.Character.isDigit; |
||||
|
||||
public class ArraySliceOperation { |
||||
|
||||
public enum Operation { |
||||
SLICE_FROM, |
||||
SLICE_TO, |
||||
SLICE_BETWEEN |
||||
} |
||||
|
||||
private final Integer from; |
||||
private final Integer to; |
||||
private final Operation operation; |
||||
|
||||
private ArraySliceOperation(Integer from, Integer to, Operation operation) { |
||||
this.from = from; |
||||
this.to = to; |
||||
this.operation = operation; |
||||
} |
||||
|
||||
public Integer from() { |
||||
return from; |
||||
} |
||||
|
||||
public Integer to() { |
||||
return to; |
||||
} |
||||
|
||||
public Operation operation() { |
||||
return operation; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
StringBuilder sb = new StringBuilder(); |
||||
sb.append("["); |
||||
sb.append(from == null ? "" : from.toString()); |
||||
sb.append(":"); |
||||
sb.append(to == null ? "" : to.toString()); |
||||
sb.append("]"); |
||||
|
||||
return sb.toString(); |
||||
} |
||||
|
||||
public static ArraySliceOperation parse(String operation){ |
||||
//check valid chars
|
||||
for (int i = 0; i < operation.length(); i++) { |
||||
char c = operation.charAt(i); |
||||
if( !isDigit(c) && c != '-' && c != ':'){ |
||||
throw new InvalidPathException("Failed to parse SliceOperation: " + operation); |
||||
} |
||||
} |
||||
String[] tokens = operation.split(":"); |
||||
|
||||
Integer tempFrom = tryRead(tokens, 0); |
||||
Integer tempTo = tryRead(tokens, 1); |
||||
Operation tempOperpation; |
||||
|
||||
if(tempFrom != null && tempTo == null){ |
||||
tempOperpation = Operation.SLICE_FROM; |
||||
} else if(tempFrom != null && tempTo != null){ |
||||
tempOperpation = Operation.SLICE_BETWEEN; |
||||
} else if(tempFrom == null && tempTo != null){ |
||||
tempOperpation = Operation.SLICE_TO; |
||||
} else { |
||||
throw new InvalidPathException("Failed to parse SliceOperation: " + operation); |
||||
} |
||||
|
||||
return new ArraySliceOperation(tempFrom, tempTo, tempOperpation); |
||||
} |
||||
|
||||
private static Integer tryRead(String[] tokens, int idx){ |
||||
if(tokens.length > idx){ |
||||
if(tokens[idx].equals("")){ |
||||
return null; |
||||
} |
||||
return Integer.parseInt(tokens[idx]); |
||||
} else { |
||||
return null; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,5 @@
|
||||
package com.jayway.jsonpath.internal.token; |
||||
|
||||
public interface PathTokenAppender { |
||||
PathTokenAppender appendPathToken(PathToken next); |
||||
} |
@ -0,0 +1,47 @@
|
||||
package com.jayway.jsonpath.internal.token; |
||||
|
||||
import com.jayway.jsonpath.Predicate; |
||||
|
||||
import java.util.Collection; |
||||
import java.util.List; |
||||
|
||||
import static java.util.Collections.singletonList; |
||||
|
||||
public class PathTokenFactory { |
||||
|
||||
public static RootPathToken createRootPathToken(char token) { |
||||
return new RootPathToken(token); |
||||
} |
||||
|
||||
public static PathToken createSinglePropertyPathToken(String property) { |
||||
return new PropertyPathToken(singletonList(property)); |
||||
} |
||||
|
||||
public static PathToken createPropertyPathToken(List<String> properties) { |
||||
return new PropertyPathToken(properties); |
||||
} |
||||
|
||||
public static PathToken createSliceArrayPathToken(final ArraySliceOperation arraySliceOperation) { |
||||
return new ArrayPathToken(arraySliceOperation); |
||||
} |
||||
|
||||
public static PathToken createIndexArrayPathToken(final ArrayIndexOperation arrayIndexOperation) { |
||||
return new ArrayPathToken(arrayIndexOperation); |
||||
} |
||||
|
||||
public static PathToken createWildCardPathToken() { |
||||
return new WildcardPathToken(); |
||||
} |
||||
|
||||
public static PathToken crateScanToken() { |
||||
return new ScanPathToken(); |
||||
} |
||||
|
||||
public static PathToken createPredicatePathToken(Collection<Predicate> predicates) { |
||||
return new PredicatePathToken(predicates); |
||||
} |
||||
|
||||
public static PathToken createPredicatePathToken(Predicate predicate) { |
||||
return new PredicatePathToken(predicate); |
||||
} |
||||
} |
@ -0,0 +1,131 @@
|
||||
package com.jayway.jsonpath; |
||||
|
||||
import org.junit.Ignore; |
||||
import org.junit.Test; |
||||
|
||||
import static com.jayway.jsonpath.internal.PathCompiler.compile; |
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
public class PathCompilerTest { |
||||
|
||||
|
||||
@Ignore("Backward compatibility <= 2.0.0") |
||||
@Test(expected = InvalidPathException.class) |
||||
public void a_path_must_start_with_$_or_at() { |
||||
compile("x"); |
||||
} |
||||
|
||||
@Ignore("Backward compatibility <= 2.0.0") |
||||
@Test(expected = InvalidPathException.class) |
||||
public void a_square_bracket_may_not_follow_a_period() { |
||||
compile("$.["); |
||||
} |
||||
|
||||
@Test(expected = InvalidPathException.class) |
||||
public void a_root_path_must_be_followed_by_period_or_bracket() { |
||||
compile("$X"); |
||||
} |
||||
|
||||
@Test |
||||
public void a_root_path_can_be_compiled() { |
||||
assertThat(compile("$").toString()).isEqualTo("$"); |
||||
assertThat(compile("@").toString()).isEqualTo("@"); |
||||
} |
||||
|
||||
@Test(expected = InvalidPathException.class) |
||||
public void a_path_may_not_end_with_period() { |
||||
assertThat(compile("$.").toString()); |
||||
assertThat(compile("$.prop.").toString()); |
||||
} |
||||
|
||||
@Test(expected = InvalidPathException.class) |
||||
public void a_path_may_not_end_with_scan() { |
||||
assertThat(compile("$..").toString()); |
||||
assertThat(compile("$.prop..").toString()); |
||||
} |
||||
|
||||
@Test |
||||
public void a_property_token_can_be_compiled() { |
||||
assertThat(compile("$.prop").toString()).isEqualTo("$['prop']"); |
||||
assertThat(compile("$.1prop").toString()).isEqualTo("$['1prop']"); |
||||
assertThat(compile("$.@prop").toString()).isEqualTo("$['@prop']"); |
||||
} |
||||
|
||||
@Test |
||||
public void a_bracket_notation_property_token_can_be_compiled() { |
||||
assertThat(compile("$['prop']").toString()).isEqualTo("$['prop']"); |
||||
assertThat(compile("$['1prop']").toString()).isEqualTo("$['1prop']"); |
||||
assertThat(compile("$['@prop']").toString()).isEqualTo("$['@prop']"); |
||||
assertThat(compile("$[ '@prop' ]").toString()).isEqualTo("$['@prop']"); |
||||
} |
||||
|
||||
@Test |
||||
public void a_multi_property_token_can_be_compiled() { |
||||
assertThat(compile("$['prop0', 'prop1']").toString()).isEqualTo("$['prop0','prop1']"); |
||||
assertThat(compile("$[ 'prop0' , 'prop1' ]").toString()).isEqualTo("$['prop0','prop1']"); |
||||
} |
||||
|
||||
@Test |
||||
public void a_property_chain_can_be_compiled() { |
||||
assertThat(compile("$.abc").toString()).isEqualTo("$['abc']"); |
||||
assertThat(compile("$.aaa.bbb").toString()).isEqualTo("$['aaa']['bbb']"); |
||||
assertThat(compile("$.aaa.bbb.ccc").toString()).isEqualTo("$['aaa']['bbb']['ccc']"); |
||||
} |
||||
|
||||
@Test(expected = InvalidPathException.class) |
||||
public void a_property_may_not_contain_blanks() { |
||||
assertThat(compile("$.foo bar").toString()); |
||||
} |
||||
|
||||
@Test |
||||
public void a_wildcard_can_be_compiled() { |
||||
assertThat(compile("$.*").toString()).isEqualTo("$[*]"); |
||||
assertThat(compile("$[*]").toString()).isEqualTo("$[*]"); |
||||
assertThat(compile("$[ * ]").toString()).isEqualTo("$[*]"); |
||||
} |
||||
|
||||
@Test |
||||
public void a_wildcard_can_follow_a_property() { |
||||
assertThat(compile("$.prop[*]").toString()).isEqualTo("$['prop'][*]"); |
||||
assertThat(compile("$['prop'][*]").toString()).isEqualTo("$['prop'][*]"); |
||||
} |
||||
|
||||
@Test |
||||
public void an_array_index_path_can_be_compiled() { |
||||
assertThat(compile("$[1]").toString()).isEqualTo("$[1]"); |
||||
assertThat(compile("$[1,2,3]").toString()).isEqualTo("$[1,2,3]"); |
||||
assertThat(compile("$[ 1 , 2 , 3 ]").toString()).isEqualTo("$[1,2,3]"); |
||||
} |
||||
|
||||
@Test |
||||
public void an_array_slice_path_can_be_compiled() { |
||||
assertThat(compile("$[-1:]").toString()).isEqualTo("$[-1:]"); |
||||
assertThat(compile("$[1:2]").toString()).isEqualTo("$[1:2]"); |
||||
assertThat(compile("$[:2]").toString()).isEqualTo("$[:2]"); |
||||
} |
||||
|
||||
@Test |
||||
public void an_inline_criteria_can_be_parsed() { |
||||
assertThat(compile("$[?(@.foo == 'bar')]").toString()).isEqualTo("$[?]"); |
||||
} |
||||
|
||||
@Test |
||||
public void a_placeholder_criteria_can_be_parsed() { |
||||
|
||||
Predicate p = new Predicate() { |
||||
@Override |
||||
public boolean apply(PredicateContext ctx) { |
||||
return false; |
||||
} |
||||
}; |
||||
assertThat(compile("$[?]", p).toString()).isEqualTo("$[?]"); |
||||
assertThat(compile("$[?,?]", p, p).toString()).isEqualTo("$[?,?]"); |
||||
assertThat(compile("$[?,?,?]", p, p, p).toString()).isEqualTo("$[?,?,?]"); |
||||
} |
||||
|
||||
@Test |
||||
public void a_scan_token_can_be_parsed() { |
||||
assertThat(compile("$..['prop']..[*]").toString()).isEqualTo("$..['prop']..[*]"); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue