From c13ed81d5aaa552cb95b1cde76ede2d9ed1457fe Mon Sep 17 00:00:00 2001 From: Konrad Beiske Date: Fri, 15 Jan 2016 17:47:55 +0100 Subject: [PATCH] Changes after review --- .../common/xcontent/XContent.java | 18 ++++++++++++++---- .../common/xcontent/XContentBuilder.java | 6 ++---- .../common/xcontent/XContentGenerator.java | 2 -- .../common/xcontent/cbor/CborXContent.java | 10 ---------- .../xcontent/cbor/CborXContentGenerator.java | 7 +++---- .../common/xcontent/json/JsonXContent.java | 14 ++------------ .../xcontent/json/JsonXContentGenerator.java | 4 ---- .../common/xcontent/smile/SmileXContent.java | 10 ---------- .../xcontent/smile/SmileXContentGenerator.java | 7 +++---- .../filtering/FilterPathBasedFilter.java | 2 +- .../common/xcontent/yaml/YamlXContent.java | 11 ----------- .../xcontent/yaml/YamlXContentGenerator.java | 6 +++--- .../FilterPathGeneratorFilteringTests.java | 9 +++++++-- 13 files changed, 35 insertions(+), 71 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContent.java index 5ee85c4df61..35579965f30 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContent.java @@ -41,18 +41,28 @@ public interface XContent { /** * Creates a new generator using the provided output stream. */ - XContentGenerator createGenerator(OutputStream os) throws IOException; + default XContentGenerator createGenerator(OutputStream os) throws IOException { + return createGenerator(os, null, true); + } /** - * Creates a new generator using the provided output stream and some filters. + * Creates a new generator using the provided output stream and some + * inclusive filters. Same as createGenerator(os, filters, true). */ - XContentGenerator createGenerator(OutputStream os, String[] filters) throws IOException; + default XContentGenerator createGenerator(OutputStream os, String[] filters) throws IOException { + return createGenerator(os, filters, true); + } /** * Creates a new generator using the provided output stream and some * filters. + * + * @param inclusive + * If true only paths matching a filter will be included in + * output. If false no path matching a filter will be included in + * output */ - XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException; + XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusive) throws IOException; /** * Creates a parser over the provided string content. */ diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java index 3429dddcbc5..1776799c4ab 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java @@ -30,7 +30,6 @@ import org.elasticsearch.common.lease.Releasable; import org.elasticsearch.common.text.Text; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.common.xcontent.XContentBuilder.FieldCaseConversion; import org.joda.time.DateTimeZone; import org.joda.time.ReadableInstant; import org.joda.time.format.DateTimeFormatter; @@ -109,8 +108,7 @@ public final class XContentBuilder implements BytesStream, Releasable { * {@link #close()} when the builder is done with. */ public XContentBuilder(XContent xContent, OutputStream bos, String[] filters) throws IOException { - this.bos = bos; - this.generator = xContent.createGenerator(bos, filters); + this(xContent, bos, true, filters); } /** @@ -172,7 +170,7 @@ public final class XContentBuilder implements BytesStream, Releasable { } public XContentBuilder startObject(String name, FieldCaseConversion conversion) throws IOException { - field(name); + field(name, conversion); startObject(); return this; } diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContentGenerator.java index a69aae4253f..e11fc42b5a8 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentGenerator.java @@ -123,6 +123,4 @@ public interface XContentGenerator extends Closeable { @Override void close() throws IOException; - - void writeStartObject(String name) throws IOException; } diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java index ac6d4f27b10..cecdb5a9c86 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java @@ -66,16 +66,6 @@ public class CborXContent implements XContent { throw new ElasticsearchParseException("cbor does not support stream parsing..."); } - @Override - public XContentGenerator createGenerator(OutputStream os) throws IOException { - return new CborXContentGenerator(cborFactory.createGenerator(os, JsonEncoding.UTF8), os); - } - - @Override - public XContentGenerator createGenerator(OutputStream os, String[] filters) throws IOException { - return new CborXContentGenerator(cborFactory.createGenerator(os, JsonEncoding.UTF8), os, filters); - } - @Override public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException { return new CborXContentGenerator(cborFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters); diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContentGenerator.java index 13cd3060bcf..bf71fc9dc7a 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContentGenerator.java @@ -20,7 +20,6 @@ package org.elasticsearch.common.xcontent.cbor; import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.dataformat.cbor.CBORGenerator; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContentGenerator; @@ -33,11 +32,11 @@ import java.io.OutputStream; public class CborXContentGenerator extends JsonXContentGenerator { public CborXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, String... filters) { - super(jsonGenerator, os, filters); + this(jsonGenerator, os, true, filters); } - public CborXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusiveFilters, String[] filters) { - super(jsonGenerator, os, inclusiveFilters, filters); + public CborXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusive, String[] filters) { + super(jsonGenerator, os, inclusive, filters); } @Override diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java index 5d1ed6912fd..f29604eb61f 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java @@ -71,18 +71,8 @@ public class JsonXContent implements XContent { } @Override - public XContentGenerator createGenerator(OutputStream os) throws IOException { - return new JsonXContentGenerator(jsonFactory.createGenerator(os, JsonEncoding.UTF8), os); - } - - @Override - public XContentGenerator createGenerator(OutputStream os, String[] filters) throws IOException { - return new JsonXContentGenerator(jsonFactory.createGenerator(os, JsonEncoding.UTF8), os, filters); - } - - @Override - public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException { - return new JsonXContentGenerator(jsonFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters); + public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusive) throws IOException { + return new JsonXContentGenerator(jsonFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusive, filters); } @Override diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java index 97ce35915a4..7af14f25fcd 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java @@ -457,8 +457,4 @@ public class JsonXContentGenerator implements XContentGenerator { generator.close(); } - @Override - public void writeStartObject(String name) throws IOException { - generator.writeObjectFieldStart(name); - } } diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java index 15a418b0c02..285314e9b89 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java @@ -67,16 +67,6 @@ public class SmileXContent implements XContent { return (byte) 0xFF; } - @Override - public XContentGenerator createGenerator(OutputStream os) throws IOException { - return new SmileXContentGenerator(smileFactory.createGenerator(os, JsonEncoding.UTF8), os); - } - - @Override - public XContentGenerator createGenerator(OutputStream os, String[] filters) throws IOException { - return new SmileXContentGenerator(smileFactory.createGenerator(os, JsonEncoding.UTF8), os, filters); - } - @Override public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException { return new SmileXContentGenerator(smileFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters); diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContentGenerator.java index abfe95b91d1..f479102b8ae 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContentGenerator.java @@ -20,7 +20,6 @@ package org.elasticsearch.common.xcontent.smile; import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.dataformat.smile.SmileGenerator; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContentGenerator; @@ -33,11 +32,11 @@ import java.io.OutputStream; public class SmileXContentGenerator extends JsonXContentGenerator { public SmileXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, String... filters) { - super(jsonGenerator, os, filters); + this(jsonGenerator, os, true, filters); } - public SmileXContentGenerator(SmileGenerator jsonGenerator, OutputStream os, boolean inclusiveFilters, String[] filters) { - super(jsonGenerator, os, inclusiveFilters, filters); + public SmileXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusive, String[] filters) { + super(jsonGenerator, os, inclusive, filters); } @Override diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java b/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java index 1a537c7b7aa..03e0122e9d2 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java @@ -46,10 +46,10 @@ public class FilterPathBasedFilter extends TokenFilter { private final boolean inclusive; public FilterPathBasedFilter(boolean inclusive, FilterPath[] filters) { - this.inclusive = inclusive; if (CollectionUtils.isEmpty(filters)) { throw new IllegalArgumentException("filters cannot be null or empty"); } + this.inclusive = inclusive; this.filters = filters; } diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java index 3cd6416804c..ff4d805cd88 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java @@ -65,22 +65,11 @@ public class YamlXContent implements XContent { throw new ElasticsearchParseException("yaml does not support stream parsing..."); } - @Override - public XContentGenerator createGenerator(OutputStream os) throws IOException { - return new YamlXContentGenerator(yamlFactory.createGenerator(os, JsonEncoding.UTF8), os); - } - - @Override - public XContentGenerator createGenerator(OutputStream os, String[] filters) throws IOException { - return new YamlXContentGenerator(yamlFactory.createGenerator(os, JsonEncoding.UTF8), os, filters); - } - @Override public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException { return new YamlXContentGenerator(yamlFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters); } - @Override public XContentParser createParser(String content) throws IOException { return new YamlXContentParser(yamlFactory.createParser(new FastStringReader(content))); diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContentGenerator.java index 0c9ffc73762..9e2f5b7cbbd 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContentGenerator.java @@ -32,11 +32,11 @@ import java.io.OutputStream; public class YamlXContentGenerator extends JsonXContentGenerator { public YamlXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, String... filters) { - super(jsonGenerator, os, filters); + this(jsonGenerator, os, true, filters); } - public YamlXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusiveFilters, String[] filters) { - super(jsonGenerator, os, inclusiveFilters, filters); + public YamlXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusive, String[] filters) { + super(jsonGenerator, os, inclusive, filters); } @Override diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java b/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java index 8f31eb093a3..ba5301c4160 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java @@ -31,7 +31,7 @@ public class FilterPathGeneratorFilteringTests extends ESTestCase { private final JsonFactory JSON_FACTORY = new JsonFactory(); - public void testFilters() throws Exception { + public void testInclusiveFilters() throws Exception { final String SAMPLE = "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"; assertResult(SAMPLE, true, "a", "{'a':0}"); @@ -122,11 +122,16 @@ public class FilterPathGeneratorFilteringTests extends ESTestCase { } - public void testFiltersWithDots() throws Exception { + public void testInclusiveFiltersWithDots() throws Exception { assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", true, "b.c", "{'b':{'c':'c_value'}}"); assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", true, "b\\.c", "{'b.c':'value'}"); } + public void testExclusiveFiltersWithDots() throws Exception { + assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", false, "b.c", "{'a':0,'b.c':'value'}"); + assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", false, "b\\.c", "{'a':0,'b':{'c':'c_value'}}"); + } + private void assertResult(String input, boolean inclusive, String filter, String expected) throws Exception { try (BytesStreamOutput os = new BytesStreamOutput()) { try (FilteringGeneratorDelegate generator = new FilteringGeneratorDelegate(JSON_FACTORY.createGenerator(os),