public TokenFilterContext createChildArrayContext(TokenFilter filter, boolean writeStart) { TokenFilterContext ctxt = _child; if (ctxt == null) { _child = ctxt = new TokenFilterContext(TYPE_ARRAY, this, filter, writeStart); return ctxt; } return ctxt.reset(TYPE_ARRAY, filter, writeStart); }
/** * Method called to ensure that parent path from root is written up to * and including this node. */ public void writePath(JsonGenerator gen) throws IOException { if ((_filter == null) || (_filter == TokenFilter.INCLUDE_ALL)) { return; } if (_parent != null) { _parent._writePath(gen); } if (_startHandled) { // even if Object started, need to start leaf-level name if (_needToHandleName) { gen.writeFieldName(_currentName); } } else { _startHandled = true; if (_type == TYPE_OBJECT) { gen.writeStartObject(); gen.writeFieldName(_currentName); // we know name must be written } else if (_type == TYPE_ARRAY) { gen.writeStartArray(); } } }
protected void appendDesc(StringBuilder sb) { if (_parent != null) { _parent.appendDesc(sb); } if (_type == TYPE_OBJECT) { sb.append('{'); if (_currentName != null) { sb.append('"'); // !!! TODO: Name chars should be escaped? sb.append(_currentName); sb.append('"'); } else { sb.append('?'); } sb.append('}'); } else if (_type == TYPE_ARRAY) { sb.append('['); sb.append(getCurrentIndex()); sb.append(']'); } else { // nah, ROOT: sb.append("/"); } }
@Override public void writeEndArray() throws IOException { _filterContext = _filterContext.closeArray(delegate); if (_filterContext != null) { _itemFilter = _filterContext.getFilter(); } }
@Override public void writeEndObject() throws IOException { _filterContext = _filterContext.closeObject(delegate); if (_filterContext != null) { _itemFilter = _filterContext.getFilter(); } }
if (_currToken.isScalarValue() && !_headContext.isStartHandled() && !_includePath && (_itemFilter == TokenFilter.INCLUDE_ALL)) { return (_currToken = null); JsonToken t = ctxt.nextTokenToRead(); if (t != null) { _currToken = t; if (ctxt.inArray()) { t = delegate.getCurrentToken(); ctxt = _headContext.findChildOf(ctxt); _exposedContext = ctxt; if (ctxt == null) { // should never occur f = _itemFilter; if (f == TokenFilter.INCLUDE_ALL) { _headContext = _headContext.createChildArrayContext(f, true); return (_currToken = t); f = _headContext.checkValue(f); if (f == null) { delegate.skipChildren(); _headContext = _headContext.createChildArrayContext(f, true); return (_currToken = t); _headContext = _headContext.createChildArrayContext(f, false);
f = _itemFilter; if (f == TokenFilter.INCLUDE_ALL) { _headContext = _headContext.createChildArrayContext(f, true); return (_currToken = t); f = _headContext.checkValue(f); if (f == null) { delegate.skipChildren(); _headContext = _headContext.createChildArrayContext(f, true); return (_currToken = t); _headContext = _headContext.createChildArrayContext(f, false); f = _itemFilter; if (f == TokenFilter.INCLUDE_ALL) { _headContext = _headContext.createChildObjectContext(f, true); return (_currToken = t); f = _headContext.checkValue(f); if (f == null) { delegate.skipChildren(); _headContext = _headContext.createChildObjectContext(f, true); return (_currToken = t); _headContext = _headContext.createChildObjectContext(f, false); if (_includePath) { t = _nextTokenWithBuffering(_headContext);
@Override public void writeStartObject() throws IOException { if (_itemFilter == null) { _filterContext = _filterContext.createChildObjectContext(_itemFilter, false); return; } if (_itemFilter == TokenFilter.INCLUDE_ALL) { _filterContext = _filterContext.createChildObjectContext(_itemFilter, true); delegate.writeStartObject(); return; } TokenFilter f = _filterContext.checkValue(_itemFilter); if (f == null) { return; } if (f != TokenFilter.INCLUDE_ALL) { f = f.filterStartObject(); } if (f == TokenFilter.INCLUDE_ALL) { _checkParentPath(); _filterContext = _filterContext.createChildObjectContext(f, true); delegate.writeStartObject(); } else { // filter out _filterContext = _filterContext.createChildObjectContext(f, false); } }
@Override public void writeStartArray(int size) throws IOException { if (_itemFilter == null) { _filterContext = _filterContext.createChildArrayContext(null, false); return; } if (_itemFilter == TokenFilter.INCLUDE_ALL) { _filterContext = _filterContext.createChildArrayContext(_itemFilter, true); delegate.writeStartArray(size); return; } _itemFilter = _filterContext.checkValue(_itemFilter); if (_itemFilter == null) { _filterContext = _filterContext.createChildArrayContext(null, false); return; } if (_itemFilter != TokenFilter.INCLUDE_ALL) { _itemFilter = _itemFilter.filterStartArray(); } if (_itemFilter == TokenFilter.INCLUDE_ALL) { _checkParentPath(); _filterContext = _filterContext.createChildArrayContext(_itemFilter, true); delegate.writeStartArray(size); } else { _filterContext = _filterContext.createChildArrayContext(_itemFilter, false); } }
@Override public void writeString(char[] text, int offset, int len) throws IOException { if (_itemFilter == null) { return; } if (_itemFilter != TokenFilter.INCLUDE_ALL) { String value = new String(text, offset, len); TokenFilter state = _filterContext.checkValue(_itemFilter); if (state == null) { return; } if (state != TokenFilter.INCLUDE_ALL) { if (!state.includeString(value)) { return; } } _checkParentPath(); } delegate.writeString(text, offset, len); }
private JsonToken _nextBuffered(TokenFilterContext buffRoot) throws IOException { _exposedContext = buffRoot; TokenFilterContext ctxt = buffRoot; JsonToken t = ctxt.nextTokenToRead(); if (t != null) { return t; } while (true) { // all done with buffered stuff? if (ctxt == _headContext) { throw _constructError("Internal error: failed to locate expected buffered tokens"); /* _exposedContext = null; break; */ } // If not, traverse down the context chain ctxt = _exposedContext.findChildOf(ctxt); _exposedContext = ctxt; if (ctxt == null) { // should never occur throw _constructError("Unexpected problem: chain of filtered context broken"); } t = _exposedContext.nextTokenToRead(); if (t != null) { return t; } } }
public FilteringParserDelegate(JsonParser p, TokenFilter f, boolean includePath, boolean allowMultipleMatches) { super(p); rootFilter = f; // and this is the currently active filter for root values _itemFilter = f; _headContext = TokenFilterContext.createRootContext(f); _includePath = includePath; _allowMultipleMatches = allowMultipleMatches; }
/** * Overridden to provide developer writeable "JsonPath" representation * of the context. */ @Override public String toString() { StringBuilder sb = new StringBuilder(64); appendDesc(sb); return sb.toString(); } }
public static TokenFilterContext createRootContext(TokenFilter filter) { // true -> since we have no start/end marker, consider start handled return new TokenFilterContext(TYPE_ROOT, null, filter, true); }
if (_currToken.isScalarValue() && !_headContext.isStartHandled() && !_includePath && (_itemFilter == TokenFilter.INCLUDE_ALL)) { return (_currToken = null); JsonToken t = ctxt.nextTokenToRead(); if (t != null) { _currToken = t; if (ctxt.inArray()) { t = delegate.currentToken(); ctxt = _headContext.findChildOf(ctxt); _exposedContext = ctxt; if (ctxt == null) { // should never occur f = _itemFilter; if (f == TokenFilter.INCLUDE_ALL) { _headContext = _headContext.createChildArrayContext(f, true); return (_currToken = t); f = _headContext.checkValue(f); if (f == null) { delegate.skipChildren(); _headContext = _headContext.createChildArrayContext(f, true); return (_currToken = t); _headContext = _headContext.createChildArrayContext(f, false);
f = _headContext.checkValue(_itemFilter); if (f == null) { delegate.skipChildren(); _headContext = _headContext.createChildArrayContext(f, true); return _nextBuffered(buffRoot); _headContext = _headContext.createChildArrayContext(f, false); continue main_loop; f = _itemFilter; if (f == TokenFilter.INCLUDE_ALL) { _headContext = _headContext.createChildObjectContext(f, true); return t; f = _headContext.checkValue(f); if (f == null) { delegate.skipChildren(); _headContext = _headContext.createChildObjectContext(f, true); return _nextBuffered(buffRoot); _headContext = _headContext.createChildObjectContext(f, false); continue main_loop; f = _headContext.getFilter(); if ((f != null) && (f != TokenFilter.INCLUDE_ALL)) { f.filterFinishArray();
@Override public void writeStartObject(Object forValue) throws IOException { if (_itemFilter == null) { _filterContext = _filterContext.createChildObjectContext(_itemFilter, false); return; } if (_itemFilter == TokenFilter.INCLUDE_ALL) { _filterContext = _filterContext.createChildObjectContext(_itemFilter, true); delegate.writeStartObject(forValue); return; } TokenFilter f = _filterContext.checkValue(_itemFilter); if (f == null) { return; } if (f != TokenFilter.INCLUDE_ALL) { f = f.filterStartObject(); } if (f == TokenFilter.INCLUDE_ALL) { _checkParentPath(); _filterContext = _filterContext.createChildObjectContext(f, true); delegate.writeStartObject(forValue); } else { // filter out _filterContext = _filterContext.createChildObjectContext(f, false); } }
_filterContext = _filterContext.createChildArrayContext(null, false); return; _filterContext = _filterContext.createChildArrayContext(_itemFilter, true); delegate.writeStartArray(); return; _itemFilter = _filterContext.checkValue(_itemFilter); if (_itemFilter == null) { _filterContext = _filterContext.createChildArrayContext(null, false); return; _filterContext = _filterContext.createChildArrayContext(_itemFilter, true); delegate.writeStartArray(); } else { _filterContext = _filterContext.createChildArrayContext(_itemFilter, false);
@Override public void writeNumber(long v) throws IOException { if (_itemFilter == null) { return; } if (_itemFilter != TokenFilter.INCLUDE_ALL) { TokenFilter state = _filterContext.checkValue(_itemFilter); if (state == null) { return; } if (state != TokenFilter.INCLUDE_ALL) { if (!state.includeNumber(v)) { return; } } _checkParentPath(); } delegate.writeNumber(v); }
@Override public void writeEndArray() throws IOException { _filterContext = _filterContext.closeArray(delegate); if (_filterContext != null) { _itemFilter = _filterContext.getFilter(); } }