Changes after review

This commit is contained in:
Konrad Beiske 2016-01-15 17:47:55 +01:00
parent 06516f652f
commit c13ed81d5a
13 changed files with 35 additions and 71 deletions

View File

@ -41,18 +41,28 @@ public interface XContent {
/** /**
* Creates a new generator using the provided output stream. * 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 * Creates a new generator using the provided output stream and some
* filters. * 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. * Creates a parser over the provided string content.
*/ */

View File

@ -30,7 +30,6 @@ import org.elasticsearch.common.lease.Releasable;
import org.elasticsearch.common.text.Text; import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder.FieldCaseConversion;
import org.joda.time.DateTimeZone; import org.joda.time.DateTimeZone;
import org.joda.time.ReadableInstant; import org.joda.time.ReadableInstant;
import org.joda.time.format.DateTimeFormatter; 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. * {@link #close()} when the builder is done with.
*/ */
public XContentBuilder(XContent xContent, OutputStream bos, String[] filters) throws IOException { public XContentBuilder(XContent xContent, OutputStream bos, String[] filters) throws IOException {
this.bos = bos; this(xContent, bos, true, filters);
this.generator = xContent.createGenerator(bos, filters);
} }
/** /**
@ -172,7 +170,7 @@ public final class XContentBuilder implements BytesStream, Releasable {
} }
public XContentBuilder startObject(String name, FieldCaseConversion conversion) throws IOException { public XContentBuilder startObject(String name, FieldCaseConversion conversion) throws IOException {
field(name); field(name, conversion);
startObject(); startObject();
return this; return this;
} }

View File

@ -123,6 +123,4 @@ public interface XContentGenerator extends Closeable {
@Override @Override
void close() throws IOException; void close() throws IOException;
void writeStartObject(String name) throws IOException;
} }

View File

@ -66,16 +66,6 @@ public class CborXContent implements XContent {
throw new ElasticsearchParseException("cbor does not support stream parsing..."); 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 @Override
public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException { public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException {
return new CborXContentGenerator(cborFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters); return new CborXContentGenerator(cborFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters);

View File

@ -20,7 +20,6 @@
package org.elasticsearch.common.xcontent.cbor; package org.elasticsearch.common.xcontent.cbor;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.dataformat.cbor.CBORGenerator;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContentGenerator; import org.elasticsearch.common.xcontent.json.JsonXContentGenerator;
@ -33,11 +32,11 @@ import java.io.OutputStream;
public class CborXContentGenerator extends JsonXContentGenerator { public class CborXContentGenerator extends JsonXContentGenerator {
public CborXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, String... filters) { 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) { public CborXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusive, String[] filters) {
super(jsonGenerator, os, inclusiveFilters, filters); super(jsonGenerator, os, inclusive, filters);
} }
@Override @Override

View File

@ -71,18 +71,8 @@ public class JsonXContent implements XContent {
} }
@Override @Override
public XContentGenerator createGenerator(OutputStream os) throws IOException { public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusive) throws IOException {
return new JsonXContentGenerator(jsonFactory.createGenerator(os, JsonEncoding.UTF8), os); return new JsonXContentGenerator(jsonFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusive, filters);
}
@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);
} }
@Override @Override

View File

@ -457,8 +457,4 @@ public class JsonXContentGenerator implements XContentGenerator {
generator.close(); generator.close();
} }
@Override
public void writeStartObject(String name) throws IOException {
generator.writeObjectFieldStart(name);
}
} }

View File

@ -67,16 +67,6 @@ public class SmileXContent implements XContent {
return (byte) 0xFF; 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 @Override
public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException { public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException {
return new SmileXContentGenerator(smileFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters); return new SmileXContentGenerator(smileFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters);

View File

@ -20,7 +20,6 @@
package org.elasticsearch.common.xcontent.smile; package org.elasticsearch.common.xcontent.smile;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.dataformat.smile.SmileGenerator;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContentGenerator; import org.elasticsearch.common.xcontent.json.JsonXContentGenerator;
@ -33,11 +32,11 @@ import java.io.OutputStream;
public class SmileXContentGenerator extends JsonXContentGenerator { public class SmileXContentGenerator extends JsonXContentGenerator {
public SmileXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, String... filters) { 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) { public SmileXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusive, String[] filters) {
super(jsonGenerator, os, inclusiveFilters, filters); super(jsonGenerator, os, inclusive, filters);
} }
@Override @Override

View File

@ -46,10 +46,10 @@ public class FilterPathBasedFilter extends TokenFilter {
private final boolean inclusive; private final boolean inclusive;
public FilterPathBasedFilter(boolean inclusive, FilterPath[] filters) { public FilterPathBasedFilter(boolean inclusive, FilterPath[] filters) {
this.inclusive = inclusive;
if (CollectionUtils.isEmpty(filters)) { if (CollectionUtils.isEmpty(filters)) {
throw new IllegalArgumentException("filters cannot be null or empty"); throw new IllegalArgumentException("filters cannot be null or empty");
} }
this.inclusive = inclusive;
this.filters = filters; this.filters = filters;
} }

View File

@ -65,22 +65,11 @@ public class YamlXContent implements XContent {
throw new ElasticsearchParseException("yaml does not support stream parsing..."); 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 @Override
public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException { public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException {
return new YamlXContentGenerator(yamlFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters); return new YamlXContentGenerator(yamlFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters);
} }
@Override @Override
public XContentParser createParser(String content) throws IOException { public XContentParser createParser(String content) throws IOException {
return new YamlXContentParser(yamlFactory.createParser(new FastStringReader(content))); return new YamlXContentParser(yamlFactory.createParser(new FastStringReader(content)));

View File

@ -32,11 +32,11 @@ import java.io.OutputStream;
public class YamlXContentGenerator extends JsonXContentGenerator { public class YamlXContentGenerator extends JsonXContentGenerator {
public YamlXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, String... filters) { 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) { public YamlXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusive, String[] filters) {
super(jsonGenerator, os, inclusiveFilters, filters); super(jsonGenerator, os, inclusive, filters);
} }
@Override @Override

View File

@ -31,7 +31,7 @@ public class FilterPathGeneratorFilteringTests extends ESTestCase {
private final JsonFactory JSON_FACTORY = new JsonFactory(); 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'}}}}}"; 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}"); 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':'c_value'}}");
assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", true, "b\\.c", "{'b.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 { private void assertResult(String input, boolean inclusive, String filter, String expected) throws Exception {
try (BytesStreamOutput os = new BytesStreamOutput()) { try (BytesStreamOutput os = new BytesStreamOutput()) {
try (FilteringGeneratorDelegate generator = new FilteringGeneratorDelegate(JSON_FACTORY.createGenerator(os), try (FilteringGeneratorDelegate generator = new FilteringGeneratorDelegate(JSON_FACTORY.createGenerator(os),