blob: ae26808d3572f538d760861a75c7a7d54a685d7f [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;
215 if (ctxt != null) {
216 while (true) {
217 JsonToken t = _exposedContext.nextTokenToRead(_currToken);
218 if (t != null) {
219 _currToken = t;
220 return t;
221 }
222 // all done with buffered stuff?
223 if (ctxt == _headContext) {
224 _exposedContext = null;
225 break;
226 }
227 // If not, traverse down the context chain
228 ctxt = _exposedContext.findChildOf(_exposedContext);
229 _exposedContext = ctxt;
230 if (ctxt == null) { // should never occur
231 throw _constructError("Unexpected problem: chain of filtered context broken");
232 }
233 }
234 }
235
Tatu Salorantab46e0372015-04-14 21:43:03 -0700236 // If not, need to read more. If we got any:
Cowtowncoder37d0d6e2015-04-14 19:48:10 -0700237 JsonToken t = delegate.nextToken();
238 if (t == null) {
Tatu Salorantab46e0372015-04-14 21:43:03 -0700239 // no strict need to close, since we have no state here
240 return (_currToken = t);
Cowtowncoder9a797fb2015-04-14 16:12:12 -0700241 }
Tatu Salorantace077d42015-04-14 20:48:23 -0700242
Tatu Salorantab46e0372015-04-14 21:43:03 -0700243 // otherwise... to include or not?
244 switch (_currToken.id()) {
245 case ID_START_ARRAY:
246 if (_itemFilter == null) {
247 delegate.skipChildren();
248 break;
249 }
250 if (_itemFilter == TokenFilter.INCLUDE_ALL) {
251 _headContext = _headContext.createChildArrayContext(_itemFilter, true);
252 return (_currToken = t);
253 }
254 // TODO
255
256 case ID_START_OBJECT:
257 if (_itemFilter == null) {
258 delegate.skipChildren();
259 break;
260 }
261 if (_itemFilter == TokenFilter.INCLUDE_ALL) {
262 _headContext = _headContext.createChildObjectContext(_itemFilter, true);
263 return (_currToken = t);
264 }
265 // TODO
266
267 case ID_END_ARRAY:
268 case ID_END_OBJECT:
269 {
270 boolean returnEnd = _headContext.isStartHandled();
271 TokenFilter f = _headContext.getFilter();
272 if ((f != null) && (f != TokenFilter.INCLUDE_ALL)) {
273 f.filterFinishArray();
274 }
275 _headContext = _headContext.getParent();
276 _itemFilter = _headContext.getFilter();
277 if (returnEnd) {
278 return (_currToken = t);
279 }
280 }
281 break;
282
283 case ID_FIELD_NAME:
284
285 default: // scalar value
286 }
287
288 // We get here if token was not yet found; offlined handling
289 return _nextToken2();
290 }
291
292 protected final JsonToken _nextToken2() throws IOException
293 {
Tatu Salorantace077d42015-04-14 20:48:23 -0700294 // !!! TODO
295 return null;
Cowtowncoder00900312015-04-14 16:02:33 -0700296 }
Tatu Salorantab46e0372015-04-14 21:43:03 -0700297
Tatu Salorantadfd69092015-04-09 22:29:34 -0700298 @Override
299 public JsonToken nextValue() throws IOException {
300 // Re-implemented same as ParserMinimalBase:
301 JsonToken t = nextToken();
302 if (t == JsonToken.FIELD_NAME) {
303 t = nextToken();
304 }
305 return t;
306 }
307
308 /**
309 * Need to override, re-implement similar to how method defined in
310 * {@link com.fasterxml.jackson.core.base.ParserMinimalBase}, to keep
311 * state correct here.
312 */
313 @Override
314 public JsonParser skipChildren() throws IOException
315 {
Cowtowncoder00900312015-04-14 16:02:33 -0700316 if ((_currToken != JsonToken.START_OBJECT)
317 && (_currToken != JsonToken.START_ARRAY)) {
Tatu Salorantadfd69092015-04-09 22:29:34 -0700318 return this;
319 }
320 int open = 1;
321
322 // Since proper matching of start/end markers is handled
323 // by nextToken(), we'll just count nesting levels here
324 while (true) {
325 JsonToken t = nextToken();
326 if (t == null) { // not ideal but for now, just return
327 return this;
328 }
329 if (t.isStructStart()) {
330 ++open;
331 } else if (t.isStructEnd()) {
332 if (--open == 0) {
333 return this;
334 }
335 }
336 }
337 }
338
339 /*
340 /**********************************************************
341 /* Public API, access to token information, text
342 /**********************************************************
343 */
344
345 @Override public String getText() throws IOException { return delegate.getText(); }
346 @Override public boolean hasTextCharacters() { return delegate.hasTextCharacters(); }
347 @Override public char[] getTextCharacters() throws IOException { return delegate.getTextCharacters(); }
348 @Override public int getTextLength() throws IOException { return delegate.getTextLength(); }
349 @Override public int getTextOffset() throws IOException { return delegate.getTextOffset(); }
350
351 /*
352 /**********************************************************
353 /* Public API, access to token information, numeric
354 /**********************************************************
355 */
356
357 @Override
358 public BigInteger getBigIntegerValue() throws IOException { return delegate.getBigIntegerValue(); }
359
360 @Override
361 public boolean getBooleanValue() throws IOException { return delegate.getBooleanValue(); }
362
363 @Override
364 public byte getByteValue() throws IOException { return delegate.getByteValue(); }
365
366 @Override
367 public short getShortValue() throws IOException { return delegate.getShortValue(); }
368
369 @Override
370 public BigDecimal getDecimalValue() throws IOException { return delegate.getDecimalValue(); }
371
372 @Override
373 public double getDoubleValue() throws IOException { return delegate.getDoubleValue(); }
374
375 @Override
376 public float getFloatValue() throws IOException { return delegate.getFloatValue(); }
377
378 @Override
379 public int getIntValue() throws IOException { return delegate.getIntValue(); }
380
381 @Override
382 public long getLongValue() throws IOException { return delegate.getLongValue(); }
383
384 @Override
385 public NumberType getNumberType() throws IOException { return delegate.getNumberType(); }
386
387 @Override
388 public Number getNumberValue() throws IOException { return delegate.getNumberValue(); }
389
390 /*
391 /**********************************************************
392 /* Public API, access to token information, coercion/conversion
393 /**********************************************************
394 */
395
396 @Override public int getValueAsInt() throws IOException { return delegate.getValueAsInt(); }
397 @Override public int getValueAsInt(int defaultValue) throws IOException { return delegate.getValueAsInt(defaultValue); }
398 @Override public long getValueAsLong() throws IOException { return delegate.getValueAsLong(); }
399 @Override public long getValueAsLong(long defaultValue) throws IOException { return delegate.getValueAsLong(defaultValue); }
400 @Override public double getValueAsDouble() throws IOException { return delegate.getValueAsDouble(); }
401 @Override public double getValueAsDouble(double defaultValue) throws IOException { return delegate.getValueAsDouble(defaultValue); }
402 @Override public boolean getValueAsBoolean() throws IOException { return delegate.getValueAsBoolean(); }
403 @Override public boolean getValueAsBoolean(boolean defaultValue) throws IOException { return delegate.getValueAsBoolean(defaultValue); }
404 @Override public String getValueAsString() throws IOException { return delegate.getValueAsString(); }
405 @Override public String getValueAsString(String defaultValue) throws IOException { return delegate.getValueAsString(defaultValue); }
406
407 /*
408 /**********************************************************
409 /* Public API, access to token values, other
410 /**********************************************************
411 */
412
413 @Override public Object getEmbeddedObject() throws IOException { return delegate.getEmbeddedObject(); }
414 @Override public byte[] getBinaryValue(Base64Variant b64variant) throws IOException { return delegate.getBinaryValue(b64variant); }
415 @Override public int readBinaryValue(Base64Variant b64variant, OutputStream out) throws IOException { return delegate.readBinaryValue(b64variant, out); }
416 @Override public JsonLocation getTokenLocation() { return delegate.getTokenLocation(); }
Cowtowncoder00900312015-04-14 16:02:33 -0700417
418 /*
419 /**********************************************************
420 /* Internal helper methods
421 /**********************************************************
422 */
423
424 protected JsonStreamContext _filterContext() {
425 if (_exposedContext != null) {
426 return _exposedContext;
427 }
428 return _headContext;
429 }
430
Tatu Salorantadfd69092015-04-09 22:29:34 -0700431}