Browse Source

refactor: Implementation smells refactored

pull/1008/head
Vaibhav Ramchandani 2 months ago
parent
commit
739699deb3
  1. 5
      json-path/src/main/java/com/jayway/jsonpath/internal/Utils.java
  2. 32
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java
  3. 66
      json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java

5
json-path/src/main/java/com/jayway/jsonpath/internal/Utils.java

@ -175,14 +175,15 @@ public final class Utils {
}
int len = str.length();
StringWriter writer = new StringWriter(len);
StringBuilder unicode = new StringBuilder(4);
final int UNICODE_LENGTH = 4;
StringBuilder unicode = new StringBuilder(UNICODE_LENGTH);
boolean hadSlash = false;
boolean inUnicode = false;
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (inUnicode) {
unicode.append(ch);
if (unicode.length() == 4) {
if (unicode.length() == UNICODE_LENGTH) {
try {
int value = Integer.parseInt(unicode.toString(), 16);
writer.write((char) value);

32
json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java

@ -357,17 +357,8 @@ public class FilterCompiler {
filter.incrementPosition(1); //skip $ and @
while (filter.inBounds()) {
if (filter.currentChar() == OPEN_SQUARE_BRACKET) {
int closingSquareBracketIndex = filter.indexOfMatchingCloseChar(filter.position(), OPEN_SQUARE_BRACKET, CLOSE_SQUARE_BRACKET, true, false);
if (closingSquareBracketIndex == -1) {
throw new InvalidPathException("Square brackets does not match in filter " + filter);
} else {
filter.setPosition(closingSquareBracketIndex + 1);
}
}
boolean closingFunctionBracket = (filter.currentChar() == CLOSE_PARENTHESIS && currentCharIsClosingFunctionBracket(begin));
boolean closingLogicalBracket = (filter.currentChar() == CLOSE_PARENTHESIS && !closingFunctionBracket);
if (!filter.inBounds() || isRelationalOperatorChar(filter.currentChar()) || filter.currentChar() == SPACE || closingLogicalBracket) {
handleSquareBracket();
} else if (shouldBreakParsing()) {
break;
} else {
filter.incrementPosition(1);
@ -379,6 +370,25 @@ public class FilterCompiler {
return ValueNode.createPathNode(path, false, shouldExists);
}
private void handleSquareBracket() {
int closingSquareBracketIndex = filter.indexOfMatchingCloseChar(filter.position(), OPEN_SQUARE_BRACKET, CLOSE_SQUARE_BRACKET, true, false);
if (closingSquareBracketIndex == -1) {
throw new InvalidPathException("Square brackets do not match in filter " + filter);
} else {
filter.setPosition(closingSquareBracketIndex + 1);
}
}
private boolean shouldBreakParsing() {
return !filter.inBounds() || isRelationalOperatorChar(filter.currentChar()) || filter.currentChar() == SPACE || closingLogicalBracket();
}
private boolean closingLogicalBracket() {
boolean closingFunctionBracket = (filter.currentChar() == CLOSE_PARENTHESIS && currentCharIsClosingFunctionBracket(filter.position()));
return (filter.currentChar() == CLOSE_PARENTHESIS && !closingFunctionBracket);
}
private boolean expressionIsTerminated(){
char c = filter.currentChar();
if(c == CLOSE_PARENTHESIS || isLogicalOperatorChar(c)){

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

@ -519,12 +519,11 @@ public class PathCompiler {
// [1], [1,2, n], [1:], [1:2], [:2]
//
private boolean readArrayToken(PathTokenAppender appender) {
if (!path.currentCharIs(OPEN_SQUARE_BRACKET)) {
if (!isOpeningSquareBracket()) {
return false;
}
char nextSignificantChar = path.nextSignificantChar();
if (!isDigit(nextSignificantChar) && nextSignificantChar != MINUS && nextSignificantChar != SPLIT) {
char nextSignificantChar = getNextSignificantChar();
if (!isValidNextChar(nextSignificantChar)) {
return false;
}
@ -535,28 +534,18 @@ public class PathCompiler {
return false;
}
String expression = path.subSequence(expressionBeginIndex, expressionEndIndex).toString().trim();
String expression = extractExpression(expressionBeginIndex, expressionEndIndex);
if ("*".equals(expression)) {
if (!isValidExpression(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 && c != SPACE) {
return false;
}
}
boolean isSliceOperation = expression.contains(":");
if (isSliceOperation) {
ArraySliceOperation arraySliceOperation = ArraySliceOperation.parse(expression);
appender.appendPathToken(PathTokenFactory.createSliceArrayPathToken(arraySliceOperation));
processSliceOperation(expression, appender);
} else {
ArrayIndexOperation arrayIndexOperation = ArrayIndexOperation.parse(expression);
appender.appendPathToken(PathTokenFactory.createIndexArrayPathToken(arrayIndexOperation));
processIndexOperation(expression, appender);
}
path.setPosition(expressionEndIndex + 1);
@ -564,6 +553,47 @@ public class PathCompiler {
return path.currentIsTail() || readNextToken(appender);
}
private boolean isOpeningSquareBracket() {
return path.currentCharIs(OPEN_SQUARE_BRACKET);
}
private char getNextSignificantChar() {
return path.nextSignificantChar();
}
private boolean isValidNextChar(char nextChar) {
return isDigit(nextChar) || nextChar == MINUS || nextChar == SPLIT;
}
private String extractExpression(int beginIndex, int endIndex) {
return path.subSequence(beginIndex, endIndex).toString().trim();
}
private boolean isValidExpression(String expression) {
return !"*".equals(expression) && containsValidChars(expression);
}
private boolean containsValidChars(String expression) {
for (int i = 0; i < expression.length(); i++) {
char c = expression.charAt(i);
if (!isDigit(c) && c != COMMA && c != MINUS && c != SPLIT && c != SPACE) {
return false;
}
}
return true;
}
private void processSliceOperation(String expression, PathTokenAppender appender) {
ArraySliceOperation arraySliceOperation = ArraySliceOperation.parse(expression);
appender.appendPathToken(PathTokenFactory.createSliceArrayPathToken(arraySliceOperation));
}
private void processIndexOperation(String expression, PathTokenAppender appender) {
ArrayIndexOperation arrayIndexOperation = ArrayIndexOperation.parse(expression);
appender.appendPathToken(PathTokenFactory.createIndexArrayPathToken(arrayIndexOperation));
}
//
// ['foo']
//

Loading…
Cancel
Save