JSONPointer.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package com.usky.xml;
  2. import java.io.UnsupportedEncodingException;
  3. import java.net.URLDecoder;
  4. import java.net.URLEncoder;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. import java.util.List;
  8. import static java.lang.String.format;
  9. /*
  10. Copyright (c) 2002 JSON.org
  11. Permission is hereby granted, free of charge, to any person obtaining a copy
  12. of this software and associated documentation files (the "Software"), to deal
  13. in the Software without restriction, including without limitation the rights
  14. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. copies of the Software, and to permit persons to whom the Software is
  16. furnished to do so, subject to the following conditions:
  17. The above copyright notice and this permission notice shall be included in all
  18. copies or substantial portions of the Software.
  19. The Software shall be used for Good, not Evil.
  20. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26. SOFTWARE.
  27. */
  28. /**
  29. * A JSON Pointer is a simple query language defined for JSON documents by
  30. * <a href="https://tools.ietf.org/html/rfc6901">RFC 6901</a>.
  31. *
  32. * In a nutshell, JSONPointer allows the user to navigate into a JSON document
  33. * using strings, and retrieve targeted objects, like a simple form of XPATH.
  34. * Path segments are separated by the '/' char, which signifies the root of
  35. * the document when it appears as the first char of the string. Array
  36. * elements are navigated using ordinals, counting from 0. JSONPointer strings
  37. * may be extended to any arbitrary number of segments. If the navigation
  38. * is successful, the matched item is returned. A matched item may be a
  39. * JSONObject, a JSONArray, or a JSON value. If the JSONPointer string building
  40. * fails, an appropriate exception is thrown. If the navigation fails to find
  41. * a match, a JSONPointerException is thrown.
  42. *
  43. * @author JSON.org
  44. * @version 2016-05-14
  45. */
  46. public class JSONPointer {
  47. // used for URL encoding and decoding
  48. private static final String ENCODING = "utf-8";
  49. /**
  50. * This class allows the user to build a JSONPointer in steps, using
  51. * exactly one segment in each step.
  52. */
  53. public static class Builder {
  54. // Segments for the eventual JSONPointer string
  55. private final List<String> refTokens = new ArrayList<String>();
  56. /**
  57. * Creates a {@code JSONPointer} instance using the tokens previously set using the
  58. * {@link #append(String)} method calls.
  59. * @return a JSONPointer object
  60. */
  61. public JSONPointer build() {
  62. return new JSONPointer(this.refTokens);
  63. }
  64. /**
  65. * Adds an arbitrary token to the list of reference tokens. It can be any non-null value.
  66. *
  67. * Unlike in the case of JSON string or URI fragment representation of JSON pointers, the
  68. * argument of this method MUST NOT be escaped. If you want to query the property called
  69. * {@code "a~b"} then you should simply pass the {@code "a~b"} string as-is, there is no
  70. * need to escape it as {@code "a~0b"}.
  71. *
  72. * @param token the new token to be appended to the list
  73. * @return {@code this}
  74. * @throws NullPointerException if {@code token} is null
  75. */
  76. public Builder append(String token) {
  77. if (token == null) {
  78. throw new NullPointerException("token cannot be null");
  79. }
  80. this.refTokens.add(token);
  81. return this;
  82. }
  83. /**
  84. * Adds an integer to the reference token list. Although not necessarily, mostly this token will
  85. * denote an array index.
  86. *
  87. * @param arrayIndex the array index to be added to the token list
  88. * @return {@code this}
  89. */
  90. public Builder append(int arrayIndex) {
  91. this.refTokens.add(String.valueOf(arrayIndex));
  92. return this;
  93. }
  94. }
  95. /**
  96. * Static factory method for {@link Builder}. Example usage:
  97. *
  98. * <pre><code>
  99. * JSONPointer pointer = JSONPointer.builder()
  100. * .append("obj")
  101. * .append("other~key").append("another/key")
  102. * .append("\"")
  103. * .append(0)
  104. * .build();
  105. * </code></pre>
  106. *
  107. * @return a builder instance which can be used to construct a {@code JSONPointer} instance by chained
  108. * {@link Builder#append(String)} calls.
  109. */
  110. public static Builder builder() {
  111. return new Builder();
  112. }
  113. // Segments for the JSONPointer string
  114. private final List<String> refTokens;
  115. /**
  116. * Pre-parses and initializes a new {@code JSONPointer} instance. If you want to
  117. * evaluate the same JSON Pointer on different JSON documents then it is recommended
  118. * to keep the {@code JSONPointer} instances due to performance considerations.
  119. *
  120. * @param pointer the JSON String or URI Fragment representation of the JSON pointer.
  121. * @throws IllegalArgumentException if {@code pointer} is not a valid JSON pointer
  122. */
  123. public JSONPointer(final String pointer) {
  124. if (pointer == null) {
  125. throw new NullPointerException("pointer cannot be null");
  126. }
  127. if (pointer.isEmpty() || pointer.equals("#")) {
  128. this.refTokens = Collections.emptyList();
  129. return;
  130. }
  131. String refs;
  132. if (pointer.startsWith("#/")) {
  133. refs = pointer.substring(2);
  134. try {
  135. refs = URLDecoder.decode(refs, ENCODING);
  136. } catch (UnsupportedEncodingException e) {
  137. throw new RuntimeException(e);
  138. }
  139. } else if (pointer.startsWith("/")) {
  140. refs = pointer.substring(1);
  141. } else {
  142. throw new IllegalArgumentException("a JSON pointer should start with '/' or '#/'");
  143. }
  144. this.refTokens = new ArrayList<String>();
  145. int slashIdx = -1;
  146. int prevSlashIdx = 0;
  147. do {
  148. prevSlashIdx = slashIdx + 1;
  149. slashIdx = refs.indexOf('/', prevSlashIdx);
  150. if(prevSlashIdx == slashIdx || prevSlashIdx == refs.length()) {
  151. // found 2 slashes in a row ( obj//next )
  152. // or single slash at the end of a string ( obj/test/ )
  153. this.refTokens.add("");
  154. } else if (slashIdx >= 0) {
  155. final String token = refs.substring(prevSlashIdx, slashIdx);
  156. this.refTokens.add(unescape(token));
  157. } else {
  158. // last item after separator, or no separator at all.
  159. final String token = refs.substring(prevSlashIdx);
  160. this.refTokens.add(unescape(token));
  161. }
  162. } while (slashIdx >= 0);
  163. // using split does not take into account consecutive separators or "ending nulls"
  164. //for (String token : refs.split("/")) {
  165. // this.refTokens.add(unescape(token));
  166. //}
  167. }
  168. public JSONPointer(List<String> refTokens) {
  169. this.refTokens = new ArrayList<String>(refTokens);
  170. }
  171. /**
  172. * @see https://tools.ietf.org/html/rfc6901#section-3
  173. */
  174. private static String unescape(String token) {
  175. return token.replace("~1", "/").replace("~0", "~");
  176. }
  177. /**
  178. * Evaluates this JSON Pointer on the given {@code document}. The {@code document}
  179. * is usually a {@link JSONObject} or a {@link JSONArray} instance, but the empty
  180. * JSON Pointer ({@code ""}) can be evaluated on any JSON values and in such case the
  181. * returned value will be {@code document} itself.
  182. *
  183. * @param document the JSON document which should be the subject of querying.
  184. * @return the result of the evaluation
  185. * @throws JSONPointerException if an error occurs during evaluation
  186. */
  187. public Object queryFrom(Object document) throws JSONPointerException {
  188. if (this.refTokens.isEmpty()) {
  189. return document;
  190. }
  191. Object current = document;
  192. for (String token : this.refTokens) {
  193. if (current instanceof JSONObject) {
  194. current = ((JSONObject) current).opt(unescape(token));
  195. } else if (current instanceof JSONArray) {
  196. current = readByIndexToken(current, token);
  197. } else {
  198. throw new JSONPointerException(format(
  199. "value [%s] is not an array or object therefore its key %s cannot be resolved", current,
  200. token));
  201. }
  202. }
  203. return current;
  204. }
  205. /**
  206. * Matches a JSONArray element by ordinal position
  207. * @param current the JSONArray to be evaluated
  208. * @param indexToken the array index in string form
  209. * @return the matched object. If no matching item is found a
  210. * @throws JSONPointerException is thrown if the index is out of bounds
  211. */
  212. private static Object readByIndexToken(Object current, String indexToken) throws JSONPointerException {
  213. try {
  214. int index = Integer.parseInt(indexToken);
  215. JSONArray currentArr = (JSONArray) current;
  216. if (index >= currentArr.length()) {
  217. throw new JSONPointerException(format("index %s is out of bounds - the array has %d elements", indexToken,
  218. Integer.valueOf(currentArr.length())));
  219. }
  220. try {
  221. return currentArr.get(index);
  222. } catch (JSONException e) {
  223. throw new JSONPointerException("Error reading value at index position " + index, e);
  224. }
  225. } catch (NumberFormatException e) {
  226. throw new JSONPointerException(format("%s is not an array index", indexToken), e);
  227. }
  228. }
  229. /**
  230. * Returns a string representing the JSONPointer path value using string
  231. * representation
  232. */
  233. @Override
  234. public String toString() {
  235. StringBuilder rval = new StringBuilder("");
  236. for (String token: this.refTokens) {
  237. rval.append('/').append(escape(token));
  238. }
  239. return rval.toString();
  240. }
  241. /**
  242. * Escapes path segment values to an unambiguous form.
  243. * The escape char to be inserted is '~'. The chars to be escaped
  244. * are ~, which maps to ~0, and /, which maps to ~1.
  245. * @param token the JSONPointer segment value to be escaped
  246. * @return the escaped value for the token
  247. *
  248. * @see https://tools.ietf.org/html/rfc6901#section-3
  249. */
  250. private static String escape(String token) {
  251. return token.replace("~", "~0")
  252. .replace("/", "~1");
  253. }
  254. /**
  255. * Returns a string representing the JSONPointer path value using URI
  256. * fragment identifier representation
  257. * @return a uri fragment string
  258. */
  259. public String toURIFragment() {
  260. try {
  261. StringBuilder rval = new StringBuilder("#");
  262. for (String token : this.refTokens) {
  263. rval.append('/').append(URLEncoder.encode(token, ENCODING));
  264. }
  265. return rval.toString();
  266. } catch (UnsupportedEncodingException e) {
  267. throw new RuntimeException(e);
  268. }
  269. }
  270. }