Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 1 | package com.fasterxml.jackson.core; |
| 2 | |
| 3 | import com.fasterxml.jackson.core.io.NumberInput; |
| 4 | |
| 5 | /** |
| 6 | * Implementation of |
| 7 | * <a href="http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-03">JSON Pointer</a> |
| 8 | * specification. |
| 9 | * Pointer instances can be used to locate logical JSON nodes for things like |
Tatu Saloranta | 911cca0 | 2013-10-08 10:03:44 -0700 | [diff] [blame] | 10 | * tree traversal (see {@link TreeNode#at}). |
| 11 | * It may be used in future for filtering of streaming JSON content |
| 12 | * as well (not implemented yet for 2.3). |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 13 | *<p> |
Tatu Saloranta | f008130 | 2017-03-21 17:07:00 -0700 | [diff] [blame] | 14 | * Instances are fully immutable and can be cached, shared between threads. |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 15 | * |
| 16 | * @author Tatu Saloranta |
Cowtowncoder | d61541d | 2014-12-09 14:16:11 -0800 | [diff] [blame] | 17 | * |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 18 | * @since 2.3 |
| 19 | */ |
| 20 | public class JsonPointer |
| 21 | { |
| 22 | /** |
Dave Jarvis | f35f7d7 | 2016-09-21 22:48:27 -0700 | [diff] [blame] | 23 | * Character used to separate segments. |
Tatu Saloranta | 06f401c | 2016-09-22 10:30:07 -0700 | [diff] [blame] | 24 | * |
| 25 | * @since 2.9 |
Dave Jarvis | f35f7d7 | 2016-09-21 22:48:27 -0700 | [diff] [blame] | 26 | */ |
| 27 | public final static char SEPARATOR = '/'; |
Tatu Saloranta | 06f401c | 2016-09-22 10:30:07 -0700 | [diff] [blame] | 28 | |
Dave Jarvis | f35f7d7 | 2016-09-21 22:48:27 -0700 | [diff] [blame] | 29 | /** |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 30 | * Marker instance used to represent segment that matches current |
Cowtowncoder | 6d8e5e6 | 2014-12-09 13:47:18 -0800 | [diff] [blame] | 31 | * node or position (that is, returns true for |
| 32 | * {@link #matches()}). |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 33 | */ |
| 34 | protected final static JsonPointer EMPTY = new JsonPointer(); |
| 35 | |
| 36 | /** |
| 37 | * Reference to rest of the pointer beyond currently matching |
Cowtowncoder | 6d8e5e6 | 2014-12-09 13:47:18 -0800 | [diff] [blame] | 38 | * segment (if any); null if this pointer refers to the matching |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 39 | * segment. |
| 40 | */ |
| 41 | protected final JsonPointer _nextSegment; |
Alex Soto | 7384d06 | 2014-12-03 11:57:02 +0100 | [diff] [blame] | 42 | |
| 43 | /** |
Cowtowncoder | 6d8e5e6 | 2014-12-09 13:47:18 -0800 | [diff] [blame] | 44 | * Reference from currently matching segment (if any) to node |
Alex Soto | 7384d06 | 2014-12-03 11:57:02 +0100 | [diff] [blame] | 45 | * before leaf. |
Cowtowncoder | d2c6023 | 2014-12-09 14:39:17 -0800 | [diff] [blame] | 46 | * Lazily constructed if/as needed. |
| 47 | *<p> |
| 48 | * NOTE: we'll use `volatile` here assuming that this is unlikely to |
| 49 | * become a performance bottleneck. If it becomes one we can probably |
| 50 | * just drop it and things still should work (despite warnings as per JMM |
| 51 | * regarding visibility (and lack thereof) of unguarded changes). |
Cowtowncoder | d61541d | 2014-12-09 14:16:11 -0800 | [diff] [blame] | 52 | * |
| 53 | * @since 2.5 |
Alex Soto | 7384d06 | 2014-12-03 11:57:02 +0100 | [diff] [blame] | 54 | */ |
Cowtowncoder | d2c6023 | 2014-12-09 14:39:17 -0800 | [diff] [blame] | 55 | protected volatile JsonPointer _head; |
Alex Soto | 7384d06 | 2014-12-03 11:57:02 +0100 | [diff] [blame] | 56 | |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 57 | /** |
| 58 | * We will retain representation of the pointer, as a String, |
| 59 | * so that {@link #toString} should be as efficient as possible. |
| 60 | */ |
| 61 | protected final String _asString; |
| 62 | |
| 63 | protected final String _matchingPropertyName; |
| 64 | |
| 65 | protected final int _matchingElementIndex; |
| 66 | |
| 67 | /* |
| 68 | /********************************************************** |
esanchez | 7c56b00 | 2015-10-27 10:45:25 -0700 | [diff] [blame] | 69 | /* Construction |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 70 | /********************************************************** |
| 71 | */ |
| 72 | |
| 73 | /** |
| 74 | * Constructor used for creating "empty" instance, used to represent |
| 75 | * state that matches current node. |
| 76 | */ |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 77 | protected JsonPointer() { |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 78 | _nextSegment = null; |
| 79 | _matchingPropertyName = ""; |
| 80 | _matchingElementIndex = -1; |
| 81 | _asString = ""; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Constructor used for creating non-empty Segments |
| 86 | */ |
Cowtowncoder | d2c6023 | 2014-12-09 14:39:17 -0800 | [diff] [blame] | 87 | protected JsonPointer(String fullString, String segment, JsonPointer next) { |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 88 | _asString = fullString; |
| 89 | _nextSegment = next; |
| 90 | // Ok; may always be a property |
| 91 | _matchingPropertyName = segment; |
Cowtowncoder | d2c6023 | 2014-12-09 14:39:17 -0800 | [diff] [blame] | 92 | // but could be an index, if parsable |
Tatu Saloranta | 4769c36 | 2014-03-14 13:26:30 -0700 | [diff] [blame] | 93 | _matchingElementIndex = _parseIndex(segment); |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 94 | } |
Cowtowncoder | 1762814 | 2014-12-09 15:03:38 -0800 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * @since 2.5 |
| 98 | */ |
| 99 | protected JsonPointer(String fullString, String segment, int matchIndex, JsonPointer next) { |
| 100 | _asString = fullString; |
| 101 | _nextSegment = next; |
| 102 | _matchingPropertyName = segment; |
| 103 | _matchingElementIndex = matchIndex; |
| 104 | } |
Tatu Saloranta | a12195a | 2013-09-02 21:24:15 -0700 | [diff] [blame] | 105 | |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 106 | /* |
| 107 | /********************************************************** |
| 108 | /* Factory methods |
| 109 | /********************************************************** |
| 110 | */ |
| 111 | |
| 112 | /** |
| 113 | * Factory method that parses given input and construct matching pointer |
| 114 | * instance, if it represents a valid JSON Pointer: if not, a |
| 115 | * {@link IllegalArgumentException} is thrown. |
| 116 | * |
| 117 | * @throws IllegalArgumentException Thrown if the input does not present a valid JSON Pointer |
| 118 | * expression: currently the only such expression is one that does NOT start with |
| 119 | * a slash ('/'). |
| 120 | */ |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 121 | public static JsonPointer compile(String input) throws IllegalArgumentException |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 122 | { |
| 123 | // First quick checks for well-known 'empty' pointer |
| 124 | if ((input == null) || input.length() == 0) { |
| 125 | return EMPTY; |
| 126 | } |
| 127 | // And then quick validity check: |
| 128 | if (input.charAt(0) != '/') { |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 129 | throw new IllegalArgumentException("Invalid input: JSON Pointer expression must start with '/': "+"\""+input+"\""); |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 130 | } |
Cowtowncoder | d2c6023 | 2014-12-09 14:39:17 -0800 | [diff] [blame] | 131 | return _parseTail(input); |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Tatu Saloranta | a12195a | 2013-09-02 21:24:15 -0700 | [diff] [blame] | 134 | /** |
| 135 | * Alias for {@link #compile}; added to make instances automatically |
| 136 | * deserializable by Jackson databind. |
| 137 | */ |
| 138 | public static JsonPointer valueOf(String input) { return compile(input); } |
| 139 | |
Tatu Saloranta | f008130 | 2017-03-21 17:07:00 -0700 | [diff] [blame] | 140 | /** |
| 141 | * Factory method that will construct a pointer instance that describes |
| 142 | * path to location given {@link JsonStreamContext} points to. |
| 143 | * |
| 144 | * @param context Context to build pointer expression fot |
| 145 | * @param includeRoot Whether to include number offset for virtual "root context" |
| 146 | * or not. |
| 147 | * |
| 148 | * @since 2.9 |
| 149 | */ |
| 150 | public static JsonPointer forPath(JsonStreamContext context, |
| 151 | boolean includeRoot) |
| 152 | { |
| 153 | // First things first: last segment may be for START_ARRAY/START_OBJECT, |
| 154 | // in which case it does not yet point to anything, and should be skipped |
| 155 | if (context == null) { |
| 156 | return EMPTY; |
| 157 | } |
| 158 | if (!context.hasPathSegment()) { |
| 159 | // one special case; do not prune root if we need it |
| 160 | if (!(includeRoot && context.inRoot() && context.hasCurrentIndex())) { |
| 161 | context = context.getParent(); |
| 162 | } |
| 163 | } |
| 164 | JsonPointer tail = null; |
| 165 | |
| 166 | for (; context != null; context = context.getParent()) { |
| 167 | if (context.inObject()) { |
| 168 | String seg = context.getCurrentName(); |
| 169 | if (seg == null) { // is this legal? |
| 170 | seg = ""; |
| 171 | } |
| 172 | tail = new JsonPointer(_fullPath(tail, seg), seg, tail); |
| 173 | } else if (context.inArray() || includeRoot) { |
| 174 | int ix = context.getCurrentIndex(); |
| 175 | String ixStr = String.valueOf(ix); |
| 176 | tail = new JsonPointer(_fullPath(tail, ixStr), ixStr, ix, tail); |
| 177 | } |
| 178 | // NOTE: this effectively drops ROOT node(s); should have 1 such node, |
| 179 | // as the last one, but we don't have to care (probably some paths have |
| 180 | // no root, for example) |
| 181 | } |
| 182 | if (tail == null) { |
| 183 | return EMPTY; |
| 184 | } |
| 185 | return tail; |
| 186 | } |
| 187 | |
| 188 | private static String _fullPath(JsonPointer tail, String segment) |
| 189 | { |
| 190 | if (tail == null) { |
| 191 | StringBuilder sb = new StringBuilder(segment.length()+1); |
| 192 | sb.append('/'); |
| 193 | _appendEscaped(sb, segment); |
| 194 | return sb.toString(); |
| 195 | } |
| 196 | String tailDesc = tail._asString; |
| 197 | StringBuilder sb = new StringBuilder(segment.length() + 1 + tailDesc.length()); |
| 198 | sb.append('/'); |
| 199 | _appendEscaped(sb, segment); |
| 200 | sb.append(tailDesc); |
| 201 | return sb.toString(); |
| 202 | } |
| 203 | |
| 204 | private static void _appendEscaped(StringBuilder sb, String segment) |
| 205 | { |
| 206 | for (int i = 0, end = segment.length(); i < end; ++i) { |
| 207 | char c = segment.charAt(i); |
| 208 | if (c == '/') { |
| 209 | sb.append("~1"); |
| 210 | continue; |
| 211 | } |
| 212 | if (c == '~') { |
| 213 | sb.append("~0"); |
| 214 | continue; |
| 215 | } |
| 216 | sb.append(c); |
| 217 | } |
| 218 | } |
| 219 | |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 220 | /* Factory method that composes a pointer instance, given a set |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 221 | * of 'raw' segments: raw meaning that no processing will be done, |
| 222 | * no escaping may is present. |
| 223 | * |
| 224 | * @param segments |
| 225 | * |
| 226 | * @return Constructed path instance |
| 227 | */ |
| 228 | /* TODO! |
| 229 | public static JsonPointer fromSegment(String... segments) |
| 230 | { |
| 231 | if (segments.length == 0) { |
| 232 | return EMPTY; |
| 233 | } |
| 234 | JsonPointer prev = null; |
| 235 | |
| 236 | for (String segment : segments) { |
| 237 | JsonPointer next = new JsonPointer() |
| 238 | } |
| 239 | } |
| 240 | */ |
| 241 | |
| 242 | /* |
| 243 | /********************************************************** |
| 244 | /* Public API |
| 245 | /********************************************************** |
| 246 | */ |
| 247 | |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 248 | public boolean matches() { return _nextSegment == null; } |
| 249 | public String getMatchingProperty() { return _matchingPropertyName; } |
| 250 | public int getMatchingIndex() { return _matchingElementIndex; } |
| 251 | public boolean mayMatchProperty() { return _matchingPropertyName != null; } |
| 252 | public boolean mayMatchElement() { return _matchingElementIndex >= 0; } |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 253 | |
Tatu Saloranta | e220734 | 2014-12-07 10:11:52 -0800 | [diff] [blame] | 254 | /** |
Cowtowncoder | d61541d | 2014-12-09 14:16:11 -0800 | [diff] [blame] | 255 | * Returns the leaf of current JSON Pointer expression. |
| 256 | * Leaf is the last non-null segment of current JSON Pointer. |
| 257 | * |
| 258 | * @since 2.5 |
Alex Soto | fca3ed3 | 2014-12-09 09:53:19 +0100 | [diff] [blame] | 259 | */ |
| 260 | public JsonPointer last() { |
| 261 | JsonPointer current = this; |
Cowtowncoder | 1762814 | 2014-12-09 15:03:38 -0800 | [diff] [blame] | 262 | if (current == EMPTY) { |
| 263 | return null; |
| 264 | } |
| 265 | JsonPointer next; |
| 266 | while ((next = current._nextSegment) != JsonPointer.EMPTY) { |
Cowtowncoder | d61541d | 2014-12-09 14:16:11 -0800 | [diff] [blame] | 267 | current = next; |
Alex Soto | fca3ed3 | 2014-12-09 09:53:19 +0100 | [diff] [blame] | 268 | } |
| 269 | return current; |
| 270 | } |
| 271 | |
Tatu Saloranta | f008130 | 2017-03-21 17:07:00 -0700 | [diff] [blame] | 272 | /** |
| 273 | * Mutant factory method that will return |
| 274 | *<ul> |
| 275 | * <li>`tail` if `this` instance is "empty" pointer, OR |
| 276 | * </li> |
| 277 | * <li>`this` instance if `tail` is "empty" pointer, OR |
| 278 | * </li> |
| 279 | * <li>Newly constructed {@link JsonPointer} instance that starts with all segments |
| 280 | * of `this`, followed by all segments of `tail`. |
| 281 | * </li> |
| 282 | *</ul> |
| 283 | * |
| 284 | * @param tail {@link JsonPointer} instance to append to this one, to create a new pointer instance |
| 285 | * |
| 286 | * @return Either `this` instance, `tail`, or a newly created combination, as per description above. |
| 287 | */ |
Cowtowncoder | 1762814 | 2014-12-09 15:03:38 -0800 | [diff] [blame] | 288 | public JsonPointer append(JsonPointer tail) { |
| 289 | if (this == EMPTY) { |
| 290 | return tail; |
| 291 | } |
| 292 | if (tail == EMPTY) { |
| 293 | return this; |
| 294 | } |
Tatu Saloranta | f008130 | 2017-03-21 17:07:00 -0700 | [diff] [blame] | 295 | // 21-Mar-2017, tatu: Not superbly efficient; could probably improve by not concatenating, |
| 296 | // re-decoding -- by stitching together segments -- but for now should be fine. |
| 297 | |
Alex Soto | e1dffcc | 2014-12-09 12:53:55 +0100 | [diff] [blame] | 298 | String currentJsonPointer = _asString; |
Cowtowncoder | 1762814 | 2014-12-09 15:03:38 -0800 | [diff] [blame] | 299 | if (currentJsonPointer.endsWith("/")) { |
Alex Soto | e1dffcc | 2014-12-09 12:53:55 +0100 | [diff] [blame] | 300 | //removes final slash |
| 301 | currentJsonPointer = currentJsonPointer.substring(0, currentJsonPointer.length()-1); |
| 302 | } |
Cowtowncoder | 1762814 | 2014-12-09 15:03:38 -0800 | [diff] [blame] | 303 | return compile(currentJsonPointer + tail._asString); |
Alex Soto | e1dffcc | 2014-12-09 12:53:55 +0100 | [diff] [blame] | 304 | } |
| 305 | |
Alex Soto | fca3ed3 | 2014-12-09 09:53:19 +0100 | [diff] [blame] | 306 | /** |
Tatu Saloranta | e220734 | 2014-12-07 10:11:52 -0800 | [diff] [blame] | 307 | * Method that may be called to see if the pointer would match property |
| 308 | * (of a JSON Object) with given name. |
| 309 | * |
| 310 | * @since 2.5 |
| 311 | */ |
| 312 | public boolean matchesProperty(String name) { |
| 313 | return (_nextSegment != null) && _matchingPropertyName.equals(name); |
| 314 | } |
| 315 | |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 316 | public JsonPointer matchProperty(String name) { |
Cowtowncoder | b4b7a7e | 2015-04-09 17:43:06 -0700 | [diff] [blame] | 317 | if ((_nextSegment != null) && _matchingPropertyName.equals(name)) { |
| 318 | return _nextSegment; |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 319 | } |
Cowtowncoder | b4b7a7e | 2015-04-09 17:43:06 -0700 | [diff] [blame] | 320 | return null; |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Tatu Saloranta | e220734 | 2014-12-07 10:11:52 -0800 | [diff] [blame] | 323 | /** |
| 324 | * Method that may be called to see if the pointer would match |
| 325 | * array element (of a JSON Array) with given index. |
| 326 | * |
| 327 | * @since 2.5 |
| 328 | */ |
| 329 | public boolean matchesElement(int index) { |
| 330 | return (index == _matchingElementIndex) && (index >= 0); |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 331 | } |
Tatu Saloranta | 2d1e19e | 2013-09-03 20:40:03 -0700 | [diff] [blame] | 332 | |
| 333 | /** |
Cowtowncoder | b4b7a7e | 2015-04-09 17:43:06 -0700 | [diff] [blame] | 334 | * @since 2.6 |
| 335 | */ |
| 336 | public JsonPointer matchElement(int index) { |
| 337 | if ((index != _matchingElementIndex) || (index < 0)) { |
| 338 | return null; |
| 339 | } |
| 340 | return _nextSegment; |
| 341 | } |
| 342 | |
| 343 | /** |
Tatu Saloranta | 2d1e19e | 2013-09-03 20:40:03 -0700 | [diff] [blame] | 344 | * Accessor for getting a "sub-pointer", instance where current segment |
Tatu Saloranta | e220734 | 2014-12-07 10:11:52 -0800 | [diff] [blame] | 345 | * has been removed and pointer includes rest of segments. |
| 346 | * For matching state, will return null. |
Tatu Saloranta | 2d1e19e | 2013-09-03 20:40:03 -0700 | [diff] [blame] | 347 | */ |
| 348 | public JsonPointer tail() { |
| 349 | return _nextSegment; |
| 350 | } |
Alex Soto | 7384d06 | 2014-12-03 11:57:02 +0100 | [diff] [blame] | 351 | |
| 352 | /** |
Cowtowncoder | d61541d | 2014-12-09 14:16:11 -0800 | [diff] [blame] | 353 | * Accessor for getting a pointer instance that is identical to this |
| 354 | * instance except that the last segment has been dropped. |
| 355 | * For example, for JSON Point "/root/branch/leaf", this method would |
| 356 | * return pointer "/root/branch" (compared to {@link #tail()} that |
| 357 | * would return "/branch/leaf"). |
| 358 | * For leaf |
Tatu Saloranta | e220734 | 2014-12-07 10:11:52 -0800 | [diff] [blame] | 359 | * |
| 360 | * @since 2.5 |
Alex Soto | 7384d06 | 2014-12-03 11:57:02 +0100 | [diff] [blame] | 361 | */ |
| 362 | public JsonPointer head() { |
Cowtowncoder | d2c6023 | 2014-12-09 14:39:17 -0800 | [diff] [blame] | 363 | JsonPointer h = _head; |
| 364 | if (h == null) { |
| 365 | if (this != EMPTY) { |
| 366 | h = _constructHead(); |
| 367 | } |
| 368 | _head = h; |
| 369 | } |
| 370 | return h; |
Alex Soto | 7384d06 | 2014-12-03 11:57:02 +0100 | [diff] [blame] | 371 | } |
| 372 | |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 373 | /* |
| 374 | /********************************************************** |
| 375 | /* Standard method overrides |
| 376 | /********************************************************** |
| 377 | */ |
| 378 | |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 379 | @Override public String toString() { return _asString; } |
| 380 | @Override public int hashCode() { return _asString.hashCode(); } |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 381 | |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 382 | @Override public boolean equals(Object o) { |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 383 | if (o == this) return true; |
| 384 | if (o == null) return false; |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 385 | if (!(o instanceof JsonPointer)) return false; |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 386 | return _asString.equals(((JsonPointer) o)._asString); |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | /********************************************************** |
| 391 | /* Internal methods |
| 392 | /********************************************************** |
| 393 | */ |
| 394 | |
Tatu Saloranta | 4769c36 | 2014-03-14 13:26:30 -0700 | [diff] [blame] | 395 | private final static int _parseIndex(String str) { |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 396 | final int len = str.length(); |
Cowtowncoder | d999514 | 2014-12-30 17:09:51 -0800 | [diff] [blame] | 397 | // [core#133]: beware of super long indexes; assume we never |
Tatu Saloranta | 4769c36 | 2014-03-14 13:26:30 -0700 | [diff] [blame] | 398 | // have arrays over 2 billion entries so ints are fine. |
| 399 | if (len == 0 || len > 10) { |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 400 | return -1; |
| 401 | } |
Cowtowncoder | d999514 | 2014-12-30 17:09:51 -0800 | [diff] [blame] | 402 | // [core#176]: no leading zeroes allowed |
| 403 | char c = str.charAt(0); |
| 404 | if (c <= '0') { |
| 405 | return (len == 1 && c == '0') ? 0 : -1; |
| 406 | } |
| 407 | if (c > '9') { |
| 408 | return -1; |
| 409 | } |
| 410 | for (int i = 1; i < len; ++i) { |
| 411 | c = str.charAt(i); |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 412 | if (c > '9' || c < '0') { |
| 413 | return -1; |
| 414 | } |
| 415 | } |
Tatu Saloranta | 4769c36 | 2014-03-14 13:26:30 -0700 | [diff] [blame] | 416 | if (len == 10) { |
| 417 | long l = NumberInput.parseLong(str); |
| 418 | if (l > Integer.MAX_VALUE) { |
| 419 | return -1; |
| 420 | } |
| 421 | } |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 422 | return NumberInput.parseInt(str); |
| 423 | } |
| 424 | |
Cowtowncoder | d2c6023 | 2014-12-09 14:39:17 -0800 | [diff] [blame] | 425 | protected static JsonPointer _parseTail(String input) { |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 426 | final int end = input.length(); |
| 427 | |
| 428 | // first char is the contextual slash, skip |
Tatu Saloranta | 240c455 | 2013-09-03 21:14:50 -0700 | [diff] [blame] | 429 | for (int i = 1; i < end; ) { |
| 430 | char c = input.charAt(i); |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 431 | if (c == '/') { // common case, got a segment |
Cowtowncoder | 6d8e5e6 | 2014-12-09 13:47:18 -0800 | [diff] [blame] | 432 | return new JsonPointer(input, input.substring(1, i), |
Cowtowncoder | d2c6023 | 2014-12-09 14:39:17 -0800 | [diff] [blame] | 433 | _parseTail(input.substring(i))); |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 434 | } |
Tatu Saloranta | 240c455 | 2013-09-03 21:14:50 -0700 | [diff] [blame] | 435 | ++i; |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 436 | // quoting is different; offline this case |
| 437 | if (c == '~' && i < end) { // possibly, quote |
Cowtowncoder | d2c6023 | 2014-12-09 14:39:17 -0800 | [diff] [blame] | 438 | return _parseQuotedTail(input, i); |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 439 | } |
| 440 | // otherwise, loop on |
| 441 | } |
| 442 | // end of the road, no escapes |
Cowtowncoder | d2c6023 | 2014-12-09 14:39:17 -0800 | [diff] [blame] | 443 | return new JsonPointer(input, input.substring(1), EMPTY); |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Method called to parse tail of pointer path, when a potentially |
| 448 | * escaped character has been seen. |
| 449 | * |
| 450 | * @param input Full input for the tail being parsed |
| 451 | * @param i Offset to character after tilde |
| 452 | */ |
Cowtowncoder | d2c6023 | 2014-12-09 14:39:17 -0800 | [diff] [blame] | 453 | protected static JsonPointer _parseQuotedTail(String input, int i) { |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 454 | final int end = input.length(); |
| 455 | StringBuilder sb = new StringBuilder(Math.max(16, end)); |
| 456 | if (i > 2) { |
| 457 | sb.append(input, 1, i-1); |
| 458 | } |
| 459 | _appendEscape(sb, input.charAt(i++)); |
| 460 | while (i < end) { |
Tatu Saloranta | 240c455 | 2013-09-03 21:14:50 -0700 | [diff] [blame] | 461 | char c = input.charAt(i); |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 462 | if (c == '/') { // end is nigh! |
Cowtowncoder | 6d8e5e6 | 2014-12-09 13:47:18 -0800 | [diff] [blame] | 463 | return new JsonPointer(input, sb.toString(), |
Cowtowncoder | d2c6023 | 2014-12-09 14:39:17 -0800 | [diff] [blame] | 464 | _parseTail(input.substring(i))); |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 465 | } |
Tatu Saloranta | 240c455 | 2013-09-03 21:14:50 -0700 | [diff] [blame] | 466 | ++i; |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 467 | if (c == '~' && i < end) { |
Tatu Saloranta | 240c455 | 2013-09-03 21:14:50 -0700 | [diff] [blame] | 468 | _appendEscape(sb, input.charAt(i++)); |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 469 | continue; |
| 470 | } |
| 471 | sb.append(c); |
| 472 | } |
| 473 | // end of the road, last segment |
Cowtowncoder | d2c6023 | 2014-12-09 14:39:17 -0800 | [diff] [blame] | 474 | return new JsonPointer(input, sb.toString(), EMPTY); |
| 475 | } |
| 476 | |
| 477 | protected JsonPointer _constructHead() |
| 478 | { |
Cowtowncoder | 1762814 | 2014-12-09 15:03:38 -0800 | [diff] [blame] | 479 | // ok; find out who we are to drop |
| 480 | JsonPointer last = last(); |
| 481 | if (last == this) { |
Cowtowncoder | d2c6023 | 2014-12-09 14:39:17 -0800 | [diff] [blame] | 482 | return EMPTY; |
| 483 | } |
Cowtowncoder | 1762814 | 2014-12-09 15:03:38 -0800 | [diff] [blame] | 484 | // and from that, length of suffix to drop |
| 485 | int suffixLength = last._asString.length(); |
| 486 | JsonPointer next = _nextSegment; |
| 487 | return new JsonPointer(_asString.substring(0, _asString.length() - suffixLength), _matchingPropertyName, |
| 488 | _matchingElementIndex, next._constructHead(suffixLength, last)); |
| 489 | } |
| 490 | |
| 491 | protected JsonPointer _constructHead(int suffixLength, JsonPointer last) |
| 492 | { |
| 493 | if (this == last) { |
| 494 | return EMPTY; |
| 495 | } |
| 496 | JsonPointer next = _nextSegment; |
| 497 | String str = _asString; |
| 498 | return new JsonPointer(str.substring(0, str.length() - suffixLength), _matchingPropertyName, |
| 499 | _matchingElementIndex, next._constructHead(suffixLength, last)); |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 502 | private static void _appendEscape(StringBuilder sb, char c) { |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 503 | if (c == '0') { |
| 504 | c = '~'; |
Tatu Saloranta | 240c455 | 2013-09-03 21:14:50 -0700 | [diff] [blame] | 505 | } else if (c == '1') { |
Tatu Saloranta | 11151c5 | 2016-09-30 15:20:14 -0700 | [diff] [blame] | 506 | c = '/'; |
Tatu Saloranta | bb63997 | 2013-08-30 21:45:13 -0700 | [diff] [blame] | 507 | } else { |
| 508 | sb.append('~'); |
| 509 | } |
| 510 | sb.append(c); |
| 511 | } |
| 512 | } |