Class XmlTypeProvider

java.lang.Object
net.luis.utils.io.codec.provider.XmlTypeProvider
All Implemented Interfaces:
TypeProvider<XmlElement>

@Singleton public final class XmlTypeProvider extends Object implements TypeProvider<XmlElement>
Type provider implementation for xml elements.
This class is a singleton and should be accessed through the INSTANCE constant.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private 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
    Constants for the name of root xml element type.
    Used as name for maps if useRoot is true.
    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 use ROOT as name for maps.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    XmlTypeProvider(boolean useRoot)
    Private constructor to prevent instantiation.
  • Method Summary

    Modifier and Type
    Method
    Description
    private @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
    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.
    @NotNull Result<Boolean>
    getBoolean(@NotNull XmlElement type)
    Gets the given value as a boolean value of the type this provider is for.
    @NotNull Result<Byte>
    getByte(@NotNull XmlElement type)
    Gets the given value as a byte value of the type this provider is for.
    @NotNull Result<Double>
    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.
    @NotNull Result<Float>
    getFloat(@NotNull XmlElement type)
    Gets the given value as a float value of the type this provider is for.
    @NotNull Result<Integer>
    getInteger(@NotNull XmlElement type)
    Gets the given value as an integer value of the type this provider is for.
    getList(@NotNull XmlElement type)
    Gets the given value as a list value of the type this provider is for.
    @NotNull Result<Long>
    getLong(@NotNull XmlElement type)
    Gets the given value as a long value of the type this provider is for.
    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 on useRoot.
    @NotNull Result<Short>
    getShort(@NotNull XmlElement type)
    Gets the given value as a short value of the type this provider is for.
    @NotNull Result<String>
    getString(@NotNull XmlElement type)
    Gets the given value as a string value of the type this provider is for.
    @NotNull Result<Boolean>
    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.
    Returns a new instance of this class with useRoot set to true.
    If useRoot is already true, the same instance is returned.

    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

      private static final String 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

      private static final String EMPTY
      Constant for the name of the empty xml element type.
      See Also:
    • BOOLEAN

      private static final String BOOLEAN
      Constants for the name of boolean xml element type.
      See Also:
    • BYTE

      private static final String BYTE
      Constants for the name of byte xml element type.
      See Also:
    • SHORT

      private static final String SHORT
      Constants for the name of short xml element type.
      See Also:
    • INTEGER

      private static final String INTEGER
      Constants for the name of integer xml element type.
      See Also:
    • LONG

      private static final String LONG
      Constants for the name of long xml element type.
      See Also:
    • FLOAT

      private static final String FLOAT
      Constants for the name of float xml element type.
      See Also:
    • DOUBLE

      private static final String DOUBLE
      Constants for the name of double xml element type.
      See Also:
    • STRING

      private static final String STRING
      Constants for the name of string xml element type.
      See Also:
    • LIST

      private static final String LIST
      Constants for the name of list xml element type.
      See Also:
    • ELEMENT

      private static final String ELEMENT
      Constants for the name of list element xml element type.
      See Also:
    • MAP

      private static final String MAP
      Constants for the name of map xml element type.
      See Also:
    • ROOT

      private static final String ROOT
      Constants for the name of root xml element type.
      Used as name for maps if useRoot is true.
      See Also:
    • INSTANCE

      public static final XmlTypeProvider INSTANCE
      The singleton instance of this class.
    • useRoot

      private final boolean useRoot
      Whether to use ROOT 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

      @NotNull public @NotNull XmlTypeProvider useRoot()
      Returns a new instance of this class with useRoot set to true.
      If useRoot is already true, the same instance is returned.
      Returns:
      A new instance
    • empty

      @NotNull public @NotNull XmlElement empty()
      Description copied from interface: TypeProvider
      Creates an empty value of the type this provider is for.
      Specified by:
      empty in interface TypeProvider<XmlElement>
      Returns:
      An empty value
    • createBoolean

      @NotNull public @NotNull Result<XmlElement> createBoolean(boolean value)
      Description copied from interface: TypeProvider
      Creates a boolean value of the type this provider is for using the given value.
      Specified by:
      createBoolean in interface TypeProvider<XmlElement>
      Parameters:
      value - The value to create the boolean value from
      Returns:
      A result containing the boolean value
    • createByte

      @NotNull public @NotNull Result<XmlElement> createByte(byte value)
      Description copied from interface: TypeProvider
      Creates a byte value of the type this provider is for using the given value.
      Specified by:
      createByte in interface TypeProvider<XmlElement>
      Parameters:
      value - The value to create the byte value from
      Returns:
      A result containing the byte value
    • createShort

      @NotNull public @NotNull Result<XmlElement> createShort(short value)
      Description copied from interface: TypeProvider
      Creates a short value of the type this provider is for using the given value.
      Specified by:
      createShort in interface TypeProvider<XmlElement>
      Parameters:
      value - The value to create the short value from
      Returns:
      A result containing the short value
    • createInteger

      @NotNull public @NotNull Result<XmlElement> createInteger(int value)
      Description copied from interface: TypeProvider
      Creates an integer value of the type this provider is for using the given value.
      Specified by:
      createInteger in interface TypeProvider<XmlElement>
      Parameters:
      value - The value to create the integer value from
      Returns:
      A result containing the integer value
    • createLong

      @NotNull public @NotNull Result<XmlElement> createLong(long value)
      Description copied from interface: TypeProvider
      Creates a long value of the type this provider is for using the given value.
      Specified by:
      createLong in interface TypeProvider<XmlElement>
      Parameters:
      value - The value to create the long value from
      Returns:
      A result containing the long value
    • createFloat

      @NotNull public @NotNull Result<XmlElement> createFloat(float value)
      Description copied from interface: TypeProvider
      Creates a float value of the type this provider is for using the given value.
      Specified by:
      createFloat in interface TypeProvider<XmlElement>
      Parameters:
      value - The value to create the float value from
      Returns:
      A result containing the float value
    • createDouble

      @NotNull public @NotNull Result<XmlElement> createDouble(double value)
      Description copied from interface: TypeProvider
      Creates a double value of the type this provider is for using the given value.
      Specified by:
      createDouble in interface TypeProvider<XmlElement>
      Parameters:
      value - The value to create the double value from
      Returns:
      A result containing the double value
    • createString

      @NotNull public @NotNull Result<XmlElement> createString(@NotNull @NotNull String value)
      Description copied from interface: TypeProvider
      Creates a string value of the type this provider is for using the given value.
      Specified by:
      createString in interface TypeProvider<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 interface TypeProvider<XmlElement>
      Parameters:
      values - The values to create the list value from
      Returns:
      A result containing the list value
    • createMap

      @NotNull public @NotNull Result<XmlElement> createMap()
      Description copied from interface: TypeProvider
      Creates an empty map value of the type this provider is for.
      Specified by:
      createMap in interface TypeProvider<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 interface TypeProvider<XmlElement>
      Parameters:
      values - The values to create the map value from
      Returns:
      A result containing the map value
    • getEmpty

      @NotNull public @NotNull Result<XmlElement> getEmpty(@NotNull @NotNull XmlElement type)
      Description copied from interface: TypeProvider
      Gets the given value as an empty value of the type this provider is for.
      Specified by:
      getEmpty in interface TypeProvider<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

      @NotNull public @NotNull Result<Boolean> getBoolean(@NotNull @NotNull XmlElement type)
      Description copied from interface: TypeProvider
      Gets the given value as a boolean value of the type this provider is for.
      Specified by:
      getBoolean in interface TypeProvider<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

      @NotNull public @NotNull Result<Byte> getByte(@NotNull @NotNull XmlElement type)
      Description copied from interface: TypeProvider
      Gets the given value as a byte value of the type this provider is for.
      Specified by:
      getByte in interface TypeProvider<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

      @NotNull public @NotNull Result<Short> getShort(@NotNull @NotNull XmlElement type)
      Description copied from interface: TypeProvider
      Gets the given value as a short value of the type this provider is for.
      Specified by:
      getShort in interface TypeProvider<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

      @NotNull public @NotNull Result<Integer> getInteger(@NotNull @NotNull XmlElement type)
      Description copied from interface: TypeProvider
      Gets the given value as an integer value of the type this provider is for.
      Specified by:
      getInteger in interface TypeProvider<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

      @NotNull public @NotNull Result<Long> getLong(@NotNull @NotNull XmlElement type)
      Description copied from interface: TypeProvider
      Gets the given value as a long value of the type this provider is for.
      Specified by:
      getLong in interface TypeProvider<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

      @NotNull public @NotNull Result<Float> getFloat(@NotNull @NotNull XmlElement type)
      Description copied from interface: TypeProvider
      Gets the given value as a float value of the type this provider is for.
      Specified by:
      getFloat in interface TypeProvider<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

      @NotNull public @NotNull Result<Double> getDouble(@NotNull @NotNull XmlElement type)
      Description copied from interface: TypeProvider
      Gets the given value as a double value of the type this provider is for.
      Specified by:
      getDouble in interface TypeProvider<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

      @NotNull public @NotNull Result<String> getString(@NotNull @NotNull XmlElement type)
      Description copied from interface: TypeProvider
      Gets the given value as a string value of the type this provider is for.
      Specified by:
      getString in interface TypeProvider<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

      @NotNull public @NotNull Result<List<XmlElement>> getList(@NotNull @NotNull XmlElement type)
      Description copied from interface: TypeProvider
      Gets the given value as a list value of the type this provider is for.
      Specified by:
      getList in interface TypeProvider<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

      @NotNull public @NotNull Result<Map<String,XmlElement>> getMap(@NotNull @NotNull XmlElement type)
      Description copied from interface: TypeProvider
      Gets the given value as a map value of the type this provider is for.
      Specified by:
      getMap in interface TypeProvider<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 interface TypeProvider<XmlElement>
      Parameters:
      type - The value to check
      key - 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 interface TypeProvider<XmlElement>
      Parameters:
      type - The value to get the value from
      key - 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 interface TypeProvider<XmlElement>
      Parameters:
      type - The value to set the value in
      key - The key to set the value for
      value - 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 interface TypeProvider<XmlElement>
      Parameters:
      current - The current value
      value - 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

      @NotNull private @NotNull String getMapName()
      Returns the name for maps based on useRoot.
      Returns:
      The name for maps
    • escapeName

      @NotNull private @NotNull String escapeName(@NotNull @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.
      Parameters:
      name - The name to escape
      Returns:
      The escaped name
      Throws:
      NullPointerException - If the name is null
    • unescapeName

      @NotNull private @NotNull String unescapeName(@NotNull @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.
      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 element
      value - The element to copy
      Returns:
      The copied element
      Throws:
      NullPointerException - If the name or value is null
      IllegalStateException - 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 elements
      value - The value elements
      Returns:
      The merged array
      Throws:
      NullPointerException - If the current or value elements are null
      XmlTypeException - 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 elements
      value - The value elements
      Returns:
      The merged object as map
      Throws:
      NullPointerException - If the current or value elements are null
      XmlTypeException - 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 elements
      value - The value elements
      Returns:
      A result containing the merged container or an error
      Throws:
      NullPointerException - If the current or value elements are null