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; |
package com.jayway.jsonpath.internal; |
||||||
|
|
||||||
import com.jayway.jsonpath.Filter; |
import com.jayway.jsonpath.Filter; |
||||||
import com.jayway.jsonpath.InvalidPathException; |
import com.jayway.jsonpath.InvalidPathException; |
||||||
import com.jayway.jsonpath.Predicate; |
import com.jayway.jsonpath.Predicate; |
||||||
import com.jayway.jsonpath.internal.token.ArrayPathToken; |
import com.jayway.jsonpath.internal.token.ArrayIndexOperation; |
||||||
import com.jayway.jsonpath.internal.token.PathToken; |
import com.jayway.jsonpath.internal.token.ArraySliceOperation; |
||||||
import com.jayway.jsonpath.internal.token.PredicatePathToken; |
import com.jayway.jsonpath.internal.token.PathTokenAppender; |
||||||
import com.jayway.jsonpath.internal.token.PropertyPathToken; |
import com.jayway.jsonpath.internal.token.PathTokenFactory; |
||||||
import com.jayway.jsonpath.internal.token.RootPathToken; |
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.Logger; |
||||||
import org.slf4j.LoggerFactory; |
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
import java.util.ArrayList; |
import java.util.ArrayList; |
||||||
|
import java.util.Collection; |
||||||
import java.util.LinkedList; |
import java.util.LinkedList; |
||||||
import java.util.List; |
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; |
import static java.util.Arrays.asList; |
||||||
|
|
||||||
public class PathCompiler { |
public class PathCompiler { |
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(PathCompiler.class); |
private static final Logger logger = LoggerFactory.getLogger(PathCompiler.class); |
||||||
|
|
||||||
private static final String PROPERTY_OPEN = "['"; |
private static final char DOC_CONTEXT = '$'; |
||||||
private static final String PROPERTY_CLOSE = "']"; |
private static final char EVAL_CONTEXT = '@'; |
||||||
private static final char DOCUMENT = '$'; |
private static final char OPEN_SQUARE_BRACKET = '['; |
||||||
private static final char ANY = '*'; |
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 PERIOD = '.'; |
||||||
private static final char BRACKET_OPEN = '['; |
|
||||||
private static final char BRACKET_CLOSE = ']'; |
|
||||||
private static final char SPACE = ' '; |
private static final char SPACE = ' '; |
||||||
|
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); |
private static final Cache cache = new Cache(200); |
||||||
|
|
||||||
|
private final LinkedList<Predicate> filterStack; |
||||||
|
private final CharacterIndex path; |
||||||
|
|
||||||
public static Path compile(final String path, final Predicate... filters) { |
private PathCompiler(String path, LinkedList<Predicate> filterStack) { |
||||||
|
this.filterStack = filterStack; |
||||||
|
this.path = new CharacterIndex(path); |
||||||
|
} |
||||||
|
|
||||||
notEmpty(path, "Path may not be null empty"); |
private Path compile() { |
||||||
|
RootPathToken root = readContextToken(); |
||||||
|
return new CompiledPath(root, root.getPathFragment().equals("$")); |
||||||
|
} |
||||||
|
|
||||||
|
public static Path compile(String path, final Predicate... filters) { |
||||||
try { |
try { |
||||||
String trimmedPath = path.trim(); |
path = path.trim(); |
||||||
|
|
||||||
if (trimmedPath.endsWith("..")) { |
if(!path.startsWith("$") && !path.startsWith("@")){ |
||||||
throw new InvalidPathException("A path can not end with a scan."); |
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; |
||||||
|
} |
||||||
} |
} |
||||||
|
|
||||||
LinkedList<Predicate> filterList = new LinkedList<Predicate>(asList(filters)); |
//[$ | @]
|
||||||
|
private RootPathToken readContextToken() { |
||||||
|
|
||||||
if (trimmedPath.charAt(0) != '$' && trimmedPath.charAt(0) != '@') { |
if (!path.currentCharIs(DOC_CONTEXT) && !path.currentCharIs(EVAL_CONTEXT)) { |
||||||
trimmedPath = Utils.concat("$.", trimmedPath); |
throw new InvalidPathException("Path must start with '$' or '@'"); |
||||||
} |
} |
||||||
|
|
||||||
boolean isRootPath = (trimmedPath.charAt(0) == '$'); |
RootPathToken pathToken = PathTokenFactory.createRootPathToken(path.currentChar()); |
||||||
|
PathTokenAppender appender = pathToken.getPathTokenAppender(); |
||||||
|
|
||||||
if (trimmedPath.charAt(0) == '@') { |
if (path.currentIsTail()) { |
||||||
trimmedPath = Utils.concat("$", trimmedPath.substring(1)); |
return pathToken; |
||||||
} |
} |
||||||
|
|
||||||
if (trimmedPath.length() > 1 && |
path.incrementPosition(1); |
||||||
trimmedPath.charAt(1) != '.' && |
|
||||||
trimmedPath.charAt(1) != '[') { |
|
||||||
throw new InvalidPathException("Invalid path " + trimmedPath); |
|
||||||
} |
|
||||||
|
|
||||||
String cacheKey = Utils.concat(trimmedPath, Boolean.toString(isRootPath), filterList.toString()); |
if(path.currentChar() != PERIOD && path.currentChar() != OPEN_SQUARE_BRACKET){ |
||||||
Path p = cache.get(cacheKey); |
fail("Illegal character at position " + path.position + " expected '.' or '["); |
||||||
if (p != null) { |
|
||||||
if (logger.isDebugEnabled()) logger.debug("Using cached path: {}", cacheKey); |
|
||||||
return p; |
|
||||||
} |
} |
||||||
|
|
||||||
RootPathToken root = null; |
readNextToken(appender); |
||||||
|
|
||||||
|
return pathToken; |
||||||
|
} |
||||||
|
|
||||||
int i = 0; |
//
|
||||||
int positions; |
//
|
||||||
String fragment = ""; |
//
|
||||||
|
private boolean readNextToken(PathTokenAppender appender) { |
||||||
|
|
||||||
do { |
char c = path.currentChar(); |
||||||
char current = trimmedPath.charAt(i); |
|
||||||
|
|
||||||
switch (current) { |
switch (c) { |
||||||
case SPACE: |
case OPEN_SQUARE_BRACKET: |
||||||
throw new InvalidPathException("Space not allowed in path"); |
return readBracketPropertyToken(appender) || |
||||||
case DOCUMENT: |
readArrayToken(appender) || |
||||||
fragment = "$"; |
readWildCardToken(appender) || |
||||||
i++; |
readFilterToken(appender) || |
||||||
break; |
readPlaceholderToken(appender) || |
||||||
case BRACKET_OPEN: |
fail("Could not parse bracket statement at position " + path.position); |
||||||
positions = fastForwardUntilClosed(trimmedPath, i); |
|
||||||
fragment = trimmedPath.substring(i, i + positions); |
|
||||||
i += positions; |
|
||||||
break; |
|
||||||
case PERIOD: |
case PERIOD: |
||||||
i++; |
return readDotSeparatorToken(appender) || |
||||||
if ( i < trimmedPath.length() && trimmedPath.charAt(i) == PERIOD) { |
readScanToken(appender) || |
||||||
//This is a deep scan
|
fail("Could not parse token at position " + path.position); |
||||||
fragment = ".."; |
case WILDCARD: |
||||||
i++; |
return readWildCardToken(appender) || |
||||||
} else { |
fail("Could not parse token at position " + path.position); |
||||||
positions = fastForward(trimmedPath, i); |
default: |
||||||
if (positions == 0) { |
return readPropertyToken(appender) || |
||||||
continue; |
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 '.");
|
||||||
|
// }
|
||||||
|
|
||||||
|
path.incrementPosition(1); |
||||||
|
|
||||||
fragment = Utils.concat(PROPERTY_OPEN, trimmedPath.substring(i, i + positions), PROPERTY_CLOSE); |
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; |
int startPosition = path.position; |
||||||
case ANY: |
int readPosition = startPosition; |
||||||
fragment = new String("[*]"); |
int endPosition = 0; |
||||||
i++; |
|
||||||
break; |
|
||||||
default: |
|
||||||
positions = fastForward(trimmedPath, i); |
|
||||||
|
|
||||||
fragment = Utils.concat(PROPERTY_OPEN, trimmedPath.substring(i, i + positions), PROPERTY_CLOSE); |
while (path.inBounds(readPosition)) { |
||||||
i += positions; |
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; |
break; |
||||||
} |
} |
||||||
if (root == null) { |
readPosition++; |
||||||
root = (RootPathToken) PathComponentAnalyzer.analyze(fragment, filterList); |
} |
||||||
} else { |
if (endPosition == 0) { |
||||||
root.append(PathComponentAnalyzer.analyze(fragment, filterList)); |
endPosition = path.length(); |
||||||
} |
} |
||||||
|
|
||||||
} while (i < trimmedPath.length()); |
path.setPosition(endPosition); |
||||||
|
|
||||||
Path pa = new CompiledPath(root, isRootPath); |
|
||||||
|
|
||||||
cache.put(cacheKey, pa); |
String property = path.subSequence(startPosition, endPosition).toString(); |
||||||
|
|
||||||
return pa; |
appender.appendPathToken(PathTokenFactory.createSinglePropertyPathToken(property)); |
||||||
|
|
||||||
} catch (Exception ex){ |
return path.currentIsTail() || readNextToken(appender); |
||||||
throw new InvalidPathException(ex); |
|
||||||
} |
|
||||||
} |
} |
||||||
|
|
||||||
private static void assertValidFieldChars(String s, int start, int positions) { |
//
|
||||||
/* |
// [?], [?,?, ..]
|
||||||
int i = start; |
//
|
||||||
while (i < start + positions) { |
private boolean readPlaceholderToken(PathTokenAppender appender) { |
||||||
char c = s.charAt(i); |
|
||||||
|
|
||||||
if (!Character.isLetterOrDigit(c) && c != '-' && c != '_' && c != '$' && c != '@') { |
if (!path.currentCharIs(OPEN_SQUARE_BRACKET)) { |
||||||
throw new InvalidPathException("Invalid field name! Use bracket notation if your filed names does not match pattern: ([a-zA-Z@][a-zA-Z0-9@\\$_\\-]*)$"); |
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 expressionBeginIndex = path.position + 1; |
||||||
int skipCount = 0; |
int expressionEndIndex = path.nextIndexOf(expressionBeginIndex, CLOSE_SQUARE_BRACKET); |
||||||
while (index < s.length()) { |
|
||||||
char current = s.charAt(index); |
if (expressionEndIndex == -1) { |
||||||
if (current == PERIOD || current == BRACKET_OPEN || current == SPACE) { |
return false; |
||||||
break; |
|
||||||
} |
|
||||||
index++; |
|
||||||
skipCount++; |
|
||||||
} |
} |
||||||
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) { |
Collection<Predicate> predicates = new ArrayList<Predicate>(); |
||||||
int skipCount = 0; |
for (String token : tokens) { |
||||||
int nestedBrackets = 0; |
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
|
appender.appendPathToken(PathTokenFactory.createPredicatePathToken(predicates)); |
||||||
index++; |
|
||||||
skipCount++; |
|
||||||
|
|
||||||
while (index < s.length()) { |
path.setPosition(expressionEndIndex + 1); |
||||||
char current = s.charAt(index); |
|
||||||
|
|
||||||
index++; |
return path.currentIsTail() || readNextToken(appender); |
||||||
skipCount++; |
} |
||||||
|
|
||||||
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) { |
int openBracketIndex = path.indexOfNextSignificantChar(questionMarkIndex, OPEN_BRACKET); |
||||||
nestedBrackets--; |
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(); |
||||||
|
|
||||||
//---------------------------------------------
|
appender.appendPathToken(PathTokenFactory.createPredicatePathToken(Filter.parse(criteria))); |
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//---------------------------------------------
|
|
||||||
static class PathComponentAnalyzer { |
|
||||||
|
|
||||||
private static final Pattern FILTER_PATTERN = Pattern.compile("^\\[\\s*\\?\\s*[,\\s*\\?]*?\\s*]$"); //[?] or [?, ?, ...]
|
path.setPosition(closeStatementBracketIndex + 1); |
||||||
private int i; |
|
||||||
private char current; |
|
||||||
|
|
||||||
private final LinkedList<Predicate> filterList; |
return path.currentIsTail() || readNextToken(appender); |
||||||
private final String pathFragment; |
|
||||||
|
|
||||||
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(); |
boolean inBracket = path.currentCharIs(OPEN_SQUARE_BRACKET); |
||||||
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()); |
|
||||||
|
|
||||||
else if (FILTER_PATTERN.matcher(pathFragment).matches()) { |
if (inBracket && !path.nextSignificantCharIs(WILDCARD)) { |
||||||
final int criteriaCount = Utils.countMatches(pathFragment, "?"); |
return false; |
||||||
List<Predicate> filters = new ArrayList<Predicate>(criteriaCount); |
} |
||||||
for (int i = 0; i < criteriaCount; i++) { |
if (!path.currentCharIs(WILDCARD) && path.isOutOfBounds(path.position + 1)) { |
||||||
filters.add(filterList.poll()); |
return false; |
||||||
} |
} |
||||||
return new PredicatePathToken(filters); |
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; |
appender.appendPathToken(PathTokenFactory.createWildCardPathToken()); |
||||||
do { |
|
||||||
current = pathFragment.charAt(i); |
|
||||||
|
|
||||||
switch (current) { |
return path.currentIsTail() || readNextToken(appender); |
||||||
case '?': |
|
||||||
return analyzeCriteriaSequence4(); |
|
||||||
case '\'': |
|
||||||
return analyzeProperty(); |
|
||||||
default: |
|
||||||
if (Character.isDigit(current) || current == ':' || current == '-' || current == '@') { |
|
||||||
return analyzeArraySequence(); |
|
||||||
} |
} |
||||||
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); |
||||||
|
|
||||||
} while (i < pathFragment.length()); |
if (expressionEndIndex == -1) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
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() { |
boolean isSliceOperation = expression.contains(":"); |
||||||
int[] bounds = findFilterBounds(); |
|
||||||
i = bounds[1]; |
|
||||||
|
|
||||||
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(){ |
path.setPosition(expressionEndIndex + 1); |
||||||
int end = 0; |
|
||||||
int start = i; |
|
||||||
|
|
||||||
while(pathFragment.charAt(start) != '['){ |
return path.currentIsTail() || readNextToken(appender); |
||||||
start--; |
|
||||||
} |
} |
||||||
|
|
||||||
int mem = ' '; |
//
|
||||||
int curr = start; |
// ['foo']
|
||||||
boolean inProp = false; |
//
|
||||||
int openSquareBracket = 0; |
private boolean readBracketPropertyToken(PathTokenAppender appender) { |
||||||
int openBrackets = 0; |
if (!path.currentCharIs(OPEN_SQUARE_BRACKET) || !path.nextSignificantCharIs(TICK)) { |
||||||
while(end == 0){ |
return false; |
||||||
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; |
|
||||||
} |
} |
||||||
|
|
||||||
|
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; |
break; |
||||||
case '\'': |
} else if (c == TICK) { |
||||||
if(mem == '\\') { |
if (inProperty) { |
||||||
break; |
endPosition = readPosition; |
||||||
|
properties.add(path.subSequence(startPosition, endPosition).toString()); |
||||||
|
inProperty = false; |
||||||
|
} else { |
||||||
|
startPosition = readPosition + 1; |
||||||
|
inProperty = true; |
||||||
} |
} |
||||||
inProp = !inProp; |
|
||||||
break; |
|
||||||
default: |
|
||||||
break; |
|
||||||
} |
} |
||||||
mem = c; |
readPosition++; |
||||||
curr++; |
|
||||||
} |
} |
||||||
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 static class CharacterIndex { |
||||||
private PathToken analyzeProperty() { |
|
||||||
List<String> properties = new ArrayList<String>(); |
|
||||||
StringBuilder buffer = new StringBuilder(); |
|
||||||
|
|
||||||
boolean propertyIsOpen = false; |
private final CharSequence charSequence; |
||||||
|
private int position; |
||||||
|
|
||||||
while (current != ']') { |
private CharacterIndex(CharSequence charSequence) { |
||||||
switch (current) { |
this.charSequence = charSequence; |
||||||
case '\'': |
this.position = 0; |
||||||
if (propertyIsOpen) { |
|
||||||
properties.add(buffer.toString()); |
|
||||||
buffer.setLength(0); |
|
||||||
propertyIsOpen = false; |
|
||||||
} else { |
|
||||||
propertyIsOpen = true; |
|
||||||
} |
} |
||||||
break; |
|
||||||
default: |
private int length() { |
||||||
if (propertyIsOpen) { |
return charSequence.length(); |
||||||
buffer.append(current); |
|
||||||
} |
|
||||||
break; |
|
||||||
} |
} |
||||||
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
|
private boolean nextCharIs(char c) { |
||||||
//"[:1]" sliceTo
|
return inBounds(position + 1) && (charSequence.charAt(position + 1) == c); |
||||||
//"[0:5]" sliceBetween
|
} |
||||||
//"[1]"
|
|
||||||
//"[1,2,3]"
|
|
||||||
//"[(@.length - 1)]"
|
|
||||||
private PathToken analyzeArraySequence() { |
|
||||||
StringBuilder buffer = new StringBuilder(); |
|
||||||
List<Integer> numbers = new ArrayList<Integer>(); |
|
||||||
|
|
||||||
boolean contextSize = (current == '@'); |
private int incrementPosition(int charCount) { |
||||||
boolean sliceTo = false; |
return setPosition(position + charCount); |
||||||
boolean sliceFrom = false; |
} |
||||||
boolean sliceBetween = false; |
|
||||||
boolean indexSequence = false; |
|
||||||
boolean singleIndex = false; |
|
||||||
|
|
||||||
if (contextSize) { |
private int setPosition(int newPosition) { |
||||||
|
position = min(newPosition, charSequence.length() - 1); |
||||||
|
return position; |
||||||
|
} |
||||||
|
|
||||||
current = pathFragment.charAt(++i); |
private int indexOfClosingBracket(int startPosition, boolean skipStrings) { |
||||||
current = pathFragment.charAt(++i); |
int opened = 1; |
||||||
while (current != '-') { |
int readPosition = startPosition; |
||||||
if (current == ' ' || current == '(' || current == ')') { |
while (inBounds(readPosition)) { |
||||||
current = pathFragment.charAt(++i); |
if (skipStrings) { |
||||||
continue; |
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); |
if (charAt(readPosition) == OPEN_BRACKET) { |
||||||
current = pathFragment.charAt(++i); |
opened++; |
||||||
|
} |
||||||
|
if (charAt(readPosition) == CLOSE_BRACKET) { |
||||||
|
opened--; |
||||||
|
if(opened == 0){ |
||||||
|
return readPosition; |
||||||
|
} |
||||||
|
} |
||||||
|
readPosition++; |
||||||
|
} |
||||||
|
return -1; |
||||||
} |
} |
||||||
|
|
||||||
} else { |
public int indexOfNextSignificantChar(char c) { |
||||||
|
return indexOfNextSignificantChar(position, c); |
||||||
|
} |
||||||
while (Character.isDigit(current) || current == ',' || current == ' ' || current == ':' || current == '-') { |
|
||||||
|
|
||||||
switch (current) { |
public int indexOfNextSignificantChar(int startPosition, char c) { |
||||||
case ' ': |
int readPosition = startPosition + 1; |
||||||
break; |
while (!isOutOfBounds(readPosition) && charAt(readPosition) == SPACE) { |
||||||
case ':': |
readPosition++; |
||||||
if (buffer.length() == 0) { |
} |
||||||
//this is a tail slice [:12]
|
if (charAt(readPosition) == c) { |
||||||
sliceTo = true; |
return readPosition; |
||||||
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); |
|
||||||
} else { |
} else { |
||||||
//we now this starts with [12:???
|
return -1; |
||||||
numbers.add(Integer.parseInt(buffer.toString())); |
} |
||||||
buffer.setLength(0); |
} |
||||||
current = pathFragment.charAt(++i); |
|
||||||
|
|
||||||
//this is a tail slice [:12]
|
public int nextIndexOf(int startPosition, char c) { |
||||||
while (Character.isDigit(current) || current == ' ' || current == '-') { |
int readPosition = startPosition; |
||||||
if (current != ' ') { |
while (!isOutOfBounds(readPosition)) { |
||||||
buffer.append(current); |
if (charAt(readPosition) == c) { |
||||||
|
return readPosition; |
||||||
|
} |
||||||
|
readPosition++; |
||||||
} |
} |
||||||
current = pathFragment.charAt(++i); |
return -1; |
||||||
} |
} |
||||||
|
|
||||||
if (buffer.length() == 0) { |
public boolean nextSignificantCharIs(int startPosition, char c) { |
||||||
sliceFrom = true; |
int readPosition = startPosition + 1; |
||||||
} else { |
while (!isOutOfBounds(readPosition) && charAt(readPosition) == SPACE) { |
||||||
sliceBetween = true; |
readPosition++; |
||||||
numbers.add(Integer.parseInt(buffer.toString())); |
|
||||||
buffer.setLength(0); |
|
||||||
} |
} |
||||||
|
return !isOutOfBounds(readPosition) && charAt(readPosition) == c; |
||||||
} |
} |
||||||
break; |
|
||||||
case ',': |
public boolean nextSignificantCharIs(char c) { |
||||||
numbers.add(Integer.parseInt(buffer.toString())); |
return nextSignificantCharIs(position, c); |
||||||
buffer.setLength(0); |
|
||||||
indexSequence = true; |
|
||||||
break; |
|
||||||
default: |
|
||||||
buffer.append(current); |
|
||||||
break; |
|
||||||
} |
} |
||||||
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()) { |
private boolean currentIsTail() { |
||||||
logger.debug("numbers are : {}", numbers.toString()); |
return isOutOfBounds(position + 1); |
||||||
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("---------------------------------------------"); |
|
||||||
} |
} |
||||||
ArrayPathToken.Operation operation = null; |
|
||||||
|
|
||||||
if (singleIndex) operation = ArrayPathToken.Operation.SINGLE_INDEX; |
private boolean hasMoreCharacters() { |
||||||
else if (indexSequence) operation = ArrayPathToken.Operation.INDEX_SEQUENCE; |
return inBounds(position + 1); |
||||||
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; |
|
||||||
|
|
||||||
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