Search in sources :

Example 1 with XContentParser

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);
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) ArrayList(java.util.ArrayList) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 2 with XContentParser

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));
}
Also used : XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 3 with XContentParser

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));
}
Also used : XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 4 with XContentParser

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;
}
Also used : HashMap(java.util.HashMap) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) ArrayList(java.util.ArrayList) XContentParser(org.elasticsearch.common.xcontent.XContentParser) IOException(java.io.IOException) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException)

Example 5 with XContentParser

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);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) GetField.readGetField(org.elasticsearch.index.get.GetField.readGetField) HashMap(java.util.HashMap) XContentParser(org.elasticsearch.common.xcontent.XContentParser) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

XContentParser (org.elasticsearch.common.xcontent.XContentParser)463 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)147 ParsingException (org.elasticsearch.common.ParsingException)110 IOException (java.io.IOException)77 BytesReference (org.elasticsearch.common.bytes.BytesReference)63 ArrayList (java.util.ArrayList)59 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)56 XContentType (org.elasticsearch.common.xcontent.XContentType)47 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)44 HashMap (java.util.HashMap)33 Map (java.util.Map)27 Matchers.containsString (org.hamcrest.Matchers.containsString)23 List (java.util.List)22 Settings (org.elasticsearch.common.settings.Settings)18 ToXContent (org.elasticsearch.common.xcontent.ToXContent)16 ShardId (org.elasticsearch.index.shard.ShardId)16 Script (org.elasticsearch.script.Script)16 XContent (org.elasticsearch.common.xcontent.XContent)15 ElasticsearchException (org.elasticsearch.ElasticsearchException)14 NodeClient (org.elasticsearch.client.node.NodeClient)14