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