blob: ae57e63009f82e1e8820c983ace64dfbfb575d4c [file] [log] [blame]
Tatu Salorantadfd69092015-04-09 22:29:34 -07001package com.fasterxml.jackson.core.filter;
2
3import java.io.IOException;
4import java.io.OutputStream;
5import java.math.BigDecimal;
6import java.math.BigInteger;
7
8import com.fasterxml.jackson.core.*;
9import com.fasterxml.jackson.core.util.JsonParserDelegate;
10
Tatu Salorantab46e0372015-04-14 21:43:03 -070011import static com.fasterxml.jackson.core.JsonTokenId.*;
12
Tatu Salorantadfd69092015-04-09 22:29:34 -070013/**
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 */
20public class FilteringParserDelegate extends JsonParserDelegate
21{
22 /*
23 /**********************************************************
24 /* Configuration
25 /**********************************************************
26 */
27
28 /**
29 * Object consulted to determine whether to write parts of content generator
30 * is asked to write or not.
31 */
32 protected TokenFilter rootFilter;
33
34 /**
35 * Flag that determines whether filtering will continue after the first
36 * match is indicated or not: if `false`, output is based on just the first
37 * full match (returning {@link TokenFilter#INCLUDE_ALL}) and no more
38 * checks are made; if `true` then filtering will be applied as necessary
39 * until end of content.
40 */
41 protected boolean _allowMultipleMatches;
42
43 /**
44 * Flag that determines whether path leading up to included content should
45 * also be automatically included or not. If `false`, no path inclusion is
46 * done and only explicitly included entries are output; if `true` then
47 * path from main level down to match is also included as necessary.
48 */
49 protected boolean _includePath;
50
51 /*
52 /**********************************************************
53 /* State
54 /**********************************************************
55 */
56
57 /**
58 * Last token retrieved via {@link #nextToken}, if any.
59 * Null before the first call to <code>nextToken()</code>,
60 * as well as if token has been explicitly cleared
61 */
62 protected JsonToken _currToken;
63
64 /**
65 * Last cleared token, if any: that is, value that was in
66 * effect when {@link #clearCurrentToken} was called.
67 */
68 protected JsonToken _lastClearedToken;
69
70 /**
Cowtowncoder00900312015-04-14 16:02:33 -070071 * During traversal this is the actual "open" parse tree, which sometimes
72 * is the same as {@link #_exposedContext}, and at other times is ahead
73 * of it. Note that this context is never null.
Tatu Salorantadfd69092015-04-09 22:29:34 -070074 */
Cowtowncoder00900312015-04-14 16:02:33 -070075 protected TokenFilterContext _headContext;
Tatu Salorantadfd69092015-04-09 22:29:34 -070076
77 /**
Cowtowncoder00900312015-04-14 16:02:33 -070078 * In cases where {@link #_headContext} is "ahead" of context exposed to
79 * caller, this context points to what is currently exposed to caller.
80 * When the two are in sync, this context reference will be <code>null</code>.
81 */
82 protected TokenFilterContext _exposedContext;
Cowtowncoder9a797fb2015-04-14 16:12:12 -070083
Cowtowncoder00900312015-04-14 16:02:33 -070084 /**
Tatu Salorantadfd69092015-04-09 22:29:34 -070085 * State that applies to the item within container, used where applicable.
86 * Specifically used to pass inclusion state between property name and
87 * property, and also used for array elements.
88 */
89 protected TokenFilter _itemFilter;
90
91 /**
92 * Number of tokens for which {@link TokenFilter#INCLUDE_ALL}
Tatu Saloranta5a5d1192015-04-13 23:05:07 -070093 * has been returned.
Tatu Salorantadfd69092015-04-09 22:29:34 -070094 */
95 protected int _matchCount;
96
97 /*
98 /**********************************************************
99 /* Construction, initialization
100 /**********************************************************
101 */
102
103 public FilteringParserDelegate(JsonParser p, TokenFilter f,
104 boolean includePath, boolean allowMultipleMatches)
105 {
106 super(p);
107 rootFilter = f;
108 // and this is the currently active filter for root values
109 _itemFilter = f;
Cowtowncoder00900312015-04-14 16:02:33 -0700110 _headContext = TokenFilterContext.createRootContext(f);
Tatu Salorantadfd69092015-04-09 22:29:34 -0700111 _includePath = includePath;
112 _allowMultipleMatches = allowMultipleMatches;
113 }
114
115 /*
116 /**********************************************************
117 /* Extended API
118 /**********************************************************
119 */
120
121 public TokenFilter getFilter() { return rootFilter; }
122
123 /**
124 * Accessor for finding number of matches, where specific token and sub-tree
125 * starting (if structured type) are passed.
126 */
127 public int getMatchCount() {
128 return _matchCount;
129 }
130
131 /*
132 /**********************************************************
133 /* Public API, token accessors
134 /**********************************************************
135 */
136
137 @Override public JsonToken getCurrentToken() { return _currToken; }
138
139 @Override public final int getCurrentTokenId() {
140 final JsonToken t = _currToken;
141 return (t == null) ? JsonTokenId.ID_NO_TOKEN : t.id();
142 }
143
144 @Override public boolean hasCurrentToken() { return _currToken != null; }
145 @Override public boolean hasTokenId(int id) {
146 final JsonToken t = _currToken;
147 if (t == null) {
148 return (JsonTokenId.ID_NO_TOKEN == id);
149 }
150 return t.id() == id;
151 }
152
153 @Override public final boolean hasToken(JsonToken t) {
154 return (_currToken == t);
155 }
156
157 @Override public boolean isExpectedStartArrayToken() { return _currToken == JsonToken.START_ARRAY; }
158 @Override public boolean isExpectedStartObjectToken() { return _currToken == JsonToken.START_OBJECT; }
159
160 @Override public JsonLocation getCurrentLocation() { return delegate.getCurrentLocation(); }
Tatu Salorantadfd69092015-04-09 22:29:34 -0700161
Cowtowncoder00900312015-04-14 16:02:33 -0700162 @Override
163 public JsonStreamContext getParsingContext() {
164 return _filterContext();
165 }
Tatu Salorantadfd69092015-04-09 22:29:34 -0700166
Cowtowncoder00900312015-04-14 16:02:33 -0700167 // !!! TODO: Verify it works as expected: copied from standard JSON parser impl
168 @Override
169 public String getCurrentName() throws IOException {
170 JsonStreamContext ctxt = _filterContext();
171 if (_currToken == JsonToken.START_OBJECT || _currToken == JsonToken.START_ARRAY) {
172 JsonStreamContext parent = ctxt.getParent();
173 return (parent == null) ? null : parent.getCurrentName();
174 }
175 return ctxt.getCurrentName();
176 }
177
Tatu Salorantadfd69092015-04-09 22:29:34 -0700178 /*
179 /**********************************************************
180 /* Public API, token state overrides
181 /**********************************************************
182 */
183
184 @Override
185 public void clearCurrentToken() {
186 if (_currToken != null) {
187 _lastClearedToken = _currToken;
188 _currToken = null;
189 }
190 }
191
192 @Override
193 public JsonToken getLastClearedToken() { return _lastClearedToken; }
194
Tatu Salorantadfd69092015-04-09 22:29:34 -0700195 @Override
Cowtowncoder00900312015-04-14 16:02:33 -0700196 public void overrideCurrentName(String name) {
197 /* 14-Apr-2015, tatu: Not sure whether this can be supported, and if so,
198 * what to do with it... Delegation won't work for sure, so let's for
199 * now throw an exception
200 */
201 throw new UnsupportedOperationException("Can not currently override name during filtering read");
202 }
Tatu Salorantadfd69092015-04-09 22:29:34 -0700203
204 /*
205 /**********************************************************
206 /* Public API, traversal
207 /**********************************************************
208 */
209
Cowtowncoder00900312015-04-14 16:02:33 -0700210 @Override
211 public JsonToken nextToken() throws IOException
212 {
Cowtowncoder9a797fb2015-04-14 16:12:12 -0700213 // Anything buffered?
Cowtowncoder37d0d6e2015-04-14 19:48:10 -0700214 TokenFilterContext ctxt = _exposedContext;
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700215
Cowtowncoder37d0d6e2015-04-14 19:48:10 -0700216 if (ctxt != null) {
217 while (true) {
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700218 JsonToken t = ctxt.nextTokenToRead();
Cowtowncoder37d0d6e2015-04-14 19:48:10 -0700219 if (t != null) {
220 _currToken = t;
221 return t;
222 }
223 // all done with buffered stuff?
224 if (ctxt == _headContext) {
225 _exposedContext = null;
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700226 // Almost! Most likely still have the current token;
227 // with the sole exception of
228 t = delegate.getCurrentToken();
229 if (t != JsonToken.FIELD_NAME) {
230 _currToken = t;
231 return t;
232 }
Cowtowncoder37d0d6e2015-04-14 19:48:10 -0700233 break;
234 }
235 // If not, traverse down the context chain
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700236 ctxt = _exposedContext.findChildOf(ctxt);
Cowtowncoder37d0d6e2015-04-14 19:48:10 -0700237 _exposedContext = ctxt;
238 if (ctxt == null) { // should never occur
239 throw _constructError("Unexpected problem: chain of filtered context broken");
240 }
241 }
242 }
243
Tatu Salorantab46e0372015-04-14 21:43:03 -0700244 // If not, need to read more. If we got any:
Cowtowncoder37d0d6e2015-04-14 19:48:10 -0700245 JsonToken t = delegate.nextToken();
246 if (t == null) {
Tatu Salorantab46e0372015-04-14 21:43:03 -0700247 // no strict need to close, since we have no state here
248 return (_currToken = t);
Cowtowncoder9a797fb2015-04-14 16:12:12 -0700249 }
Tatu Salorantace077d42015-04-14 20:48:23 -0700250
Tatu Salorantab46e0372015-04-14 21:43:03 -0700251 // otherwise... to include or not?
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700252 TokenFilter f;
253
254 switch (t.id()) {
Tatu Salorantab46e0372015-04-14 21:43:03 -0700255 case ID_START_ARRAY:
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700256 f = _itemFilter;
257 if (f == TokenFilter.INCLUDE_ALL) {
258 _headContext = _headContext.createChildArrayContext(f, true);
259 return (_currToken = t);
260 }
261 if (f == null) { // does this occur?
Tatu Salorantab46e0372015-04-14 21:43:03 -0700262 delegate.skipChildren();
263 break;
264 }
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700265 // Otherwise still iffy, need to check
266 f = _headContext.checkValue(f);
267 if (f == null) {
268 delegate.skipChildren();
269 break;
270 }
271 if (f != TokenFilter.INCLUDE_ALL) {
272 f = f.filterStartArray();
273 }
274 _itemFilter = f;
275 _headContext = _headContext.createChildArrayContext(f, true);
276 if (f == TokenFilter.INCLUDE_ALL) {
Tatu Salorantab46e0372015-04-14 21:43:03 -0700277 return (_currToken = t);
278 }
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700279 // but if we didn't figure it out yet, need to buffer possible events
280 return _nextTokenWithBuffering(_headContext);
Tatu Salorantab46e0372015-04-14 21:43:03 -0700281
282 case ID_START_OBJECT:
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700283 f = _itemFilter;
284 if (f == TokenFilter.INCLUDE_ALL) {
285 _headContext = _headContext.createChildObjectContext(f, true);
286 return (_currToken = t);
287 }
288 if (f == null) { // does this occur?
Tatu Salorantab46e0372015-04-14 21:43:03 -0700289 delegate.skipChildren();
290 break;
291 }
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700292 // Otherwise still iffy, need to check
293 f = _headContext.checkValue(f);
294 if (f == null) {
295 delegate.skipChildren();
296 break;
297 }
298 if (f != TokenFilter.INCLUDE_ALL) {
299 f = f.filterStartObject();
300 }
301 _itemFilter = f;
302 _headContext = _headContext.createChildObjectContext(f, true);
303 if (f == TokenFilter.INCLUDE_ALL) {
Tatu Salorantab46e0372015-04-14 21:43:03 -0700304 return (_currToken = t);
305 }
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700306 // but if we didn't figure it out yet, need to buffer possible events
307 return _nextTokenWithBuffering(_headContext);
Tatu Salorantab46e0372015-04-14 21:43:03 -0700308
309 case ID_END_ARRAY:
310 case ID_END_OBJECT:
311 {
312 boolean returnEnd = _headContext.isStartHandled();
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700313 f = _headContext.getFilter();
Tatu Salorantab46e0372015-04-14 21:43:03 -0700314 if ((f != null) && (f != TokenFilter.INCLUDE_ALL)) {
315 f.filterFinishArray();
316 }
317 _headContext = _headContext.getParent();
318 _itemFilter = _headContext.getFilter();
319 if (returnEnd) {
320 return (_currToken = t);
321 }
322 }
323 break;
324
325 case ID_FIELD_NAME:
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700326 {
327 final String name = delegate.getCurrentName();
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700328 // note: this will also set 'needToHandleName'
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700329 f = _headContext.setFieldName(name);
330 if (f == TokenFilter.INCLUDE_ALL) {
331 _itemFilter = f;
332 return (_currToken = t);
333 }
334 if (f == null) { // filter out the value
335 delegate.nextToken();
336 delegate.skipChildren();
337 break;
338 }
339 f = f.includeProperty(name);
340 if (f == null) { // filter out the value
341 delegate.nextToken();
342 delegate.skipChildren();
343 break;
344 }
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700345 _itemFilter = f;
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700346 if (f == TokenFilter.INCLUDE_ALL) {
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700347 return (_currToken = t);
348 }
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700349 return _nextTokenWithBuffering(_headContext);
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700350 }
Tatu Salorantab46e0372015-04-14 21:43:03 -0700351
352 default: // scalar value
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700353 if (_itemFilter == TokenFilter.INCLUDE_ALL) {
354 return (_currToken = t);
355 }
356 // Otherwise not included (leaves must be explicitly included)
357 break;
Tatu Salorantab46e0372015-04-14 21:43:03 -0700358 }
359
360 // We get here if token was not yet found; offlined handling
361 return _nextToken2();
362 }
363
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700364 /**
365 * Offlined handling for cases where there was no buffered token to
366 * return, and the token read next could not be returned as-is,
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700367 * at least not yet, but where we have not yet established that
368 * buffering is needed.
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700369 */
Tatu Salorantab46e0372015-04-14 21:43:03 -0700370 protected final JsonToken _nextToken2() throws IOException
371 {
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700372 main_loop:
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700373 while (true) {
374 JsonToken t = delegate.nextToken();
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700375
376 if (t == null) { // is this even legal?
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700377 return (_currToken = t);
378 }
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700379 TokenFilter f;
380
381 switch (t.id()) {
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700382 case ID_START_ARRAY:
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700383 f = _itemFilter;
384 if (f == TokenFilter.INCLUDE_ALL) {
385 _headContext = _headContext.createChildArrayContext(f, true);
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700386 return (_currToken = t);
387 }
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700388 if (f == null) { // does this occur?
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700389 delegate.skipChildren();
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700390 continue main_loop;
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700391 }
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700392 // Otherwise still iffy, need to check
393 f = _headContext.checkValue(f);
394 if (f == null) {
395 delegate.skipChildren();
396 continue main_loop;
397 }
398 if (f != TokenFilter.INCLUDE_ALL) {
399 f = f.filterStartArray();
400 }
401 _itemFilter = f;
402 _headContext = _headContext.createChildArrayContext(f, true);
403 if (f == TokenFilter.INCLUDE_ALL) {
404 return (_currToken = t);
405 }
406 // but if we didn't figure it out yet, need to buffer possible events
407 return _nextTokenWithBuffering(_headContext);
408
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700409 case ID_START_OBJECT:
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700410 f = _itemFilter;
411 if (f == TokenFilter.INCLUDE_ALL) {
412 _headContext = _headContext.createChildObjectContext(f, true);
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700413 return (_currToken = t);
414 }
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700415 if (f == null) { // does this occur?
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700416 delegate.skipChildren();
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700417 continue main_loop;
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700418 }
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700419 // Otherwise still iffy, need to check
420 f = _headContext.checkValue(f);
421 if (f == null) {
422 delegate.skipChildren();
423 continue main_loop;
424 }
425 if (f != TokenFilter.INCLUDE_ALL) {
426 f = f.filterStartObject();
427 }
428 _itemFilter = f;
429 _headContext = _headContext.createChildObjectContext(f, true);
430 if (f == TokenFilter.INCLUDE_ALL) {
431 return (_currToken = t);
432 }
433 // but if we didn't figure it out yet, need to buffer possible events
434 return _nextTokenWithBuffering(_headContext);
435
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700436 case ID_END_ARRAY:
437 case ID_END_OBJECT:
438 {
439 boolean returnEnd = _headContext.isStartHandled();
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700440 f = _headContext.getFilter();
441 if ((f != null) && (f != TokenFilter.INCLUDE_ALL)) {
442 f.filterFinishArray();
443 }
444 _headContext = _headContext.getParent();
445 _itemFilter = _headContext.getFilter();
446 if (returnEnd) {
447 return (_currToken = t);
448 }
449 }
450 continue main_loop;
451
452 case ID_FIELD_NAME:
453 {
454 final String name = delegate.getCurrentName();
455 f = _headContext.setFieldName(name);
456 if (f == TokenFilter.INCLUDE_ALL) {
457 _itemFilter = f;
458 return (_currToken = t);
459 }
460 if (f == null) { // filter out the value
461 delegate.nextToken();
462 delegate.skipChildren();
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700463 continue main_loop;
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700464 }
465 f = f.includeProperty(name);
466 if (f == null) { // filter out the value
467 delegate.nextToken();
468 delegate.skipChildren();
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700469 continue main_loop;
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700470 }
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700471 _itemFilter = f;
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700472 if (f == TokenFilter.INCLUDE_ALL) {
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700473 return (_currToken = t);
474 }
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700475 }
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700476 return _nextTokenWithBuffering(_headContext);
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700477
478 default: // scalar value
479 if (_itemFilter == TokenFilter.INCLUDE_ALL) {
480 return (_currToken = t);
481 }
482 // Otherwise not included (leaves must be explicitly included)
483 continue main_loop;
484 }
485 }
486 }
487
488 /**
489 * Method called when a new potentially included context is found.
490 */
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700491 protected final JsonToken _nextTokenWithBuffering(final TokenFilterContext buffRoot)
492 throws IOException
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700493 {
494 _exposedContext = _headContext;
495
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700496 main_loop:
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700497 while (true) {
498 JsonToken t = delegate.nextToken();
499
500 if (t == null) { // is this even legal?
501 return (_currToken = t);
502 }
503 TokenFilter f;
504
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700505 // One simplification here: we know for a fact that the item filter is
506 // neither null nor 'include all', for most cases; the only exception
507 // being FIELD_NAME handling
508
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700509 switch (t.id()) {
510 case ID_START_ARRAY:
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700511 f = _headContext.checkValue(_itemFilter);
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700512 if (f == null) {
513 delegate.skipChildren();
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700514 continue main_loop;
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700515 }
516 if (f != TokenFilter.INCLUDE_ALL) {
517 f = f.filterStartArray();
518 }
519 _itemFilter = f;
520 _headContext = _headContext.createChildArrayContext(f, true);
521 if (f == TokenFilter.INCLUDE_ALL) {
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700522 return _nextBuffered();
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700523 }
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700524 continue main_loop;
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700525
526 case ID_START_OBJECT:
527 f = _itemFilter;
528 if (f == TokenFilter.INCLUDE_ALL) {
529 _headContext = _headContext.createChildObjectContext(f, true);
530 return (_currToken = t);
531 }
532 if (f == null) { // does this occur?
533 delegate.skipChildren();
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700534 continue main_loop;
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700535 }
536 // Otherwise still iffy, need to check
537 f = _headContext.checkValue(f);
538 if (f == null) {
539 delegate.skipChildren();
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700540 continue main_loop;
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700541 }
542 if (f != TokenFilter.INCLUDE_ALL) {
543 f = f.filterStartObject();
544 }
545 _itemFilter = f;
546 _headContext = _headContext.createChildObjectContext(f, true);
547 if (f == TokenFilter.INCLUDE_ALL) {
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700548 return _nextBuffered();
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700549 }
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700550 continue main_loop;
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700551
552 case ID_END_ARRAY:
553 case ID_END_OBJECT:
554 {
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700555 // Unlike with other loops, here we know that content was NOT
556 // included (won't get this far otherwise)
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700557 f = _headContext.getFilter();
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700558 if ((f != null) && (f != TokenFilter.INCLUDE_ALL)) {
559 f.filterFinishArray();
560 }
561 _headContext = _headContext.getParent();
562 _itemFilter = _headContext.getFilter();
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700563
564 if (_headContext == buffRoot) {
565 // !!! TBI
566 throw _constructError("Internal error: end of possible inclusion -- TBI");
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700567 }
568 }
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700569 continue main_loop;
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700570
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700571 case ID_FIELD_NAME:
572 {
573 final String name = delegate.getCurrentName();
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700574 f = _headContext.setFieldName(name);
575 if (f == TokenFilter.INCLUDE_ALL) {
576 _itemFilter = f;
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700577 return _nextBuffered();
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700578 }
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700579 if (f == null) { // filter out the value
580 delegate.nextToken();
581 delegate.skipChildren();
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700582 continue main_loop;
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700583 }
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700584 f = f.includeProperty(name);
585 if (f == null) { // filter out the value
586 delegate.nextToken();
587 delegate.skipChildren();
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700588 continue main_loop;
Tatu Salorantab1ac0572015-04-15 22:35:00 -0700589 }
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700590 _itemFilter = f;
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700591 if (f == TokenFilter.INCLUDE_ALL) {
592 return _nextBuffered();
593 }
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700594 }
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700595 continue main_loop;
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700596
597 default: // scalar value
598 if (_itemFilter == TokenFilter.INCLUDE_ALL) {
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700599 return _nextBuffered();
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700600 }
601 // Otherwise not included (leaves must be explicitly included)
Tatu Saloranta404aebc2015-04-14 22:47:07 -0700602 }
603 }
Tatu Salorantaf6d583d2015-04-15 23:06:23 -0700604 }
605
606 private JsonToken _nextBuffered() throws IOException
607 {
608 TokenFilterContext ctxt = _exposedContext;
609 JsonToken t = ctxt.nextTokenToRead();
610 if (t != null) {
611 _currToken = t;
612 return t;
613 }
614 while (true) {
615 // all done with buffered stuff?
616 if (ctxt == _headContext) {
617 throw _constructError("Internal error: failed to locate expected buffered tokens");
618 /*
619 _exposedContext = null;
620 break;
621 */
622 }
623 // If not, traverse down the context chain
624 ctxt = _exposedContext.findChildOf(ctxt);
625 _exposedContext = ctxt;
626 if (ctxt == null) { // should never occur
627 throw _constructError("Unexpected problem: chain of filtered context broken");
628 }
629 t = _exposedContext.nextTokenToRead();
630 if (t != null) {
631 _currToken = t;
632 return t;
633 }
634 }
635 }
Tatu Salorantab46e0372015-04-14 21:43:03 -0700636
Tatu Salorantadfd69092015-04-09 22:29:34 -0700637 @Override
638 public JsonToken nextValue() throws IOException {
639 // Re-implemented same as ParserMinimalBase:
640 JsonToken t = nextToken();
641 if (t == JsonToken.FIELD_NAME) {
642 t = nextToken();
643 }
644 return t;
645 }
646
647 /**
648 * Need to override, re-implement similar to how method defined in
649 * {@link com.fasterxml.jackson.core.base.ParserMinimalBase}, to keep
650 * state correct here.
651 */
652 @Override
653 public JsonParser skipChildren() throws IOException
654 {
Cowtowncoder00900312015-04-14 16:02:33 -0700655 if ((_currToken != JsonToken.START_OBJECT)
656 && (_currToken != JsonToken.START_ARRAY)) {
Tatu Salorantadfd69092015-04-09 22:29:34 -0700657 return this;
658 }
659 int open = 1;
660
661 // Since proper matching of start/end markers is handled
662 // by nextToken(), we'll just count nesting levels here
663 while (true) {
664 JsonToken t = nextToken();
665 if (t == null) { // not ideal but for now, just return
666 return this;
667 }
668 if (t.isStructStart()) {
669 ++open;
670 } else if (t.isStructEnd()) {
671 if (--open == 0) {
672 return this;
673 }
674 }
675 }
676 }
677
678 /*
679 /**********************************************************
680 /* Public API, access to token information, text
681 /**********************************************************
682 */
683
684 @Override public String getText() throws IOException { return delegate.getText(); }
685 @Override public boolean hasTextCharacters() { return delegate.hasTextCharacters(); }
686 @Override public char[] getTextCharacters() throws IOException { return delegate.getTextCharacters(); }
687 @Override public int getTextLength() throws IOException { return delegate.getTextLength(); }
688 @Override public int getTextOffset() throws IOException { return delegate.getTextOffset(); }
689
690 /*
691 /**********************************************************
692 /* Public API, access to token information, numeric
693 /**********************************************************
694 */
695
696 @Override
697 public BigInteger getBigIntegerValue() throws IOException { return delegate.getBigIntegerValue(); }
698
699 @Override
700 public boolean getBooleanValue() throws IOException { return delegate.getBooleanValue(); }
701
702 @Override
703 public byte getByteValue() throws IOException { return delegate.getByteValue(); }
704
705 @Override
706 public short getShortValue() throws IOException { return delegate.getShortValue(); }
707
708 @Override
709 public BigDecimal getDecimalValue() throws IOException { return delegate.getDecimalValue(); }
710
711 @Override
712 public double getDoubleValue() throws IOException { return delegate.getDoubleValue(); }
713
714 @Override
715 public float getFloatValue() throws IOException { return delegate.getFloatValue(); }
716
717 @Override
718 public int getIntValue() throws IOException { return delegate.getIntValue(); }
719
720 @Override
721 public long getLongValue() throws IOException { return delegate.getLongValue(); }
722
723 @Override
724 public NumberType getNumberType() throws IOException { return delegate.getNumberType(); }
725
726 @Override
727 public Number getNumberValue() throws IOException { return delegate.getNumberValue(); }
728
729 /*
730 /**********************************************************
731 /* Public API, access to token information, coercion/conversion
732 /**********************************************************
733 */
734
735 @Override public int getValueAsInt() throws IOException { return delegate.getValueAsInt(); }
736 @Override public int getValueAsInt(int defaultValue) throws IOException { return delegate.getValueAsInt(defaultValue); }
737 @Override public long getValueAsLong() throws IOException { return delegate.getValueAsLong(); }
738 @Override public long getValueAsLong(long defaultValue) throws IOException { return delegate.getValueAsLong(defaultValue); }
739 @Override public double getValueAsDouble() throws IOException { return delegate.getValueAsDouble(); }
740 @Override public double getValueAsDouble(double defaultValue) throws IOException { return delegate.getValueAsDouble(defaultValue); }
741 @Override public boolean getValueAsBoolean() throws IOException { return delegate.getValueAsBoolean(); }
742 @Override public boolean getValueAsBoolean(boolean defaultValue) throws IOException { return delegate.getValueAsBoolean(defaultValue); }
743 @Override public String getValueAsString() throws IOException { return delegate.getValueAsString(); }
744 @Override public String getValueAsString(String defaultValue) throws IOException { return delegate.getValueAsString(defaultValue); }
745
746 /*
747 /**********************************************************
748 /* Public API, access to token values, other
749 /**********************************************************
750 */
751
752 @Override public Object getEmbeddedObject() throws IOException { return delegate.getEmbeddedObject(); }
753 @Override public byte[] getBinaryValue(Base64Variant b64variant) throws IOException { return delegate.getBinaryValue(b64variant); }
754 @Override public int readBinaryValue(Base64Variant b64variant, OutputStream out) throws IOException { return delegate.readBinaryValue(b64variant, out); }
755 @Override public JsonLocation getTokenLocation() { return delegate.getTokenLocation(); }
Cowtowncoder00900312015-04-14 16:02:33 -0700756
757 /*
758 /**********************************************************
759 /* Internal helper methods
760 /**********************************************************
761 */
762
763 protected JsonStreamContext _filterContext() {
764 if (_exposedContext != null) {
765 return _exposedContext;
766 }
767 return _headContext;
768 }
769
Tatu Salorantadfd69092015-04-09 22:29:34 -0700770}