use of org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.
the class SkipSection method parse.
public static SkipSection parse(XContentParser parser) throws IOException {
if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
throw new IllegalArgumentException("Expected [" + XContentParser.Token.START_OBJECT + ", found [" + parser.currentToken() + "], the skip section is not properly indented");
}
String currentFieldName = null;
XContentParser.Token token;
String version = null;
String reason = null;
List<String> features = new ArrayList<>();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if ("version".equals(currentFieldName)) {
version = parser.text();
} else if ("reason".equals(currentFieldName)) {
reason = parser.text();
} else if ("features".equals(currentFieldName)) {
features.add(parser.text());
} else {
throw new ParsingException(parser.getTokenLocation(), "field " + currentFieldName + " not supported within skip section");
}
} else if (token == XContentParser.Token.START_ARRAY) {
if ("features".equals(currentFieldName)) {
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
features.add(parser.text());
}
}
}
}
parser.nextToken();
if (!Strings.hasLength(version) && features.isEmpty()) {
throw new ParsingException(parser.getTokenLocation(), "version or features is mandatory within skip section");
}
if (Strings.hasLength(version) && !Strings.hasLength(reason)) {
throw new ParsingException(parser.getTokenLocation(), "reason is mandatory within skip version section");
}
return new SkipSection(version, features, reason);
}
use of org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.
the class DoSectionTests method assertJsonEquals.
private void assertJsonEquals(Map<String, Object> actual, String expected) throws IOException {
Map<String, Object> expectedMap;
try (XContentParser parser = createParser(YamlXContent.yamlXContent, expected)) {
expectedMap = parser.mapOrdered();
}
MatcherAssert.assertThat(actual, equalTo(expectedMap));
}
use of org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.
the class ClientYamlSuiteRestApiParserFailingTests method parseAndExpectFailure.
private void parseAndExpectFailure(String brokenJson, String expectedErrorMessage) throws Exception {
XContentParser parser = createParser(YamlXContent.yamlXContent, brokenJson);
ClientYamlSuiteRestApiParser restApiParser = new ClientYamlSuiteRestApiParser();
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> restApiParser.parse("location", parser));
assertThat(e.getMessage(), containsString(expectedErrorMessage));
}
use of org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.
the class XContentSettingsLoader method load.
public Map<String, String> load(XContentParser jp) throws IOException {
StringBuilder sb = new StringBuilder();
Map<String, String> settings = new HashMap<>();
List<String> path = new ArrayList<>();
XContentParser.Token token = jp.nextToken();
if (token == null) {
return settings;
}
if (token != XContentParser.Token.START_OBJECT) {
throw new ElasticsearchParseException("malformed, expected settings to start with 'object', instead was [{}]", token);
}
serializeObject(settings, sb, path, jp, null);
// ensure we reached the end of the stream
XContentParser.Token lastToken = null;
try {
while (!jp.isClosed() && (lastToken = jp.nextToken()) == null) ;
} catch (Exception e) {
throw new ElasticsearchParseException("malformed, expected end of settings but encountered additional content starting at line number: [{}], " + "column number: [{}]", e, jp.getTokenLocation().lineNumber, jp.getTokenLocation().columnNumber);
}
if (lastToken != null) {
throw new ElasticsearchParseException("malformed, expected end of settings but encountered additional content starting at line number: [{}], " + "column number: [{}]", jp.getTokenLocation().lineNumber, jp.getTokenLocation().columnNumber);
}
return settings;
}
use of org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.
the class GetResult method fromXContentEmbedded.
public static GetResult fromXContentEmbedded(XContentParser parser) throws IOException {
XContentParser.Token token = parser.nextToken();
ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser::getTokenLocation);
String currentFieldName = parser.currentName();
String index = null, type = null, id = null;
long version = -1;
boolean found = false;
BytesReference source = null;
Map<String, GetField> fields = new HashMap<>();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if (_INDEX.equals(currentFieldName)) {
index = parser.text();
} else if (_TYPE.equals(currentFieldName)) {
type = parser.text();
} else if (_ID.equals(currentFieldName)) {
id = parser.text();
} else if (_VERSION.equals(currentFieldName)) {
version = parser.longValue();
} else if (FOUND.equals(currentFieldName)) {
found = parser.booleanValue();
} else {
fields.put(currentFieldName, new GetField(currentFieldName, Collections.singletonList(parser.objectText())));
}
} else if (token == XContentParser.Token.START_OBJECT) {
if (SourceFieldMapper.NAME.equals(currentFieldName)) {
try (XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent())) {
//the original document gets slightly modified: whitespaces or pretty printing are not preserved,
//it all depends on the current builder settings
builder.copyCurrentStructure(parser);
source = builder.bytes();
}
} else if (FIELDS.equals(currentFieldName)) {
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
GetField getField = GetField.fromXContent(parser);
fields.put(getField.getName(), getField);
}
} else {
throwUnknownField(currentFieldName, parser.getTokenLocation());
}
}
}
return new GetResult(index, type, id, version, found, source, fields);
}
Aggregations