java.lang.Object
net.luis.utils.io.codec.provider.XmlTypeProvider
- All Implemented Interfaces:
TypeProvider<XmlElement>
Type provider implementation for xml elements.
This class is a singleton and should be accessed through the
This class is a singleton and should be accessed through the
INSTANCE
constant.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String
Constants for the name of boolean xml element type.private static final String
Constants for the name of byte xml element type.private static final String
Constants for the name of double xml element type.private static final String
Constants for the name of list element xml element type.private static final String
Constant for the name of the empty xml element type.private static final String
Constants for the name of float xml element type.private static final String
Constant for the generated namespace of the xml type provider.
This namespace is used to create unique names for the xml types.static final XmlTypeProvider
The singleton instance of this class.private static final String
Constants for the name of integer xml element type.private static final String
Constants for the name of list xml element type.private static final String
Constants for the name of long xml element type.private static final String
Constants for the name of map xml element type.private static final String
private static final String
Constants for the name of short xml element type.private static final String
Constants for the name of string xml element type.private final boolean
Whether to useROOT
as name for maps. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
XmlTypeProvider
(boolean useRoot) Private constructor to prevent instantiation. -
Method Summary
Modifier and TypeMethodDescriptionprivate @NotNull XmlElement
copyWithName
(@NotNull String name, @NotNull XmlElement value) Copies the given xml element with the given name.
The name is used as the new name for the copied element.
The copy operation is based on the type of the given element.@NotNull Result
<XmlElement> createBoolean
(boolean value) Creates a boolean value of the type this provider is for using the given value.@NotNull Result
<XmlElement> createByte
(byte value) Creates a byte value of the type this provider is for using the given value.@NotNull Result
<XmlElement> createDouble
(double value) Creates a double value of the type this provider is for using the given value.@NotNull Result
<XmlElement> createFloat
(float value) Creates a float value of the type this provider is for using the given value.@NotNull Result
<XmlElement> createInteger
(int value) Creates an integer value of the type this provider is for using the given value.@NotNull Result
<XmlElement> createList
(@NotNull List<? extends XmlElement> values) Creates a list value of the type this provider is for using the given values.@NotNull Result
<XmlElement> createLong
(long value) Creates a long value of the type this provider is for using the given value.@NotNull Result
<XmlElement> Creates an empty map value of the type this provider is for.@NotNull Result
<XmlElement> createMap
(@NotNull Map<String, ? extends XmlElement> values) Creates a map value of the type this provider is for using the given values.@NotNull Result
<XmlElement> createShort
(short value) Creates a short value of the type this provider is for using the given value.@NotNull Result
<XmlElement> createString
(@NotNull String value) Creates a string value of the type this provider is for using the given value.@NotNull XmlElement
empty()
Creates an empty value of the type this provider is for.private @NotNull String
escapeName
(@NotNull String name) Escapes the given name if it is numeric.
The name is escaped by adding an underscore in front of it.
This is required because xml element names must not start with a number.@NotNull Result
<XmlElement> get
(@NotNull XmlElement type, @NotNull String key) Gets the value of the given key from the given value.
The given type must be a map.getBoolean
(@NotNull XmlElement type) Gets the given value as a boolean value of the type this provider is for.getByte
(@NotNull XmlElement type) Gets the given value as a byte value of the type this provider is for.getDouble
(@NotNull XmlElement type) Gets the given value as a double value of the type this provider is for.@NotNull Result
<XmlElement> getEmpty
(@NotNull XmlElement type) Gets the given value as an empty value of the type this provider is for.getFloat
(@NotNull XmlElement type) Gets the given value as a float value of the type this provider is for.getInteger
(@NotNull XmlElement type) Gets the given value as an integer value of the type this provider is for.@NotNull Result
<List<XmlElement>> getList
(@NotNull XmlElement type) Gets the given value as a list value of the type this provider is for.getLong
(@NotNull XmlElement type) Gets the given value as a long value of the type this provider is for.@NotNull Result
<Map<String, XmlElement>> getMap
(@NotNull XmlElement type) Gets the given value as a map value of the type this provider is for.private @NotNull String
Returns the name for maps based onuseRoot
.getShort
(@NotNull XmlElement type) Gets the given value as a short value of the type this provider is for.getString
(@NotNull XmlElement type) Gets the given value as a string value of the type this provider is for.has
(@NotNull XmlElement type, @NotNull String key) Checks if the given value is a map and contains the given key.
The given type must be a map.@NotNull Result
<XmlElement> merge
(@NotNull XmlElement current, @NotNull XmlElement value) Merges the given value with the current value.
The logic is specific to the type this provider is for.private @NotNull List
<XmlElement> mergeArray
(@NotNull XmlElements current, @NotNull XmlElements value) Merges two arrays of xml elements.
The elements of the current and value arrays are copied with the same name.
The copied elements are then added to a new array.private @NotNull Map
<String, XmlElement> mergeObject
(@NotNull XmlElements current, @NotNull XmlElements value) Merges two objects of xml elements.
The elements of the current and value objects are moved to a new object.private @NotNull Result
<XmlElement> mergeUndefined
(@NotNull XmlElements current, @NotNull XmlElements value) Merges two undefined containers of xml elements.
The elements of the current and value containers are moved to a new container.@NotNull Result
<XmlElement> set
(@NotNull XmlElement type, @NotNull String key, @NotNull XmlElement value) Sets the value for the given key in the given value.
The given type must be a map.private @NotNull String
unescapeName
(@NotNull String name) Unescapes the given name if it is numeric.
The name is unescaped by removing an underscore in front of it.
This is required because xml element names must not start with a number.@NotNull XmlTypeProvider
useRoot()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface net.luis.utils.io.codec.provider.TypeProvider
merge, set
-
Field Details
-
GENERATED
Constant for the generated namespace of the xml type provider.
This namespace is used to create unique names for the xml types.- See Also:
-
EMPTY
Constant for the name of the empty xml element type.- See Also:
-
BOOLEAN
Constants for the name of boolean xml element type.- See Also:
-
BYTE
Constants for the name of byte xml element type.- See Also:
-
SHORT
Constants for the name of short xml element type.- See Also:
-
INTEGER
Constants for the name of integer xml element type.- See Also:
-
LONG
Constants for the name of long xml element type.- See Also:
-
FLOAT
Constants for the name of float xml element type.- See Also:
-
DOUBLE
Constants for the name of double xml element type.- See Also:
-
STRING
Constants for the name of string xml element type.- See Also:
-
LIST
Constants for the name of list xml element type.- See Also:
-
ELEMENT
Constants for the name of list element xml element type.- See Also:
-
MAP
Constants for the name of map xml element type.- See Also:
-
ROOT
- See Also:
-
INSTANCE
The singleton instance of this class. -
useRoot
private final boolean useRootWhether to useROOT
as name for maps.
-
-
Constructor Details
-
XmlTypeProvider
private XmlTypeProvider(boolean useRoot) Private constructor to prevent instantiation.- Parameters:
useRoot
- Whether to use root as name for maps
-
-
Method Details
-
useRoot
Returns a new instance of this class withuseRoot
set to true.
IfuseRoot
is already true, the same instance is returned.- Returns:
- A new instance
-
empty
Description copied from interface:TypeProvider
Creates an empty value of the type this provider is for.- Specified by:
empty
in interfaceTypeProvider<XmlElement>
- Returns:
- An empty value
-
createBoolean
Description copied from interface:TypeProvider
Creates a boolean value of the type this provider is for using the given value.- Specified by:
createBoolean
in interfaceTypeProvider<XmlElement>
- Parameters:
value
- The value to create the boolean value from- Returns:
- A result containing the boolean value
-
createByte
Description copied from interface:TypeProvider
Creates a byte value of the type this provider is for using the given value.- Specified by:
createByte
in interfaceTypeProvider<XmlElement>
- Parameters:
value
- The value to create the byte value from- Returns:
- A result containing the byte value
-
createShort
Description copied from interface:TypeProvider
Creates a short value of the type this provider is for using the given value.- Specified by:
createShort
in interfaceTypeProvider<XmlElement>
- Parameters:
value
- The value to create the short value from- Returns:
- A result containing the short value
-
createInteger
Description copied from interface:TypeProvider
Creates an integer value of the type this provider is for using the given value.- Specified by:
createInteger
in interfaceTypeProvider<XmlElement>
- Parameters:
value
- The value to create the integer value from- Returns:
- A result containing the integer value
-
createLong
Description copied from interface:TypeProvider
Creates a long value of the type this provider is for using the given value.- Specified by:
createLong
in interfaceTypeProvider<XmlElement>
- Parameters:
value
- The value to create the long value from- Returns:
- A result containing the long value
-
createFloat
Description copied from interface:TypeProvider
Creates a float value of the type this provider is for using the given value.- Specified by:
createFloat
in interfaceTypeProvider<XmlElement>
- Parameters:
value
- The value to create the float value from- Returns:
- A result containing the float value
-
createDouble
Description copied from interface:TypeProvider
Creates a double value of the type this provider is for using the given value.- Specified by:
createDouble
in interfaceTypeProvider<XmlElement>
- Parameters:
value
- The value to create the double value from- Returns:
- A result containing the double value
-
createString
Description copied from interface:TypeProvider
Creates a string value of the type this provider is for using the given value.- Specified by:
createString
in interfaceTypeProvider<XmlElement>
- Parameters:
value
- The value to create the string value from- Returns:
- A result containing the string value
-
createList
@NotNull public @NotNull Result<XmlElement> createList(@NotNull @NotNull List<? extends XmlElement> values) Description copied from interface:TypeProvider
Creates a list value of the type this provider is for using the given values.- Specified by:
createList
in interfaceTypeProvider<XmlElement>
- Parameters:
values
- The values to create the list value from- Returns:
- A result containing the list value
-
createMap
Description copied from interface:TypeProvider
Creates an empty map value of the type this provider is for.- Specified by:
createMap
in interfaceTypeProvider<XmlElement>
- Returns:
- A result containing the empty map value
-
createMap
@NotNull public @NotNull Result<XmlElement> createMap(@NotNull @NotNull Map<String, ? extends XmlElement> values) Description copied from interface:TypeProvider
Creates a map value of the type this provider is for using the given values.- Specified by:
createMap
in interfaceTypeProvider<XmlElement>
- Parameters:
values
- The values to create the map value from- Returns:
- A result containing the map value
-
getEmpty
Description copied from interface:TypeProvider
Gets the given value as an empty value of the type this provider is for.- Specified by:
getEmpty
in interfaceTypeProvider<XmlElement>
- Parameters:
type
- The value to get as an empty value- Returns:
- A success result containing the empty value, or an error result if the given value is not an empty value
-
getBoolean
Description copied from interface:TypeProvider
Gets the given value as a boolean value of the type this provider is for.- Specified by:
getBoolean
in interfaceTypeProvider<XmlElement>
- Parameters:
type
- The value to get as a boolean value- Returns:
- A success result containing the boolean value, or an error result if the given value is not a boolean value
-
getByte
Description copied from interface:TypeProvider
Gets the given value as a byte value of the type this provider is for.- Specified by:
getByte
in interfaceTypeProvider<XmlElement>
- Parameters:
type
- The value to get as a byte value- Returns:
- A success result containing the byte value, or an error result if the given value is not a byte value
-
getShort
Description copied from interface:TypeProvider
Gets the given value as a short value of the type this provider is for.- Specified by:
getShort
in interfaceTypeProvider<XmlElement>
- Parameters:
type
- The value to get as a short value- Returns:
- A success result containing the short value, or an error result if the given value is not a short value
-
getInteger
Description copied from interface:TypeProvider
Gets the given value as an integer value of the type this provider is for.- Specified by:
getInteger
in interfaceTypeProvider<XmlElement>
- Parameters:
type
- The value to get as an integer value- Returns:
- A success result containing the integer value, or an error result if the given value is not an integer value
-
getLong
Description copied from interface:TypeProvider
Gets the given value as a long value of the type this provider is for.- Specified by:
getLong
in interfaceTypeProvider<XmlElement>
- Parameters:
type
- The value to get as a long value- Returns:
- A success result containing the long value, or an error result if the given value is not a long value
-
getFloat
Description copied from interface:TypeProvider
Gets the given value as a float value of the type this provider is for.- Specified by:
getFloat
in interfaceTypeProvider<XmlElement>
- Parameters:
type
- The value to get as a float value- Returns:
- A success result containing the float value, or an error result if the given value is not a float value
-
getDouble
Description copied from interface:TypeProvider
Gets the given value as a double value of the type this provider is for.- Specified by:
getDouble
in interfaceTypeProvider<XmlElement>
- Parameters:
type
- The value to get as a double value- Returns:
- A success result containing the double value, or an error result if the given value is not a double value
-
getString
Description copied from interface:TypeProvider
Gets the given value as a string value of the type this provider is for.- Specified by:
getString
in interfaceTypeProvider<XmlElement>
- Parameters:
type
- The value to get as a string value- Returns:
- A success result containing the string value, or an error result if the given value is not a string value
-
getList
Description copied from interface:TypeProvider
Gets the given value as a list value of the type this provider is for.- Specified by:
getList
in interfaceTypeProvider<XmlElement>
- Parameters:
type
- The value to get as a list value- Returns:
- A success result containing the list value, or an error result if the given value is not a list value
-
getMap
Description copied from interface:TypeProvider
Gets the given value as a map value of the type this provider is for.- Specified by:
getMap
in interfaceTypeProvider<XmlElement>
- Parameters:
type
- The value to get as a map value- Returns:
- A success result containing the map value, or an error result if the given value is not a map value
-
has
@NotNull public @NotNull Result<Boolean> has(@NotNull @NotNull XmlElement type, @NotNull @NotNull String key) Description copied from interface:TypeProvider
Checks if the given value is a map and contains the given key.
The given type must be a map.- Specified by:
has
in interfaceTypeProvider<XmlElement>
- Parameters:
type
- The value to checkkey
- The key to check- Returns:
- A success result containing true if the map contains the key or false if not, or an error result if the given value is not a map
-
get
@NotNull public @NotNull Result<XmlElement> get(@NotNull @NotNull XmlElement type, @NotNull @NotNull String key) Description copied from interface:TypeProvider
Gets the value of the given key from the given value.
The given type must be a map.- Specified by:
get
in interfaceTypeProvider<XmlElement>
- Parameters:
type
- The value to get the value fromkey
- The key to get the value for- Returns:
- A success result containing the value for the key, or an error result if the given value is not a map
-
set
@NotNull public @NotNull Result<XmlElement> set(@NotNull @NotNull XmlElement type, @NotNull @NotNull String key, @NotNull @NotNull XmlElement value) Description copied from interface:TypeProvider
Sets the value for the given key in the given value.
The given type must be a map.- Specified by:
set
in interfaceTypeProvider<XmlElement>
- Parameters:
type
- The value to set the value inkey
- The key to set the value forvalue
- The value to set- Returns:
- A success result containing the result of the set operation, or an error result if the given value is not a map
-
merge
@NotNull public @NotNull Result<XmlElement> merge(@NotNull @NotNull XmlElement current, @NotNull @NotNull XmlElement value) Description copied from interface:TypeProvider
Merges the given value with the current value.
The logic is specific to the type this provider is for.- Specified by:
merge
in interfaceTypeProvider<XmlElement>
- Parameters:
current
- The current valuevalue
- The value to merge- Returns:
- A success result containing the result of the merge operation, or an error result if the merge operation failed
-
getMapName
Returns the name for maps based onuseRoot
.- Returns:
- The name for maps
-
escapeName
Escapes the given name if it is numeric.
The name is escaped by adding an underscore in front of it.
This is required because xml element names must not start with a number.- Parameters:
name
- The name to escape- Returns:
- The escaped name
- Throws:
NullPointerException
- If the name is null
-
unescapeName
Unescapes the given name if it is numeric.
The name is unescaped by removing an underscore in front of it.
This is required because xml element names must not start with a number.- Parameters:
name
- The name to unescape- Returns:
- The unescaped name
- Throws:
NullPointerException
- If the name is null
-
copyWithName
@NotNull private @NotNull XmlElement copyWithName(@NotNull @NotNull String name, @NotNull @NotNull XmlElement value) Copies the given xml element with the given name.
The name is used as the new name for the copied element.
The copy operation is based on the type of the given element.- Parameters:
name
- The name for the copied elementvalue
- The element to copy- Returns:
- The copied element
- Throws:
NullPointerException
- If the name or value is nullIllegalStateException
- If the value is not a valid xml element (should not happen)
-
mergeArray
@NotNull private @NotNull List<XmlElement> mergeArray(@NotNull @NotNull XmlElements current, @NotNull @NotNull XmlElements value) Merges two arrays of xml elements.
The elements of the current and value arrays are copied with the same name.
The copied elements are then added to a new array.- Parameters:
current
- The current elementsvalue
- The value elements- Returns:
- The merged array
- Throws:
NullPointerException
- If the current or value elements are nullXmlTypeException
- If the current or value elements are not arrays
-
mergeObject
@NotNull private @NotNull Map<String,XmlElement> mergeObject(@NotNull @NotNull XmlElements current, @NotNull @NotNull XmlElements value) Merges two objects of xml elements.
The elements of the current and value objects are moved to a new object.- Parameters:
current
- The current elementsvalue
- The value elements- Returns:
- The merged object as map
- Throws:
NullPointerException
- If the current or value elements are nullXmlTypeException
- If the current or value elements are not objects
-
mergeUndefined
@NotNull private @NotNull Result<XmlElement> mergeUndefined(@NotNull @NotNull XmlElements current, @NotNull @NotNull XmlElements value) Merges two undefined containers of xml elements.
The elements of the current and value containers are moved to a new container.- Parameters:
current
- The current elementsvalue
- The value elements- Returns:
- A result containing the merged container or an error
- Throws:
NullPointerException
- If the current or value elements are null
-