Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 1 | package com.fasterxml.jackson.core.filter; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.io.OutputStream; |
| 5 | import java.math.BigDecimal; |
| 6 | import java.math.BigInteger; |
| 7 | |
| 8 | import com.fasterxml.jackson.core.*; |
| 9 | import com.fasterxml.jackson.core.util.JsonParserDelegate; |
| 10 | |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 11 | import static com.fasterxml.jackson.core.JsonTokenId.*; |
| 12 | |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 13 | /** |
| 14 | * Specialized {@link JsonParserDelegate} that allows use of |
| 15 | * {@link TokenFilter} for outputting a subset of content that |
| 16 | * is visible to caller |
| 17 | * |
| 18 | * @since 2.6 |
| 19 | */ |
| 20 | public class FilteringParserDelegate extends JsonParserDelegate |
| 21 | { |
| 22 | /* |
| 23 | /********************************************************** |
| 24 | /* Configuration |
| 25 | /********************************************************** |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * Object consulted to determine whether to write parts of content generator |
| 30 | * is asked to write or not. |
| 31 | */ |
| 32 | protected TokenFilter rootFilter; |
| 33 | |
| 34 | /** |
| 35 | * Flag that determines whether filtering will continue after the first |
| 36 | * match is indicated or not: if `false`, output is based on just the first |
| 37 | * full match (returning {@link TokenFilter#INCLUDE_ALL}) and no more |
| 38 | * checks are made; if `true` then filtering will be applied as necessary |
| 39 | * until end of content. |
| 40 | */ |
| 41 | protected boolean _allowMultipleMatches; |
| 42 | |
| 43 | /** |
| 44 | * Flag that determines whether path leading up to included content should |
| 45 | * also be automatically included or not. If `false`, no path inclusion is |
| 46 | * done and only explicitly included entries are output; if `true` then |
| 47 | * path from main level down to match is also included as necessary. |
| 48 | */ |
| 49 | protected boolean _includePath; |
| 50 | |
| 51 | /* |
| 52 | /********************************************************** |
| 53 | /* State |
| 54 | /********************************************************** |
| 55 | */ |
| 56 | |
| 57 | /** |
| 58 | * Last token retrieved via {@link #nextToken}, if any. |
| 59 | * Null before the first call to <code>nextToken()</code>, |
| 60 | * as well as if token has been explicitly cleared |
| 61 | */ |
| 62 | protected JsonToken _currToken; |
| 63 | |
| 64 | /** |
| 65 | * Last cleared token, if any: that is, value that was in |
| 66 | * effect when {@link #clearCurrentToken} was called. |
| 67 | */ |
| 68 | protected JsonToken _lastClearedToken; |
| 69 | |
| 70 | /** |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 71 | * During traversal this is the actual "open" parse tree, which sometimes |
| 72 | * is the same as {@link #_exposedContext}, and at other times is ahead |
| 73 | * of it. Note that this context is never null. |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 74 | */ |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 75 | protected TokenFilterContext _headContext; |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 78 | * In cases where {@link #_headContext} is "ahead" of context exposed to |
| 79 | * caller, this context points to what is currently exposed to caller. |
| 80 | * When the two are in sync, this context reference will be <code>null</code>. |
| 81 | */ |
| 82 | protected TokenFilterContext _exposedContext; |
Cowtowncoder | 9a797fb | 2015-04-14 16:12:12 -0700 | [diff] [blame] | 83 | |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 84 | /** |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 85 | * State that applies to the item within container, used where applicable. |
| 86 | * Specifically used to pass inclusion state between property name and |
| 87 | * property, and also used for array elements. |
| 88 | */ |
| 89 | protected TokenFilter _itemFilter; |
| 90 | |
| 91 | /** |
| 92 | * Number of tokens for which {@link TokenFilter#INCLUDE_ALL} |
Tatu Saloranta | 5a5d119 | 2015-04-13 23:05:07 -0700 | [diff] [blame] | 93 | * has been returned. |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 94 | */ |
| 95 | protected int _matchCount; |
| 96 | |
| 97 | /* |
| 98 | /********************************************************** |
| 99 | /* Construction, initialization |
| 100 | /********************************************************** |
| 101 | */ |
| 102 | |
| 103 | public FilteringParserDelegate(JsonParser p, TokenFilter f, |
| 104 | boolean includePath, boolean allowMultipleMatches) |
| 105 | { |
| 106 | super(p); |
| 107 | rootFilter = f; |
| 108 | // and this is the currently active filter for root values |
| 109 | _itemFilter = f; |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 110 | _headContext = TokenFilterContext.createRootContext(f); |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 111 | _includePath = includePath; |
| 112 | _allowMultipleMatches = allowMultipleMatches; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | /********************************************************** |
| 117 | /* Extended API |
| 118 | /********************************************************** |
| 119 | */ |
| 120 | |
| 121 | public TokenFilter getFilter() { return rootFilter; } |
| 122 | |
| 123 | /** |
| 124 | * Accessor for finding number of matches, where specific token and sub-tree |
| 125 | * starting (if structured type) are passed. |
| 126 | */ |
| 127 | public int getMatchCount() { |
| 128 | return _matchCount; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | /********************************************************** |
| 133 | /* Public API, token accessors |
| 134 | /********************************************************** |
| 135 | */ |
| 136 | |
| 137 | @Override public JsonToken getCurrentToken() { return _currToken; } |
| 138 | |
| 139 | @Override public final int getCurrentTokenId() { |
| 140 | final JsonToken t = _currToken; |
| 141 | return (t == null) ? JsonTokenId.ID_NO_TOKEN : t.id(); |
| 142 | } |
| 143 | |
| 144 | @Override public boolean hasCurrentToken() { return _currToken != null; } |
| 145 | @Override public boolean hasTokenId(int id) { |
| 146 | final JsonToken t = _currToken; |
| 147 | if (t == null) { |
| 148 | return (JsonTokenId.ID_NO_TOKEN == id); |
| 149 | } |
| 150 | return t.id() == id; |
| 151 | } |
| 152 | |
| 153 | @Override public final boolean hasToken(JsonToken t) { |
| 154 | return (_currToken == t); |
| 155 | } |
| 156 | |
| 157 | @Override public boolean isExpectedStartArrayToken() { return _currToken == JsonToken.START_ARRAY; } |
| 158 | @Override public boolean isExpectedStartObjectToken() { return _currToken == JsonToken.START_OBJECT; } |
| 159 | |
| 160 | @Override public JsonLocation getCurrentLocation() { return delegate.getCurrentLocation(); } |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 161 | |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 162 | @Override |
| 163 | public JsonStreamContext getParsingContext() { |
| 164 | return _filterContext(); |
| 165 | } |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 166 | |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 167 | // !!! TODO: Verify it works as expected: copied from standard JSON parser impl |
| 168 | @Override |
| 169 | public String getCurrentName() throws IOException { |
| 170 | JsonStreamContext ctxt = _filterContext(); |
| 171 | if (_currToken == JsonToken.START_OBJECT || _currToken == JsonToken.START_ARRAY) { |
| 172 | JsonStreamContext parent = ctxt.getParent(); |
| 173 | return (parent == null) ? null : parent.getCurrentName(); |
| 174 | } |
| 175 | return ctxt.getCurrentName(); |
| 176 | } |
| 177 | |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 178 | /* |
| 179 | /********************************************************** |
| 180 | /* Public API, token state overrides |
| 181 | /********************************************************** |
| 182 | */ |
| 183 | |
| 184 | @Override |
| 185 | public void clearCurrentToken() { |
| 186 | if (_currToken != null) { |
| 187 | _lastClearedToken = _currToken; |
| 188 | _currToken = null; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public JsonToken getLastClearedToken() { return _lastClearedToken; } |
| 194 | |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 195 | @Override |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 196 | public void overrideCurrentName(String name) { |
| 197 | /* 14-Apr-2015, tatu: Not sure whether this can be supported, and if so, |
| 198 | * what to do with it... Delegation won't work for sure, so let's for |
| 199 | * now throw an exception |
| 200 | */ |
| 201 | throw new UnsupportedOperationException("Can not currently override name during filtering read"); |
| 202 | } |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | /********************************************************** |
| 206 | /* Public API, traversal |
| 207 | /********************************************************** |
| 208 | */ |
| 209 | |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 210 | @Override |
| 211 | public JsonToken nextToken() throws IOException |
| 212 | { |
Cowtowncoder | 9a797fb | 2015-04-14 16:12:12 -0700 | [diff] [blame] | 213 | // Anything buffered? |
Cowtowncoder | 37d0d6e | 2015-04-14 19:48:10 -0700 | [diff] [blame] | 214 | TokenFilterContext ctxt = _exposedContext; |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 215 | |
Cowtowncoder | 37d0d6e | 2015-04-14 19:48:10 -0700 | [diff] [blame] | 216 | if (ctxt != null) { |
| 217 | while (true) { |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 218 | JsonToken t = ctxt.nextTokenToRead(); |
Cowtowncoder | 37d0d6e | 2015-04-14 19:48:10 -0700 | [diff] [blame] | 219 | if (t != null) { |
| 220 | _currToken = t; |
| 221 | return t; |
| 222 | } |
| 223 | // all done with buffered stuff? |
| 224 | if (ctxt == _headContext) { |
| 225 | _exposedContext = null; |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 226 | // Almost! Most likely still have the current token; |
| 227 | // with the sole exception of |
| 228 | t = delegate.getCurrentToken(); |
| 229 | if (t != JsonToken.FIELD_NAME) { |
| 230 | _currToken = t; |
| 231 | return t; |
| 232 | } |
Cowtowncoder | 37d0d6e | 2015-04-14 19:48:10 -0700 | [diff] [blame] | 233 | break; |
| 234 | } |
| 235 | // If not, traverse down the context chain |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 236 | ctxt = _exposedContext.findChildOf(ctxt); |
Cowtowncoder | 37d0d6e | 2015-04-14 19:48:10 -0700 | [diff] [blame] | 237 | _exposedContext = ctxt; |
| 238 | if (ctxt == null) { // should never occur |
| 239 | throw _constructError("Unexpected problem: chain of filtered context broken"); |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 244 | // If not, need to read more. If we got any: |
Cowtowncoder | 37d0d6e | 2015-04-14 19:48:10 -0700 | [diff] [blame] | 245 | JsonToken t = delegate.nextToken(); |
| 246 | if (t == null) { |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 247 | // no strict need to close, since we have no state here |
| 248 | return (_currToken = t); |
Cowtowncoder | 9a797fb | 2015-04-14 16:12:12 -0700 | [diff] [blame] | 249 | } |
Tatu Saloranta | ce077d4 | 2015-04-14 20:48:23 -0700 | [diff] [blame] | 250 | |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 251 | // otherwise... to include or not? |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 252 | TokenFilter f; |
| 253 | |
| 254 | switch (t.id()) { |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 255 | case ID_START_ARRAY: |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 256 | f = _itemFilter; |
| 257 | if (f == TokenFilter.INCLUDE_ALL) { |
| 258 | _headContext = _headContext.createChildArrayContext(f, true); |
| 259 | return (_currToken = t); |
| 260 | } |
| 261 | if (f == null) { // does this occur? |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 262 | delegate.skipChildren(); |
| 263 | break; |
| 264 | } |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 265 | // Otherwise still iffy, need to check |
| 266 | f = _headContext.checkValue(f); |
| 267 | if (f == null) { |
| 268 | delegate.skipChildren(); |
| 269 | break; |
| 270 | } |
| 271 | if (f != TokenFilter.INCLUDE_ALL) { |
| 272 | f = f.filterStartArray(); |
| 273 | } |
| 274 | _itemFilter = f; |
| 275 | _headContext = _headContext.createChildArrayContext(f, true); |
| 276 | if (f == TokenFilter.INCLUDE_ALL) { |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 277 | return (_currToken = t); |
| 278 | } |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 279 | // but if we didn't figure it out yet, need to buffer possible events |
| 280 | return _nextTokenWithBuffering(_headContext); |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 281 | |
| 282 | case ID_START_OBJECT: |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 283 | f = _itemFilter; |
| 284 | if (f == TokenFilter.INCLUDE_ALL) { |
| 285 | _headContext = _headContext.createChildObjectContext(f, true); |
| 286 | return (_currToken = t); |
| 287 | } |
| 288 | if (f == null) { // does this occur? |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 289 | delegate.skipChildren(); |
| 290 | break; |
| 291 | } |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 292 | // Otherwise still iffy, need to check |
| 293 | f = _headContext.checkValue(f); |
| 294 | if (f == null) { |
| 295 | delegate.skipChildren(); |
| 296 | break; |
| 297 | } |
| 298 | if (f != TokenFilter.INCLUDE_ALL) { |
| 299 | f = f.filterStartObject(); |
| 300 | } |
| 301 | _itemFilter = f; |
| 302 | _headContext = _headContext.createChildObjectContext(f, true); |
| 303 | if (f == TokenFilter.INCLUDE_ALL) { |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 304 | return (_currToken = t); |
| 305 | } |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 306 | // but if we didn't figure it out yet, need to buffer possible events |
| 307 | return _nextTokenWithBuffering(_headContext); |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 308 | |
| 309 | case ID_END_ARRAY: |
| 310 | case ID_END_OBJECT: |
| 311 | { |
| 312 | boolean returnEnd = _headContext.isStartHandled(); |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 313 | f = _headContext.getFilter(); |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 314 | if ((f != null) && (f != TokenFilter.INCLUDE_ALL)) { |
| 315 | f.filterFinishArray(); |
| 316 | } |
| 317 | _headContext = _headContext.getParent(); |
| 318 | _itemFilter = _headContext.getFilter(); |
| 319 | if (returnEnd) { |
| 320 | return (_currToken = t); |
| 321 | } |
| 322 | } |
| 323 | break; |
| 324 | |
| 325 | case ID_FIELD_NAME: |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 326 | { |
| 327 | final String name = delegate.getCurrentName(); |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 328 | // note: this will also set 'needToHandleName' |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 329 | f = _headContext.setFieldName(name); |
| 330 | if (f == TokenFilter.INCLUDE_ALL) { |
| 331 | _itemFilter = f; |
| 332 | return (_currToken = t); |
| 333 | } |
| 334 | if (f == null) { // filter out the value |
| 335 | delegate.nextToken(); |
| 336 | delegate.skipChildren(); |
| 337 | break; |
| 338 | } |
| 339 | f = f.includeProperty(name); |
| 340 | if (f == null) { // filter out the value |
| 341 | delegate.nextToken(); |
| 342 | delegate.skipChildren(); |
| 343 | break; |
| 344 | } |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 345 | _itemFilter = f; |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 346 | if (f == TokenFilter.INCLUDE_ALL) { |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 347 | return (_currToken = t); |
| 348 | } |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 349 | return _nextTokenWithBuffering(_headContext); |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 350 | } |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 351 | |
| 352 | default: // scalar value |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 353 | if (_itemFilter == TokenFilter.INCLUDE_ALL) { |
| 354 | return (_currToken = t); |
| 355 | } |
| 356 | // Otherwise not included (leaves must be explicitly included) |
| 357 | break; |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // We get here if token was not yet found; offlined handling |
| 361 | return _nextToken2(); |
| 362 | } |
| 363 | |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 364 | /** |
| 365 | * Offlined handling for cases where there was no buffered token to |
| 366 | * return, and the token read next could not be returned as-is, |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 367 | * at least not yet, but where we have not yet established that |
| 368 | * buffering is needed. |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 369 | */ |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 370 | protected final JsonToken _nextToken2() throws IOException |
| 371 | { |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 372 | main_loop: |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 373 | while (true) { |
| 374 | JsonToken t = delegate.nextToken(); |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 375 | |
| 376 | if (t == null) { // is this even legal? |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 377 | return (_currToken = t); |
| 378 | } |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 379 | TokenFilter f; |
| 380 | |
| 381 | switch (t.id()) { |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 382 | case ID_START_ARRAY: |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 383 | f = _itemFilter; |
| 384 | if (f == TokenFilter.INCLUDE_ALL) { |
| 385 | _headContext = _headContext.createChildArrayContext(f, true); |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 386 | return (_currToken = t); |
| 387 | } |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 388 | if (f == null) { // does this occur? |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 389 | delegate.skipChildren(); |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 390 | continue main_loop; |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 391 | } |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 392 | // Otherwise still iffy, need to check |
| 393 | f = _headContext.checkValue(f); |
| 394 | if (f == null) { |
| 395 | delegate.skipChildren(); |
| 396 | continue main_loop; |
| 397 | } |
| 398 | if (f != TokenFilter.INCLUDE_ALL) { |
| 399 | f = f.filterStartArray(); |
| 400 | } |
| 401 | _itemFilter = f; |
| 402 | _headContext = _headContext.createChildArrayContext(f, true); |
| 403 | if (f == TokenFilter.INCLUDE_ALL) { |
| 404 | return (_currToken = t); |
| 405 | } |
| 406 | // but if we didn't figure it out yet, need to buffer possible events |
| 407 | return _nextTokenWithBuffering(_headContext); |
| 408 | |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 409 | case ID_START_OBJECT: |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 410 | f = _itemFilter; |
| 411 | if (f == TokenFilter.INCLUDE_ALL) { |
| 412 | _headContext = _headContext.createChildObjectContext(f, true); |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 413 | return (_currToken = t); |
| 414 | } |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 415 | if (f == null) { // does this occur? |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 416 | delegate.skipChildren(); |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 417 | continue main_loop; |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 418 | } |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 419 | // Otherwise still iffy, need to check |
| 420 | f = _headContext.checkValue(f); |
| 421 | if (f == null) { |
| 422 | delegate.skipChildren(); |
| 423 | continue main_loop; |
| 424 | } |
| 425 | if (f != TokenFilter.INCLUDE_ALL) { |
| 426 | f = f.filterStartObject(); |
| 427 | } |
| 428 | _itemFilter = f; |
| 429 | _headContext = _headContext.createChildObjectContext(f, true); |
| 430 | if (f == TokenFilter.INCLUDE_ALL) { |
| 431 | return (_currToken = t); |
| 432 | } |
| 433 | // but if we didn't figure it out yet, need to buffer possible events |
| 434 | return _nextTokenWithBuffering(_headContext); |
| 435 | |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 436 | case ID_END_ARRAY: |
| 437 | case ID_END_OBJECT: |
| 438 | { |
| 439 | boolean returnEnd = _headContext.isStartHandled(); |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 440 | f = _headContext.getFilter(); |
| 441 | if ((f != null) && (f != TokenFilter.INCLUDE_ALL)) { |
| 442 | f.filterFinishArray(); |
| 443 | } |
| 444 | _headContext = _headContext.getParent(); |
| 445 | _itemFilter = _headContext.getFilter(); |
| 446 | if (returnEnd) { |
| 447 | return (_currToken = t); |
| 448 | } |
| 449 | } |
| 450 | continue main_loop; |
| 451 | |
| 452 | case ID_FIELD_NAME: |
| 453 | { |
| 454 | final String name = delegate.getCurrentName(); |
| 455 | f = _headContext.setFieldName(name); |
| 456 | if (f == TokenFilter.INCLUDE_ALL) { |
| 457 | _itemFilter = f; |
| 458 | return (_currToken = t); |
| 459 | } |
| 460 | if (f == null) { // filter out the value |
| 461 | delegate.nextToken(); |
| 462 | delegate.skipChildren(); |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 463 | continue main_loop; |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 464 | } |
| 465 | f = f.includeProperty(name); |
| 466 | if (f == null) { // filter out the value |
| 467 | delegate.nextToken(); |
| 468 | delegate.skipChildren(); |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 469 | continue main_loop; |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 470 | } |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 471 | _itemFilter = f; |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 472 | if (f == TokenFilter.INCLUDE_ALL) { |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 473 | return (_currToken = t); |
| 474 | } |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 475 | } |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 476 | return _nextTokenWithBuffering(_headContext); |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 477 | |
| 478 | default: // scalar value |
| 479 | if (_itemFilter == TokenFilter.INCLUDE_ALL) { |
| 480 | return (_currToken = t); |
| 481 | } |
| 482 | // Otherwise not included (leaves must be explicitly included) |
| 483 | continue main_loop; |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Method called when a new potentially included context is found. |
| 490 | */ |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 491 | protected final JsonToken _nextTokenWithBuffering(final TokenFilterContext buffRoot) |
| 492 | throws IOException |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 493 | { |
| 494 | _exposedContext = _headContext; |
| 495 | |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 496 | main_loop: |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 497 | while (true) { |
| 498 | JsonToken t = delegate.nextToken(); |
| 499 | |
| 500 | if (t == null) { // is this even legal? |
| 501 | return (_currToken = t); |
| 502 | } |
| 503 | TokenFilter f; |
| 504 | |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 505 | // One simplification here: we know for a fact that the item filter is |
| 506 | // neither null nor 'include all', for most cases; the only exception |
| 507 | // being FIELD_NAME handling |
| 508 | |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 509 | switch (t.id()) { |
| 510 | case ID_START_ARRAY: |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 511 | f = _headContext.checkValue(_itemFilter); |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 512 | if (f == null) { |
| 513 | delegate.skipChildren(); |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 514 | continue main_loop; |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 515 | } |
| 516 | if (f != TokenFilter.INCLUDE_ALL) { |
| 517 | f = f.filterStartArray(); |
| 518 | } |
| 519 | _itemFilter = f; |
| 520 | _headContext = _headContext.createChildArrayContext(f, true); |
| 521 | if (f == TokenFilter.INCLUDE_ALL) { |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 522 | return _nextBuffered(); |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 523 | } |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 524 | continue main_loop; |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 525 | |
| 526 | case ID_START_OBJECT: |
| 527 | f = _itemFilter; |
| 528 | if (f == TokenFilter.INCLUDE_ALL) { |
| 529 | _headContext = _headContext.createChildObjectContext(f, true); |
| 530 | return (_currToken = t); |
| 531 | } |
| 532 | if (f == null) { // does this occur? |
| 533 | delegate.skipChildren(); |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 534 | continue main_loop; |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 535 | } |
| 536 | // Otherwise still iffy, need to check |
| 537 | f = _headContext.checkValue(f); |
| 538 | if (f == null) { |
| 539 | delegate.skipChildren(); |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 540 | continue main_loop; |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 541 | } |
| 542 | if (f != TokenFilter.INCLUDE_ALL) { |
| 543 | f = f.filterStartObject(); |
| 544 | } |
| 545 | _itemFilter = f; |
| 546 | _headContext = _headContext.createChildObjectContext(f, true); |
| 547 | if (f == TokenFilter.INCLUDE_ALL) { |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 548 | return _nextBuffered(); |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 549 | } |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 550 | continue main_loop; |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 551 | |
| 552 | case ID_END_ARRAY: |
| 553 | case ID_END_OBJECT: |
| 554 | { |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 555 | // Unlike with other loops, here we know that content was NOT |
| 556 | // included (won't get this far otherwise) |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 557 | f = _headContext.getFilter(); |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 558 | if ((f != null) && (f != TokenFilter.INCLUDE_ALL)) { |
| 559 | f.filterFinishArray(); |
| 560 | } |
| 561 | _headContext = _headContext.getParent(); |
| 562 | _itemFilter = _headContext.getFilter(); |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 563 | |
| 564 | if (_headContext == buffRoot) { |
| 565 | // !!! TBI |
| 566 | throw _constructError("Internal error: end of possible inclusion -- TBI"); |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 567 | } |
| 568 | } |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 569 | continue main_loop; |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 570 | |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 571 | case ID_FIELD_NAME: |
| 572 | { |
| 573 | final String name = delegate.getCurrentName(); |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 574 | f = _headContext.setFieldName(name); |
| 575 | if (f == TokenFilter.INCLUDE_ALL) { |
| 576 | _itemFilter = f; |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 577 | return _nextBuffered(); |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 578 | } |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 579 | if (f == null) { // filter out the value |
| 580 | delegate.nextToken(); |
| 581 | delegate.skipChildren(); |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 582 | continue main_loop; |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 583 | } |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 584 | f = f.includeProperty(name); |
| 585 | if (f == null) { // filter out the value |
| 586 | delegate.nextToken(); |
| 587 | delegate.skipChildren(); |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 588 | continue main_loop; |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 589 | } |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 590 | _itemFilter = f; |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 591 | if (f == TokenFilter.INCLUDE_ALL) { |
| 592 | return _nextBuffered(); |
| 593 | } |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 594 | } |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 595 | continue main_loop; |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 596 | |
| 597 | default: // scalar value |
| 598 | if (_itemFilter == TokenFilter.INCLUDE_ALL) { |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 599 | return _nextBuffered(); |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 600 | } |
| 601 | // Otherwise not included (leaves must be explicitly included) |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 602 | } |
| 603 | } |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame^] | 604 | } |
| 605 | |
| 606 | private JsonToken _nextBuffered() throws IOException |
| 607 | { |
| 608 | TokenFilterContext ctxt = _exposedContext; |
| 609 | JsonToken t = ctxt.nextTokenToRead(); |
| 610 | if (t != null) { |
| 611 | _currToken = t; |
| 612 | return t; |
| 613 | } |
| 614 | while (true) { |
| 615 | // all done with buffered stuff? |
| 616 | if (ctxt == _headContext) { |
| 617 | throw _constructError("Internal error: failed to locate expected buffered tokens"); |
| 618 | /* |
| 619 | _exposedContext = null; |
| 620 | break; |
| 621 | */ |
| 622 | } |
| 623 | // If not, traverse down the context chain |
| 624 | ctxt = _exposedContext.findChildOf(ctxt); |
| 625 | _exposedContext = ctxt; |
| 626 | if (ctxt == null) { // should never occur |
| 627 | throw _constructError("Unexpected problem: chain of filtered context broken"); |
| 628 | } |
| 629 | t = _exposedContext.nextTokenToRead(); |
| 630 | if (t != null) { |
| 631 | _currToken = t; |
| 632 | return t; |
| 633 | } |
| 634 | } |
| 635 | } |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 636 | |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 637 | @Override |
| 638 | public JsonToken nextValue() throws IOException { |
| 639 | // Re-implemented same as ParserMinimalBase: |
| 640 | JsonToken t = nextToken(); |
| 641 | if (t == JsonToken.FIELD_NAME) { |
| 642 | t = nextToken(); |
| 643 | } |
| 644 | return t; |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Need to override, re-implement similar to how method defined in |
| 649 | * {@link com.fasterxml.jackson.core.base.ParserMinimalBase}, to keep |
| 650 | * state correct here. |
| 651 | */ |
| 652 | @Override |
| 653 | public JsonParser skipChildren() throws IOException |
| 654 | { |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 655 | if ((_currToken != JsonToken.START_OBJECT) |
| 656 | && (_currToken != JsonToken.START_ARRAY)) { |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 657 | return this; |
| 658 | } |
| 659 | int open = 1; |
| 660 | |
| 661 | // Since proper matching of start/end markers is handled |
| 662 | // by nextToken(), we'll just count nesting levels here |
| 663 | while (true) { |
| 664 | JsonToken t = nextToken(); |
| 665 | if (t == null) { // not ideal but for now, just return |
| 666 | return this; |
| 667 | } |
| 668 | if (t.isStructStart()) { |
| 669 | ++open; |
| 670 | } else if (t.isStructEnd()) { |
| 671 | if (--open == 0) { |
| 672 | return this; |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | /* |
| 679 | /********************************************************** |
| 680 | /* Public API, access to token information, text |
| 681 | /********************************************************** |
| 682 | */ |
| 683 | |
| 684 | @Override public String getText() throws IOException { return delegate.getText(); } |
| 685 | @Override public boolean hasTextCharacters() { return delegate.hasTextCharacters(); } |
| 686 | @Override public char[] getTextCharacters() throws IOException { return delegate.getTextCharacters(); } |
| 687 | @Override public int getTextLength() throws IOException { return delegate.getTextLength(); } |
| 688 | @Override public int getTextOffset() throws IOException { return delegate.getTextOffset(); } |
| 689 | |
| 690 | /* |
| 691 | /********************************************************** |
| 692 | /* Public API, access to token information, numeric |
| 693 | /********************************************************** |
| 694 | */ |
| 695 | |
| 696 | @Override |
| 697 | public BigInteger getBigIntegerValue() throws IOException { return delegate.getBigIntegerValue(); } |
| 698 | |
| 699 | @Override |
| 700 | public boolean getBooleanValue() throws IOException { return delegate.getBooleanValue(); } |
| 701 | |
| 702 | @Override |
| 703 | public byte getByteValue() throws IOException { return delegate.getByteValue(); } |
| 704 | |
| 705 | @Override |
| 706 | public short getShortValue() throws IOException { return delegate.getShortValue(); } |
| 707 | |
| 708 | @Override |
| 709 | public BigDecimal getDecimalValue() throws IOException { return delegate.getDecimalValue(); } |
| 710 | |
| 711 | @Override |
| 712 | public double getDoubleValue() throws IOException { return delegate.getDoubleValue(); } |
| 713 | |
| 714 | @Override |
| 715 | public float getFloatValue() throws IOException { return delegate.getFloatValue(); } |
| 716 | |
| 717 | @Override |
| 718 | public int getIntValue() throws IOException { return delegate.getIntValue(); } |
| 719 | |
| 720 | @Override |
| 721 | public long getLongValue() throws IOException { return delegate.getLongValue(); } |
| 722 | |
| 723 | @Override |
| 724 | public NumberType getNumberType() throws IOException { return delegate.getNumberType(); } |
| 725 | |
| 726 | @Override |
| 727 | public Number getNumberValue() throws IOException { return delegate.getNumberValue(); } |
| 728 | |
| 729 | /* |
| 730 | /********************************************************** |
| 731 | /* Public API, access to token information, coercion/conversion |
| 732 | /********************************************************** |
| 733 | */ |
| 734 | |
| 735 | @Override public int getValueAsInt() throws IOException { return delegate.getValueAsInt(); } |
| 736 | @Override public int getValueAsInt(int defaultValue) throws IOException { return delegate.getValueAsInt(defaultValue); } |
| 737 | @Override public long getValueAsLong() throws IOException { return delegate.getValueAsLong(); } |
| 738 | @Override public long getValueAsLong(long defaultValue) throws IOException { return delegate.getValueAsLong(defaultValue); } |
| 739 | @Override public double getValueAsDouble() throws IOException { return delegate.getValueAsDouble(); } |
| 740 | @Override public double getValueAsDouble(double defaultValue) throws IOException { return delegate.getValueAsDouble(defaultValue); } |
| 741 | @Override public boolean getValueAsBoolean() throws IOException { return delegate.getValueAsBoolean(); } |
| 742 | @Override public boolean getValueAsBoolean(boolean defaultValue) throws IOException { return delegate.getValueAsBoolean(defaultValue); } |
| 743 | @Override public String getValueAsString() throws IOException { return delegate.getValueAsString(); } |
| 744 | @Override public String getValueAsString(String defaultValue) throws IOException { return delegate.getValueAsString(defaultValue); } |
| 745 | |
| 746 | /* |
| 747 | /********************************************************** |
| 748 | /* Public API, access to token values, other |
| 749 | /********************************************************** |
| 750 | */ |
| 751 | |
| 752 | @Override public Object getEmbeddedObject() throws IOException { return delegate.getEmbeddedObject(); } |
| 753 | @Override public byte[] getBinaryValue(Base64Variant b64variant) throws IOException { return delegate.getBinaryValue(b64variant); } |
| 754 | @Override public int readBinaryValue(Base64Variant b64variant, OutputStream out) throws IOException { return delegate.readBinaryValue(b64variant, out); } |
| 755 | @Override public JsonLocation getTokenLocation() { return delegate.getTokenLocation(); } |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 756 | |
| 757 | /* |
| 758 | /********************************************************** |
| 759 | /* Internal helper methods |
| 760 | /********************************************************** |
| 761 | */ |
| 762 | |
| 763 | protected JsonStreamContext _filterContext() { |
| 764 | if (_exposedContext != null) { |
| 765 | return _exposedContext; |
| 766 | } |
| 767 | return _headContext; |
| 768 | } |
| 769 | |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 770 | } |