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 |
Rafal Foltynski | 1415e9d | 2017-06-03 02:43:18 +0200 | [diff] [blame] | 236 | |
Tatu Saloranta | 96faf2e | 2017-05-23 16:08:31 -0700 | [diff] [blame] | 237 | if (!_allowMultipleMatches && (_currToken != null) && (_exposedContext == null)) { |
Rafal Foltynski | 1415e9d | 2017-06-03 02:43:18 +0200 | [diff] [blame] | 238 | // if scalar, and scalar not present in obj/array and !includePath and INCLUDE_ALL |
Tatu Saloranta | 4ed6f92 | 2017-05-29 19:33:04 -0700 | [diff] [blame] | 239 | // matched once, return null |
| 240 | if (_currToken.isScalarValue() && !_headContext.isStartHandled() && !_includePath |
| 241 | && (_itemFilter == TokenFilter.INCLUDE_ALL)) { |
| 242 | return (_currToken = null); |
Tatu Saloranta | 96faf2e | 2017-05-23 16:08:31 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
Cowtowncoder | 9a797fb | 2015-04-14 16:12:12 -0700 | [diff] [blame] | 245 | // Anything buffered? |
Cowtowncoder | 37d0d6e | 2015-04-14 19:48:10 -0700 | [diff] [blame] | 246 | TokenFilterContext ctxt = _exposedContext; |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 247 | |
Cowtowncoder | 37d0d6e | 2015-04-14 19:48:10 -0700 | [diff] [blame] | 248 | if (ctxt != null) { |
| 249 | while (true) { |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame] | 250 | JsonToken t = ctxt.nextTokenToRead(); |
Cowtowncoder | 37d0d6e | 2015-04-14 19:48:10 -0700 | [diff] [blame] | 251 | if (t != null) { |
| 252 | _currToken = t; |
| 253 | return t; |
| 254 | } |
| 255 | // all done with buffered stuff? |
| 256 | if (ctxt == _headContext) { |
| 257 | _exposedContext = null; |
Cowtowncoder | ac4b449 | 2015-04-23 15:36:50 -0700 | [diff] [blame] | 258 | if (ctxt.inArray()) { |
| 259 | t = delegate.getCurrentToken(); |
| 260 | // Is this guaranteed to work without further checks? |
| 261 | // if (t != JsonToken.START_ARRAY) { |
| 262 | _currToken = t; |
| 263 | return t; |
| 264 | } |
| 265 | |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame] | 266 | // Almost! Most likely still have the current token; |
| 267 | // with the sole exception of |
Cowtowncoder | df52157 | 2015-04-16 15:58:41 -0700 | [diff] [blame] | 268 | /* |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame] | 269 | t = delegate.getCurrentToken(); |
| 270 | if (t != JsonToken.FIELD_NAME) { |
| 271 | _currToken = t; |
| 272 | return t; |
| 273 | } |
Cowtowncoder | df52157 | 2015-04-16 15:58:41 -0700 | [diff] [blame] | 274 | */ |
Cowtowncoder | 37d0d6e | 2015-04-14 19:48:10 -0700 | [diff] [blame] | 275 | break; |
| 276 | } |
| 277 | // If not, traverse down the context chain |
Cowtowncoder | df52157 | 2015-04-16 15:58:41 -0700 | [diff] [blame] | 278 | ctxt = _headContext.findChildOf(ctxt); |
Cowtowncoder | 37d0d6e | 2015-04-14 19:48:10 -0700 | [diff] [blame] | 279 | _exposedContext = ctxt; |
| 280 | if (ctxt == null) { // should never occur |
| 281 | throw _constructError("Unexpected problem: chain of filtered context broken"); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 286 | // If not, need to read more. If we got any: |
Cowtowncoder | 37d0d6e | 2015-04-14 19:48:10 -0700 | [diff] [blame] | 287 | JsonToken t = delegate.nextToken(); |
| 288 | if (t == null) { |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 289 | // no strict need to close, since we have no state here |
Tatu Saloranta | e962fb9 | 2016-07-21 14:14:26 -0700 | [diff] [blame] | 290 | _currToken = t; |
| 291 | return t; |
Cowtowncoder | 9a797fb | 2015-04-14 16:12:12 -0700 | [diff] [blame] | 292 | } |
Tatu Saloranta | ce077d4 | 2015-04-14 20:48:23 -0700 | [diff] [blame] | 293 | |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 294 | // otherwise... to include or not? |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 295 | TokenFilter f; |
| 296 | |
| 297 | switch (t.id()) { |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 298 | case ID_START_ARRAY: |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 299 | f = _itemFilter; |
| 300 | if (f == TokenFilter.INCLUDE_ALL) { |
| 301 | _headContext = _headContext.createChildArrayContext(f, true); |
| 302 | return (_currToken = t); |
| 303 | } |
| 304 | if (f == null) { // does this occur? |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 305 | delegate.skipChildren(); |
| 306 | break; |
| 307 | } |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 308 | // Otherwise still iffy, need to check |
| 309 | f = _headContext.checkValue(f); |
| 310 | if (f == null) { |
| 311 | delegate.skipChildren(); |
| 312 | break; |
| 313 | } |
| 314 | if (f != TokenFilter.INCLUDE_ALL) { |
| 315 | f = f.filterStartArray(); |
| 316 | } |
| 317 | _itemFilter = f; |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 318 | if (f == TokenFilter.INCLUDE_ALL) { |
Cowtowncoder | 654acf0 | 2015-04-16 15:30:43 -0700 | [diff] [blame] | 319 | _headContext = _headContext.createChildArrayContext(f, true); |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 320 | return (_currToken = t); |
| 321 | } |
Cowtowncoder | 654acf0 | 2015-04-16 15:30:43 -0700 | [diff] [blame] | 322 | _headContext = _headContext.createChildArrayContext(f, false); |
| 323 | |
| 324 | // Also: only need buffering if parent path to be included |
| 325 | if (_includePath) { |
Cowtowncoder | b808f26 | 2015-04-23 15:47:39 -0700 | [diff] [blame] | 326 | t = _nextTokenWithBuffering(_headContext); |
| 327 | if (t != null) { |
| 328 | _currToken = t; |
| 329 | return t; |
| 330 | } |
Cowtowncoder | 654acf0 | 2015-04-16 15:30:43 -0700 | [diff] [blame] | 331 | } |
| 332 | break; |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 333 | |
| 334 | case ID_START_OBJECT: |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 335 | f = _itemFilter; |
| 336 | if (f == TokenFilter.INCLUDE_ALL) { |
| 337 | _headContext = _headContext.createChildObjectContext(f, true); |
| 338 | return (_currToken = t); |
| 339 | } |
| 340 | if (f == null) { // does this occur? |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 341 | delegate.skipChildren(); |
| 342 | break; |
| 343 | } |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 344 | // Otherwise still iffy, need to check |
| 345 | f = _headContext.checkValue(f); |
| 346 | if (f == null) { |
| 347 | delegate.skipChildren(); |
| 348 | break; |
| 349 | } |
| 350 | if (f != TokenFilter.INCLUDE_ALL) { |
| 351 | f = f.filterStartObject(); |
| 352 | } |
| 353 | _itemFilter = f; |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 354 | if (f == TokenFilter.INCLUDE_ALL) { |
Cowtowncoder | 654acf0 | 2015-04-16 15:30:43 -0700 | [diff] [blame] | 355 | _headContext = _headContext.createChildObjectContext(f, true); |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 356 | return (_currToken = t); |
| 357 | } |
Cowtowncoder | 654acf0 | 2015-04-16 15:30:43 -0700 | [diff] [blame] | 358 | _headContext = _headContext.createChildObjectContext(f, false); |
| 359 | // Also: only need buffering if parent path to be included |
| 360 | if (_includePath) { |
Cowtowncoder | b808f26 | 2015-04-23 15:47:39 -0700 | [diff] [blame] | 361 | t = _nextTokenWithBuffering(_headContext); |
| 362 | if (t != null) { |
| 363 | _currToken = t; |
| 364 | return t; |
| 365 | } |
Cowtowncoder | 654acf0 | 2015-04-16 15:30:43 -0700 | [diff] [blame] | 366 | } |
| 367 | // note: inclusion of surrounding Object handled separately via |
| 368 | // FIELD_NAME |
| 369 | break; |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 370 | |
| 371 | case ID_END_ARRAY: |
| 372 | case ID_END_OBJECT: |
| 373 | { |
| 374 | boolean returnEnd = _headContext.isStartHandled(); |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 375 | f = _headContext.getFilter(); |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 376 | if ((f != null) && (f != TokenFilter.INCLUDE_ALL)) { |
| 377 | f.filterFinishArray(); |
| 378 | } |
| 379 | _headContext = _headContext.getParent(); |
| 380 | _itemFilter = _headContext.getFilter(); |
| 381 | if (returnEnd) { |
| 382 | return (_currToken = t); |
| 383 | } |
| 384 | } |
| 385 | break; |
| 386 | |
| 387 | case ID_FIELD_NAME: |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 388 | { |
| 389 | final String name = delegate.getCurrentName(); |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame] | 390 | // note: this will also set 'needToHandleName' |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 391 | f = _headContext.setFieldName(name); |
| 392 | if (f == TokenFilter.INCLUDE_ALL) { |
| 393 | _itemFilter = f; |
Tatu Saloranta | cfe32d0 | 2015-04-21 23:17:22 -0700 | [diff] [blame] | 394 | if (!_includePath) { |
| 395 | // Minor twist here: if parent NOT included, may need to induce output of |
| 396 | // surrounding START_OBJECT/END_OBJECT |
| 397 | if (_includeImmediateParent && !_headContext.isStartHandled()) { |
| 398 | t = _headContext.nextTokenToRead(); // returns START_OBJECT but also marks it handled |
| 399 | _exposedContext = _headContext; |
| 400 | } |
Cowtowncoder | 654acf0 | 2015-04-16 15:30:43 -0700 | [diff] [blame] | 401 | } |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 402 | return (_currToken = t); |
| 403 | } |
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 | } |
| 409 | f = f.includeProperty(name); |
Cowtowncoder | 654acf0 | 2015-04-16 15:30:43 -0700 | [diff] [blame] | 410 | if (f == null) { |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 411 | delegate.nextToken(); |
| 412 | delegate.skipChildren(); |
| 413 | break; |
| 414 | } |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame] | 415 | _itemFilter = f; |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 416 | if (f == TokenFilter.INCLUDE_ALL) { |
Rafal Foltynski | 6feec20 | 2017-06-04 05:32:03 +0200 | [diff] [blame] | 417 | if (_verifyAllowedMatches() && _includePath) { |
Tatu Saloranta | cfe32d0 | 2015-04-21 23:17:22 -0700 | [diff] [blame] | 418 | return (_currToken = t); |
| 419 | } |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 420 | } |
Cowtowncoder | 654acf0 | 2015-04-16 15:30:43 -0700 | [diff] [blame] | 421 | if (_includePath) { |
Cowtowncoder | b808f26 | 2015-04-23 15:47:39 -0700 | [diff] [blame] | 422 | t = _nextTokenWithBuffering(_headContext); |
| 423 | if (t != null) { |
| 424 | _currToken = t; |
| 425 | return t; |
| 426 | } |
Cowtowncoder | 654acf0 | 2015-04-16 15:30:43 -0700 | [diff] [blame] | 427 | } |
| 428 | break; |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 429 | } |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 430 | |
| 431 | default: // scalar value |
Cowtowncoder | 3222428 | 2015-04-16 16:39:22 -0700 | [diff] [blame] | 432 | f = _itemFilter; |
| 433 | if (f == TokenFilter.INCLUDE_ALL) { |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 434 | return (_currToken = t); |
| 435 | } |
Cowtowncoder | 3222428 | 2015-04-16 16:39:22 -0700 | [diff] [blame] | 436 | if (f != null) { |
| 437 | f = _headContext.checkValue(f); |
| 438 | if ((f == TokenFilter.INCLUDE_ALL) |
| 439 | || ((f != null) && f.includeValue(delegate))) { |
Rafal Foltynski | 6feec20 | 2017-06-04 05:32:03 +0200 | [diff] [blame] | 440 | if (_verifyAllowedMatches()) { |
| 441 | return (_currToken = t); |
| 442 | } |
Cowtowncoder | 3222428 | 2015-04-16 16:39:22 -0700 | [diff] [blame] | 443 | } |
| 444 | } |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 445 | // Otherwise not included (leaves must be explicitly included) |
| 446 | break; |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | // We get here if token was not yet found; offlined handling |
| 450 | return _nextToken2(); |
| 451 | } |
| 452 | |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 453 | /** |
| 454 | * Offlined handling for cases where there was no buffered token to |
| 455 | * return, and the token read next could not be returned as-is, |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 456 | * at least not yet, but where we have not yet established that |
| 457 | * buffering is needed. |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 458 | */ |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame] | 459 | protected final JsonToken _nextToken2() throws IOException |
| 460 | { |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 461 | main_loop: |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 462 | while (true) { |
| 463 | JsonToken t = delegate.nextToken(); |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 464 | if (t == null) { // is this even legal? |
Tatu Saloranta | e962fb9 | 2016-07-21 14:14:26 -0700 | [diff] [blame] | 465 | _currToken = t; |
| 466 | return t; |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 467 | } |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 468 | TokenFilter f; |
| 469 | |
| 470 | switch (t.id()) { |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 471 | case ID_START_ARRAY: |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 472 | f = _itemFilter; |
| 473 | if (f == TokenFilter.INCLUDE_ALL) { |
| 474 | _headContext = _headContext.createChildArrayContext(f, true); |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 475 | return (_currToken = t); |
| 476 | } |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 477 | if (f == null) { // does this occur? |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 478 | delegate.skipChildren(); |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 479 | continue main_loop; |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 480 | } |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 481 | // Otherwise still iffy, need to check |
| 482 | f = _headContext.checkValue(f); |
| 483 | if (f == null) { |
| 484 | delegate.skipChildren(); |
| 485 | continue main_loop; |
| 486 | } |
| 487 | if (f != TokenFilter.INCLUDE_ALL) { |
| 488 | f = f.filterStartArray(); |
| 489 | } |
| 490 | _itemFilter = f; |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 491 | if (f == TokenFilter.INCLUDE_ALL) { |
Cowtowncoder | 654acf0 | 2015-04-16 15:30:43 -0700 | [diff] [blame] | 492 | _headContext = _headContext.createChildArrayContext(f, true); |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 493 | return (_currToken = t); |
| 494 | } |
Cowtowncoder | 654acf0 | 2015-04-16 15:30:43 -0700 | [diff] [blame] | 495 | _headContext = _headContext.createChildArrayContext(f, false); |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 496 | // 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] | 497 | if (_includePath) { |
Cowtowncoder | b808f26 | 2015-04-23 15:47:39 -0700 | [diff] [blame] | 498 | t = _nextTokenWithBuffering(_headContext); |
| 499 | if (t != null) { |
| 500 | _currToken = t; |
| 501 | return t; |
| 502 | } |
Cowtowncoder | 654acf0 | 2015-04-16 15:30:43 -0700 | [diff] [blame] | 503 | } |
| 504 | continue main_loop; |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 505 | |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 506 | case ID_START_OBJECT: |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 507 | f = _itemFilter; |
| 508 | if (f == TokenFilter.INCLUDE_ALL) { |
| 509 | _headContext = _headContext.createChildObjectContext(f, true); |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 510 | return (_currToken = t); |
| 511 | } |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 512 | if (f == null) { // does this occur? |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 513 | delegate.skipChildren(); |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 514 | continue main_loop; |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 515 | } |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 516 | // Otherwise still iffy, need to check |
| 517 | f = _headContext.checkValue(f); |
| 518 | if (f == null) { |
| 519 | delegate.skipChildren(); |
| 520 | continue main_loop; |
| 521 | } |
| 522 | if (f != TokenFilter.INCLUDE_ALL) { |
| 523 | f = f.filterStartObject(); |
| 524 | } |
| 525 | _itemFilter = f; |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 526 | if (f == TokenFilter.INCLUDE_ALL) { |
Cowtowncoder | 654acf0 | 2015-04-16 15:30:43 -0700 | [diff] [blame] | 527 | _headContext = _headContext.createChildObjectContext(f, true); |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 528 | return (_currToken = t); |
| 529 | } |
Cowtowncoder | 654acf0 | 2015-04-16 15:30:43 -0700 | [diff] [blame] | 530 | _headContext = _headContext.createChildObjectContext(f, false); |
| 531 | if (_includePath) { |
Cowtowncoder | b808f26 | 2015-04-23 15:47:39 -0700 | [diff] [blame] | 532 | t = _nextTokenWithBuffering(_headContext); |
| 533 | if (t != null) { |
| 534 | _currToken = t; |
| 535 | return t; |
| 536 | } |
Cowtowncoder | 654acf0 | 2015-04-16 15:30:43 -0700 | [diff] [blame] | 537 | } |
| 538 | continue main_loop; |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 539 | |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 540 | case ID_END_ARRAY: |
| 541 | case ID_END_OBJECT: |
| 542 | { |
| 543 | boolean returnEnd = _headContext.isStartHandled(); |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 544 | f = _headContext.getFilter(); |
| 545 | if ((f != null) && (f != TokenFilter.INCLUDE_ALL)) { |
| 546 | f.filterFinishArray(); |
| 547 | } |
| 548 | _headContext = _headContext.getParent(); |
| 549 | _itemFilter = _headContext.getFilter(); |
| 550 | if (returnEnd) { |
| 551 | return (_currToken = t); |
| 552 | } |
| 553 | } |
| 554 | continue main_loop; |
| 555 | |
| 556 | case ID_FIELD_NAME: |
| 557 | { |
| 558 | final String name = delegate.getCurrentName(); |
| 559 | f = _headContext.setFieldName(name); |
| 560 | if (f == TokenFilter.INCLUDE_ALL) { |
| 561 | _itemFilter = f; |
| 562 | return (_currToken = t); |
| 563 | } |
| 564 | if (f == null) { // filter out the value |
| 565 | delegate.nextToken(); |
| 566 | delegate.skipChildren(); |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame] | 567 | continue main_loop; |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 568 | } |
| 569 | f = f.includeProperty(name); |
| 570 | if (f == null) { // filter out the value |
| 571 | delegate.nextToken(); |
| 572 | delegate.skipChildren(); |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame] | 573 | continue main_loop; |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 574 | } |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame] | 575 | _itemFilter = f; |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 576 | if (f == TokenFilter.INCLUDE_ALL) { |
Rafal Foltynski | 6feec20 | 2017-06-04 05:32:03 +0200 | [diff] [blame] | 577 | if (_verifyAllowedMatches() && _includePath) { |
Tatu Saloranta | cfe32d0 | 2015-04-21 23:17:22 -0700 | [diff] [blame] | 578 | return (_currToken = t); |
| 579 | } |
| 580 | // if (_includeImmediateParent) { ... |
| 581 | continue main_loop; |
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 | if (_includePath) { |
Cowtowncoder | b808f26 | 2015-04-23 15:47:39 -0700 | [diff] [blame] | 584 | t = _nextTokenWithBuffering(_headContext); |
| 585 | if (t != null) { |
| 586 | _currToken = t; |
| 587 | return t; |
| 588 | } |
Cowtowncoder | 654acf0 | 2015-04-16 15:30:43 -0700 | [diff] [blame] | 589 | } |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 590 | } |
Cowtowncoder | 654acf0 | 2015-04-16 15:30:43 -0700 | [diff] [blame] | 591 | continue main_loop; |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 592 | |
| 593 | default: // scalar value |
Cowtowncoder | c17c356 | 2015-04-16 15:47:28 -0700 | [diff] [blame] | 594 | f = _itemFilter; |
Cowtowncoder | 3222428 | 2015-04-16 16:39:22 -0700 | [diff] [blame] | 595 | if (f == TokenFilter.INCLUDE_ALL) { |
Tatu Saloranta | b1ac057 | 2015-04-15 22:35:00 -0700 | [diff] [blame] | 596 | return (_currToken = t); |
| 597 | } |
Cowtowncoder | 3222428 | 2015-04-16 16:39:22 -0700 | [diff] [blame] | 598 | if (f != null) { |
| 599 | f = _headContext.checkValue(f); |
| 600 | if ((f == TokenFilter.INCLUDE_ALL) |
| 601 | || ((f != null) && f.includeValue(delegate))) { |
Rafal Foltynski | 6feec20 | 2017-06-04 05:32:03 +0200 | [diff] [blame] | 602 | if (_verifyAllowedMatches()) { |
| 603 | return (_currToken = t); |
| 604 | } |
Cowtowncoder | 3222428 | 2015-04-16 16:39:22 -0700 | [diff] [blame] | 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; |
Rafal Foltynski | 6feec20 | 2017-06-04 05:32:03 +0200 | [diff] [blame] | 721 | if (f == TokenFilter.INCLUDE_ALL && _verifyAllowedMatches()) { |
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))) { |
Rafal Foltynski | 6feec20 | 2017-06-04 05:32:03 +0200 | [diff] [blame] | 736 | if (_verifyAllowedMatches()) { |
| 737 | return _nextBuffered(buffRoot); |
| 738 | } |
Cowtowncoder | 3222428 | 2015-04-16 16:39:22 -0700 | [diff] [blame] | 739 | } |
| 740 | } |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 741 | // Otherwise not included (leaves must be explicitly included) |
Cowtowncoder | 3222428 | 2015-04-16 16:39:22 -0700 | [diff] [blame] | 742 | continue main_loop; |
Tatu Saloranta | 404aebc | 2015-04-14 22:47:07 -0700 | [diff] [blame] | 743 | } |
| 744 | } |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Cowtowncoder | 7f7d10a | 2015-04-29 16:45:02 -0700 | [diff] [blame] | 747 | private JsonToken _nextBuffered(TokenFilterContext buffRoot) throws IOException |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame] | 748 | { |
Cowtowncoder | 7f7d10a | 2015-04-29 16:45:02 -0700 | [diff] [blame] | 749 | _exposedContext = buffRoot; |
| 750 | TokenFilterContext ctxt = buffRoot; |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame] | 751 | JsonToken t = ctxt.nextTokenToRead(); |
| 752 | if (t != null) { |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame] | 753 | return t; |
| 754 | } |
| 755 | while (true) { |
| 756 | // all done with buffered stuff? |
| 757 | if (ctxt == _headContext) { |
| 758 | throw _constructError("Internal error: failed to locate expected buffered tokens"); |
| 759 | /* |
| 760 | _exposedContext = null; |
| 761 | break; |
| 762 | */ |
| 763 | } |
| 764 | // If not, traverse down the context chain |
| 765 | ctxt = _exposedContext.findChildOf(ctxt); |
| 766 | _exposedContext = ctxt; |
| 767 | if (ctxt == null) { // should never occur |
| 768 | throw _constructError("Unexpected problem: chain of filtered context broken"); |
| 769 | } |
| 770 | t = _exposedContext.nextTokenToRead(); |
| 771 | if (t != null) { |
Tatu Saloranta | f6d583d | 2015-04-15 23:06:23 -0700 | [diff] [blame] | 772 | return t; |
| 773 | } |
| 774 | } |
| 775 | } |
Rafal Foltynski | 6feec20 | 2017-06-04 05:32:03 +0200 | [diff] [blame] | 776 | |
| 777 | private final boolean _verifyAllowedMatches() throws IOException { |
| 778 | if (_matchCount == 0 || _allowMultipleMatches) { |
| 779 | ++_matchCount; |
| 780 | return true; |
| 781 | } |
| 782 | return false; |
| 783 | } |
| 784 | |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 785 | @Override |
| 786 | public JsonToken nextValue() throws IOException { |
| 787 | // Re-implemented same as ParserMinimalBase: |
| 788 | JsonToken t = nextToken(); |
| 789 | if (t == JsonToken.FIELD_NAME) { |
| 790 | t = nextToken(); |
| 791 | } |
| 792 | return t; |
| 793 | } |
| 794 | |
| 795 | /** |
| 796 | * Need to override, re-implement similar to how method defined in |
| 797 | * {@link com.fasterxml.jackson.core.base.ParserMinimalBase}, to keep |
| 798 | * state correct here. |
| 799 | */ |
| 800 | @Override |
| 801 | public JsonParser skipChildren() throws IOException |
| 802 | { |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 803 | if ((_currToken != JsonToken.START_OBJECT) |
| 804 | && (_currToken != JsonToken.START_ARRAY)) { |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 805 | return this; |
| 806 | } |
| 807 | int open = 1; |
| 808 | |
| 809 | // Since proper matching of start/end markers is handled |
| 810 | // by nextToken(), we'll just count nesting levels here |
| 811 | while (true) { |
| 812 | JsonToken t = nextToken(); |
| 813 | if (t == null) { // not ideal but for now, just return |
| 814 | return this; |
| 815 | } |
| 816 | if (t.isStructStart()) { |
| 817 | ++open; |
| 818 | } else if (t.isStructEnd()) { |
| 819 | if (--open == 0) { |
| 820 | return this; |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | /********************************************************** |
| 828 | /* Public API, access to token information, text |
| 829 | /********************************************************** |
| 830 | */ |
| 831 | |
| 832 | @Override public String getText() throws IOException { return delegate.getText(); } |
| 833 | @Override public boolean hasTextCharacters() { return delegate.hasTextCharacters(); } |
| 834 | @Override public char[] getTextCharacters() throws IOException { return delegate.getTextCharacters(); } |
| 835 | @Override public int getTextLength() throws IOException { return delegate.getTextLength(); } |
| 836 | @Override public int getTextOffset() throws IOException { return delegate.getTextOffset(); } |
| 837 | |
| 838 | /* |
| 839 | /********************************************************** |
| 840 | /* Public API, access to token information, numeric |
| 841 | /********************************************************** |
| 842 | */ |
| 843 | |
| 844 | @Override |
| 845 | public BigInteger getBigIntegerValue() throws IOException { return delegate.getBigIntegerValue(); } |
| 846 | |
| 847 | @Override |
| 848 | public boolean getBooleanValue() throws IOException { return delegate.getBooleanValue(); } |
| 849 | |
| 850 | @Override |
| 851 | public byte getByteValue() throws IOException { return delegate.getByteValue(); } |
| 852 | |
| 853 | @Override |
| 854 | public short getShortValue() throws IOException { return delegate.getShortValue(); } |
| 855 | |
| 856 | @Override |
| 857 | public BigDecimal getDecimalValue() throws IOException { return delegate.getDecimalValue(); } |
| 858 | |
| 859 | @Override |
| 860 | public double getDoubleValue() throws IOException { return delegate.getDoubleValue(); } |
| 861 | |
| 862 | @Override |
| 863 | public float getFloatValue() throws IOException { return delegate.getFloatValue(); } |
| 864 | |
| 865 | @Override |
| 866 | public int getIntValue() throws IOException { return delegate.getIntValue(); } |
| 867 | |
| 868 | @Override |
| 869 | public long getLongValue() throws IOException { return delegate.getLongValue(); } |
| 870 | |
| 871 | @Override |
| 872 | public NumberType getNumberType() throws IOException { return delegate.getNumberType(); } |
| 873 | |
| 874 | @Override |
| 875 | public Number getNumberValue() throws IOException { return delegate.getNumberValue(); } |
| 876 | |
| 877 | /* |
| 878 | /********************************************************** |
| 879 | /* Public API, access to token information, coercion/conversion |
| 880 | /********************************************************** |
| 881 | */ |
| 882 | |
| 883 | @Override public int getValueAsInt() throws IOException { return delegate.getValueAsInt(); } |
| 884 | @Override public int getValueAsInt(int defaultValue) throws IOException { return delegate.getValueAsInt(defaultValue); } |
| 885 | @Override public long getValueAsLong() throws IOException { return delegate.getValueAsLong(); } |
| 886 | @Override public long getValueAsLong(long defaultValue) throws IOException { return delegate.getValueAsLong(defaultValue); } |
| 887 | @Override public double getValueAsDouble() throws IOException { return delegate.getValueAsDouble(); } |
| 888 | @Override public double getValueAsDouble(double defaultValue) throws IOException { return delegate.getValueAsDouble(defaultValue); } |
| 889 | @Override public boolean getValueAsBoolean() throws IOException { return delegate.getValueAsBoolean(); } |
| 890 | @Override public boolean getValueAsBoolean(boolean defaultValue) throws IOException { return delegate.getValueAsBoolean(defaultValue); } |
| 891 | @Override public String getValueAsString() throws IOException { return delegate.getValueAsString(); } |
| 892 | @Override public String getValueAsString(String defaultValue) throws IOException { return delegate.getValueAsString(defaultValue); } |
| 893 | |
| 894 | /* |
| 895 | /********************************************************** |
| 896 | /* Public API, access to token values, other |
| 897 | /********************************************************** |
| 898 | */ |
| 899 | |
| 900 | @Override public Object getEmbeddedObject() throws IOException { return delegate.getEmbeddedObject(); } |
| 901 | @Override public byte[] getBinaryValue(Base64Variant b64variant) throws IOException { return delegate.getBinaryValue(b64variant); } |
| 902 | @Override public int readBinaryValue(Base64Variant b64variant, OutputStream out) throws IOException { return delegate.readBinaryValue(b64variant, out); } |
| 903 | @Override public JsonLocation getTokenLocation() { return delegate.getTokenLocation(); } |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 904 | |
| 905 | /* |
| 906 | /********************************************************** |
| 907 | /* Internal helper methods |
| 908 | /********************************************************** |
| 909 | */ |
| 910 | |
| 911 | protected JsonStreamContext _filterContext() { |
| 912 | if (_exposedContext != null) { |
| 913 | return _exposedContext; |
| 914 | } |
| 915 | return _headContext; |
| 916 | } |
LokeshN | fd32772 | 2016-03-20 17:28:25 +0530 | [diff] [blame] | 917 | |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 918 | } |