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; |
| 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 | /** |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 71 | * 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 Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 74 | */ |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 75 | protected TokenFilterContext _headContext; |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 78 | * 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; |
Cowtowncoder | 9a797fb | 2015-04-14 16:12:12 -0700 | [diff] [blame] | 83 | |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 84 | /** |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 85 | * 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 Saloranta | 5a5d119 | 2015-04-13 23:05:07 -0700 | [diff] [blame] | 93 | * has been returned. |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 94 | */ |
| 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; |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 110 | _headContext = TokenFilterContext.createRootContext(f); |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 111 | _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 Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 161 | |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 162 | @Override |
| 163 | public JsonStreamContext getParsingContext() { |
| 164 | return _filterContext(); |
| 165 | } |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 166 | |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 167 | // !!! 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 Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 178 | /* |
| 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 Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 195 | @Override |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 196 | 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 Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | /********************************************************** |
| 206 | /* Public API, traversal |
| 207 | /********************************************************** |
| 208 | */ |
| 209 | |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 210 | @Override |
| 211 | public JsonToken nextToken() throws IOException |
| 212 | { |
Cowtowncoder | 9a797fb | 2015-04-14 16:12:12 -0700 | [diff] [blame] | 213 | // Anything buffered? |
Cowtowncoder | 37d0d6e | 2015-04-14 19:48:10 -0700 | [diff] [blame] | 214 | 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 Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame^] | 236 | // If not, need to read more. If we got any: |
Cowtowncoder | 37d0d6e | 2015-04-14 19:48:10 -0700 | [diff] [blame] | 237 | JsonToken t = delegate.nextToken(); |
| 238 | if (t == null) { |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame^] | 239 | // no strict need to close, since we have no state here |
| 240 | return (_currToken = t); |
Cowtowncoder | 9a797fb | 2015-04-14 16:12:12 -0700 | [diff] [blame] | 241 | } |
Tatu Saloranta | ce077d4 | 2015-04-14 20:48:23 -0700 | [diff] [blame] | 242 | |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame^] | 243 | // 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 Saloranta | ce077d4 | 2015-04-14 20:48:23 -0700 | [diff] [blame] | 294 | // !!! TODO |
| 295 | return null; |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 296 | } |
Tatu Saloranta | b46e037 | 2015-04-14 21:43:03 -0700 | [diff] [blame^] | 297 | |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 298 | @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 | { |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 316 | if ((_currToken != JsonToken.START_OBJECT) |
| 317 | && (_currToken != JsonToken.START_ARRAY)) { |
Tatu Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 318 | 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(); } |
Cowtowncoder | 0090031 | 2015-04-14 16:02:33 -0700 | [diff] [blame] | 417 | |
| 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 Saloranta | dfd6909 | 2015-04-09 22:29:34 -0700 | [diff] [blame] | 431 | } |