/** * Returns a new instance of {@code ImmutableJsonFieldSelectorFactory} which constructs a {@link JsonFieldSelector} * based on the given string. * * @param jsonFieldSelectorString the string to create a JSON field selector from. * @param jsonParseOptions the JsonParseOptions to apply when parsing the {@code jsonFieldSelectorString}. * @return a new JSON field selector factory. * @throws NullPointerException if {@code jsonFieldSelectorString} is {@code null}. * @throws JsonFieldSelectorInvalidException if {@code jsonFieldSelectorString} is empty or if {@code * jsonFieldSelectorString} does not contain closing parenthesis ({@code )}) for each opening parenthesis ({@code * (}). */ public static ImmutableJsonFieldSelectorFactory newInstance(final String jsonFieldSelectorString, final JsonParseOptions jsonParseOptions) { requireNonNull(jsonFieldSelectorString, "The JSON field selector string must not be null!"); requireNonNull(jsonParseOptions, "The JSON parse options must not be null!"); final String decodedJsonFieldSelectorString = tryToDecodeString(jsonFieldSelectorString, jsonParseOptions); return new ImmutableJsonFieldSelectorFactory(decodedJsonFieldSelectorString); }
/** * Returns a new JSON field selector based on the parsed string which was given to this class' constructor. * * @return a new JSON field selector instance. * @throws IllegalStateException if the JSON field selector string could not be decoded as UTF-8. */ public JsonFieldSelector newJsonFieldSelector() { final List<String> rawJsonKeys = splitAroundComma(jsonFieldSelectorString); final Set<JsonPointer> jsonPointers = flattenToJsonPointers(rawJsonKeys); return ImmutableJsonFieldSelector.of(jsonPointers, jsonFieldSelectorString); } }
private static Set<JsonPointer> flattenToJsonPointers(final Iterable<String> rawJsonKeys) { final Set<JsonPointer> result = new LinkedHashSet<>(); for (final String rawJsonKey : rawJsonKeys) { if (isJsonSelectorFormat(rawJsonKey)) { result.addAll(flattenToJsonPointers(rawJsonKey)); } else { // slashes are already treated by the constructor of the JSON pointer result.add(JsonFactory.newPointer(rawJsonKey)); } } return result; }
} else { final ImmutableJsonFieldSelectorFactory jsonFieldSelectorFactory = ImmutableJsonFieldSelectorFactory.newInstance(fieldSelectorString, options); result = jsonFieldSelectorFactory.newJsonFieldSelector();
@SuppressWarnings("squid:S1166") private static String tryToDecodeString(final String s, final JsonParseOptions jsonParseOptions) { try { return decode(s, jsonParseOptions); } catch (final Exception e) { throw JsonFieldSelectorInvalidException.newBuilder() // .fieldSelector(s) // .description("Check if the field selector is correctly URL encoded.") // .cause(e) // .build(); } }
private ImmutableJsonFieldSelectorFactory(final String theJsonFieldSelectorString) { jsonFieldSelectorString = theJsonFieldSelectorString; validateJsonFieldSelectorString(); }
private void validateJsonFieldSelectorString() { if (jsonFieldSelectorString.isEmpty()) { throw JsonFieldSelectorInvalidException.newBuilder() // .fieldSelector(jsonFieldSelectorString) // .build(); } if (jsonFieldSelectorString.contains(OPENING_PARENTHESIS)) { final int openedParenthesesCnt = getCountOf(OPENING_PARENTHESIS); final int closedParenthesesCnt = getCountOf(CLOSING_PARENTHESIS); if (openedParenthesesCnt != closedParenthesesCnt) { throw JsonFieldSelectorInvalidException.newBuilder() // .fieldSelector(jsonFieldSelectorString) // .build(); } } }
} else { final ImmutableJsonFieldSelectorFactory jsonFieldSelectorFactory = ImmutableJsonFieldSelectorFactory.newInstance(fieldSelectorString, options); result = jsonFieldSelectorFactory.newJsonFieldSelector();
@SuppressWarnings("squid:S1166") private static String tryToDecodeString(final String s, final JsonParseOptions jsonParseOptions) { try { return decode(s, jsonParseOptions); } catch (final Exception e) { throw JsonFieldSelectorInvalidException.newBuilder() // .fieldSelector(s) // .description("Check if the field selector is correctly URL encoded.") // .cause(e) // .build(); } }
private ImmutableJsonFieldSelectorFactory(final String theJsonFieldSelectorString) { jsonFieldSelectorString = theJsonFieldSelectorString; validateJsonFieldSelectorString(); }
private void validateJsonFieldSelectorString() { if (jsonFieldSelectorString.isEmpty()) { throw JsonFieldSelectorInvalidException.newBuilder() // .fieldSelector(jsonFieldSelectorString) // .build(); } if (jsonFieldSelectorString.contains(OPENING_PARENTHESIS)) { final int openedParenthesesCnt = getCountOf(OPENING_PARENTHESIS); final int closedParenthesesCnt = getCountOf(CLOSING_PARENTHESIS); if (openedParenthesesCnt != closedParenthesesCnt) { throw JsonFieldSelectorInvalidException.newBuilder() // .fieldSelector(jsonFieldSelectorString) // .build(); } } }
/** * Returns a new JSON field selector based on the parsed string which was given to this class' constructor. * * @return a new JSON field selector instance. * @throws IllegalStateException if the JSON field selector string could not be decoded as UTF-8. */ public JsonFieldSelector newJsonFieldSelector() { final List<String> rawJsonKeys = splitAroundComma(jsonFieldSelectorString); final Set<JsonPointer> jsonPointers = flattenToJsonPointers(rawJsonKeys); return ImmutableJsonFieldSelector.of(jsonPointers, jsonFieldSelectorString); } }
private static Set<JsonPointer> flattenToJsonPointers(final Iterable<String> rawJsonKeys) { final Set<JsonPointer> result = new LinkedHashSet<>(); for (final String rawJsonKey : rawJsonKeys) { if (isJsonSelectorFormat(rawJsonKey)) { result.addAll(flattenToJsonPointers(rawJsonKey)); } else { // slashes are already treated by the constructor of the JSON pointer result.add(JsonFactory.newPointer(rawJsonKey)); } } return result; }
/** * Returns a new instance of {@code ImmutableJsonFieldSelectorFactory} which constructs a {@link JsonFieldSelector} * based on the given string. * * @param jsonFieldSelectorString the string to create a JSON field selector from. * @param jsonParseOptions the JsonParseOptions to apply when parsing the {@code jsonFieldSelectorString}. * @return a new JSON field selector factory. * @throws NullPointerException if {@code jsonFieldSelectorString} is {@code null}. * @throws JsonFieldSelectorInvalidException if {@code jsonFieldSelectorString} is empty or if {@code * jsonFieldSelectorString} does not contain closing parenthesis ({@code )}) for each opening parenthesis ({@code * (}). */ public static ImmutableJsonFieldSelectorFactory newInstance(final String jsonFieldSelectorString, final JsonParseOptions jsonParseOptions) { requireNonNull(jsonFieldSelectorString, "The JSON field selector string must not be null!"); requireNonNull(jsonParseOptions, "The JSON parse options must not be null!"); final String decodedJsonFieldSelectorString = tryToDecodeString(jsonFieldSelectorString, jsonParseOptions); return new ImmutableJsonFieldSelectorFactory(decodedJsonFieldSelectorString); }
private static Collection<JsonPointer> flattenToJsonPointers(final String stringWithParentheses) { final int indexOfOpeningParenthesis = stringWithParentheses.indexOf(OPENING_PARENTHESIS); // the string before the first opening parenthesis final String commonRootKey = stringWithParentheses.substring(0, indexOfOpeningParenthesis); final JsonPointer commonRootPointer = JsonFactory.newPointer(commonRootKey); // the string within the opening and closing parenthesis // this string is (recursively) treated like a new field selector string (because technically it is) final String withoutParentheses = stringWithParentheses.substring(indexOfOpeningParenthesis + 1, stringWithParentheses.length() - 1); final Set<JsonPointer> jsonPointers = flattenToJsonPointers(splitAroundComma(withoutParentheses)); return jsonPointers.stream() // .map(commonRootPointer::append) // .collect(Collectors.toCollection(LinkedHashSet::new)); }
private static Collection<JsonPointer> flattenToJsonPointers(final String stringWithParentheses) { final int indexOfOpeningParenthesis = stringWithParentheses.indexOf(OPENING_PARENTHESIS); // the string before the first opening parenthesis final String commonRootKey = stringWithParentheses.substring(0, indexOfOpeningParenthesis); final JsonPointer commonRootPointer = JsonFactory.newPointer(commonRootKey); // the string within the opening and closing parenthesis // this string is (recursively) treated like a new field selector string (because technically it is) final String withoutParentheses = stringWithParentheses.substring(indexOfOpeningParenthesis + 1, stringWithParentheses.length() - 1); final Set<JsonPointer> jsonPointers = flattenToJsonPointers(splitAroundComma(withoutParentheses)); return jsonPointers.stream() // .map(commonRootPointer::append) // .collect(Collectors.toCollection(LinkedHashSet::new)); }