Class XContentParserUtils
- java.lang.Object
-
- org.elasticsearch.common.xcontent.XContentParserUtils
-
public final class XContentParserUtils extends java.lang.Object
A set of static methods to getXContentParser.Token
fromXContentParser
while checking for their types and throwParsingException
if needed.
-
-
Method Summary
Modifier and Type Method Description static void
ensureExpectedToken(XContentParser.Token expected, XContentParser.Token actual, java.util.function.Supplier<XContentLocation> location)
Makes sure that provided token is of the expected typestatic void
ensureFieldName(XContentParser parser, XContentParser.Token token, java.lang.String fieldName)
Makes sure that current token is of typeXContentParser.Token.FIELD_NAME
and the field name is equal to the provided onestatic java.lang.Object
parseFieldsValue(XContentParser parser)
Parse the current token depending on its token type.static <T> void
parseTypedKeysObject(XContentParser parser, java.lang.String delimiter, java.lang.Class<T> objectClass, java.util.function.Consumer<T> consumer)
This method expects that the current field name is the concatenation of a type, a delimiter and a name (ex: terms#foo where "terms" refers to the type of a registeredNamedXContentRegistry.Entry
, "#" is the delimiter and "foo" the name of the object to parse).static void
throwUnknownField(java.lang.String field, XContentLocation location)
static void
throwUnknownToken(XContentParser.Token token, XContentLocation location)
-
-
-
Method Detail
-
ensureFieldName
public static void ensureFieldName(XContentParser parser, XContentParser.Token token, java.lang.String fieldName) throws java.io.IOException
Makes sure that current token is of typeXContentParser.Token.FIELD_NAME
and the field name is equal to the provided one- Throws:
ParsingException
- if the token is not of typeXContentParser.Token.FIELD_NAME
or is not equal to the given field namejava.io.IOException
-
throwUnknownField
public static void throwUnknownField(java.lang.String field, XContentLocation location)
- Throws:
ParsingException
- with a "unknown field found" reason
-
throwUnknownToken
public static void throwUnknownToken(XContentParser.Token token, XContentLocation location)
- Throws:
ParsingException
- with a "unknown token found" reason
-
ensureExpectedToken
public static void ensureExpectedToken(XContentParser.Token expected, XContentParser.Token actual, java.util.function.Supplier<XContentLocation> location)
Makes sure that provided token is of the expected type- Throws:
ParsingException
- if the token is not equal to the expected type
-
parseFieldsValue
public static java.lang.Object parseFieldsValue(XContentParser parser) throws java.io.IOException
Parse the current token depending on its token type. The following token types will be parsed by the corresponding parser methods:XContentParser.Token.VALUE_STRING
:XContentParser.text()
XContentParser.Token.VALUE_NUMBER
:XContentParser.numberValue()
()}XContentParser.Token.VALUE_BOOLEAN
:XContentParser.booleanValue()
()}XContentParser.Token.VALUE_EMBEDDED_OBJECT
:XContentParser.binaryValue()
()}XContentParser.Token.VALUE_NULL
: returns nullXContentParser.Token.START_OBJECT
:XContentParser.mapOrdered()
()}XContentParser.Token.START_ARRAY
:XContentParser.listOrderedMap()
()}
- Throws:
ParsingException
- if the token is none of the allowed valuesjava.io.IOException
-
parseTypedKeysObject
public static <T> void parseTypedKeysObject(XContentParser parser, java.lang.String delimiter, java.lang.Class<T> objectClass, java.util.function.Consumer<T> consumer) throws java.io.IOException
This method expects that the current field name is the concatenation of a type, a delimiter and a name (ex: terms#foo where "terms" refers to the type of a registeredNamedXContentRegistry.Entry
, "#" is the delimiter and "foo" the name of the object to parse). It also expected that following this field name is either an Object or an array xContent structure and the cursor points to the start token of this structure. The method splits the field's name to extract the type and name and then parses the object using theXContentParser.namedObject(Class, String, Object)
method.- Type Parameters:
T
- the type of the object to parse- Parameters:
parser
- the currentXContentParser
delimiter
- the delimiter to use to splits the field's nameobjectClass
- the object class of the object to parseconsumer
- something to consume the parsed object- Throws:
java.io.IOException
- if anything went wrong during parsing or if the type or name cannot be derived from the field's nameParsingException
- if the parser isn't positioned on either START_OBJECT or START_ARRAY at the beginning
-
-