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