Class XmlElement

java.lang.Object
net.luis.utils.io.data.xml.XmlElement
Direct Known Subclasses:
XmlContainer, XmlValue

public sealed class XmlElement extends Object permits XmlContainer, XmlValue
Represents a xml element.
A xml element can be a self-closing element, a container element or a value element.

A self-closing element is an element that does not have any content.
It is represented by a tag with a name and optional attributes.

A container element is an element that contains other elements.
It is represented by a tag with a name, optional attributes and a closing tag.
The containing elements have either all the same name or unique names.

A value element is an element that contains a value.
It is represented by a tag with a name, optional attributes and a value.
The value can be a string, a number or a boolean.

See Also:
  • Field Details

    • name

      private final String name
      The name of the xml element.
    • attributes

      private final XmlAttributes attributes
      The attributes of the xml element.
  • Constructor Details

    • XmlElement

      public XmlElement(@NotNull @NotNull String name)
      Constructs a new xml element with the specified name and no attributes.
      Parameters:
      name - The name of the xml element
      Throws:
      NullPointerException - If the name is null
      IllegalArgumentException - If the name is invalid
    • XmlElement

      public XmlElement(@NotNull @NotNull String name, @NotNull @NotNull XmlAttributes attributes)
      Constructs a new xml element with the specified name and attributes.
      Parameters:
      name - The name of the xml element
      attributes - The attributes of the xml element
      Throws:
      NullPointerException - If the name or attributes are null
      IllegalArgumentException - If the name is invalid
  • Method Details

    • getElementType

      @NotNull protected @NotNull String getElementType()
      Returns the type of the xml element.
      This is used internally for error messages.
      Returns:
      The type of the xml element
    • isSelfClosing

      public boolean isSelfClosing()
      Checks if the xml element is self-closing.
      Returns:
      Always true
    • isXmlContainer

      public boolean isXmlContainer()
      Checks if the xml element is a container.
      Returns:
      True if the xml element is a container, false otherwise
    • isXmlValue

      public boolean isXmlValue()
      Checks if the xml element is a value.
      Returns:
      True if the xml element is a value, false otherwise
    • getAsXmlContainer

      @NotNull public @NotNull XmlContainer getAsXmlContainer()
      Converts this xml element to a xml container.
      Returns:
      This xml element as a xml container
      Throws:
      XmlTypeException - If this xml element is not a xml container
    • getAsXmlValue

      @NotNull public @NotNull XmlValue getAsXmlValue()
      Converts this xml element to a xml value.
      Returns:
      This xml element as a xml value
      Throws:
      XmlTypeException - If this xml element is not a xml value
    • getName

      @NotNull public @NotNull String getName()
      Returns the name of the xml element.
      Returns:
      The element name
    • getAttributes

      @NotNull public @NotNull XmlAttributes getAttributes()
      Returns the attributes of the xml element.
      Returns:
      The element attributes (modifiable)
    • addAttribute

      @Nullable public @Nullable XmlAttribute addAttribute(@NotNull @NotNull XmlAttribute attribute)
      Adds the specified attribute to the element.
      Parameters:
      attribute - The attribute to add
      Returns:
      The previous attribute with the same key, or null if there was no previous attribute
      See Also:
    • addAttribute

      @Nullable public @Nullable XmlAttribute addAttribute(@NotNull @NotNull String key, @Nullable @Nullable String value)
      Adds a new attribute with the given name and string value to this element.
      Parameters:
      key - The key of the attribute
      value - The value of the attribute
      Returns:
      The previous attribute with the same key, or null if there was no previous attribute
      See Also:
    • addAttribute

      @Nullable public @Nullable XmlAttribute addAttribute(@NotNull @NotNull String key, boolean value)
      Adds a new attribute with the given name and boolean value to this element.
      Parameters:
      key - The key of the attribute
      value - The value of the attribute
      Returns:
      The previous attribute with the same key, or null if there was no previous attribute
      See Also:
    • addAttribute

      @Nullable public @Nullable XmlAttribute addAttribute(@NotNull @NotNull String key, @Nullable @Nullable Number value)
      Adds a new attribute with the given name and number value to this element.
      Parameters:
      key - The key of the attribute
      value - The value of the attribute
      Returns:
      The previous attribute with the same key, or null if there was no previous attribute
      See Also:
    • addAttribute

      @Nullable public @Nullable XmlAttribute addAttribute(@NotNull @NotNull String key, byte value)
      Adds a new attribute with the given name and byte value to this element.
      Parameters:
      key - The key of the attribute
      value - The value of the attribute
      Returns:
      The previous attribute with the same key, or null if there was no previous attribute
      See Also:
    • addAttribute

      @Nullable public @Nullable XmlAttribute addAttribute(@NotNull @NotNull String key, short value)
      Adds a new attribute with the given name and short value to this element.
      Parameters:
      key - The key of the attribute
      value - The value of the attribute
      Returns:
      The previous attribute with the same key, or null if there was no previous attribute
      See Also:
    • addAttribute

      @Nullable public @Nullable XmlAttribute addAttribute(@NotNull @NotNull String key, int value)
      Adds a new attribute with the given name and integer value to this element.
      Parameters:
      key - The key of the attribute
      value - The value of the attribute
      Returns:
      The previous attribute with the same key, or null if there was no previous attribute
      See Also:
    • addAttribute

      @Nullable public @Nullable XmlAttribute addAttribute(@NotNull @NotNull String key, long value)
      Adds a new attribute with the given name and long value to this element.
      Parameters:
      key - The key of the attribute
      value - The value of the attribute
      Returns:
      The previous attribute with the same key, or null if there was no previous attribute
      See Also:
    • addAttribute

      @Nullable public @Nullable XmlAttribute addAttribute(@NotNull @NotNull String key, float value)
      Adds a new attribute with the given name and float value to this element.
      Parameters:
      key - The key of the attribute
      value - The value of the attribute
      Returns:
      The previous attribute with the same key, or null if there was no previous attribute
      See Also:
    • addAttribute

      @Nullable public @Nullable XmlAttribute addAttribute(@NotNull @NotNull String key, double value)
      Adds a new attribute with the given name and double value to this element.
      Parameters:
      key - The key of the attribute
      value - The value of the attribute
      Returns:
      The previous attribute with the same key, or null if there was no previous attribute
      See Also:
    • getAttribute

      @Nullable public @Nullable XmlAttribute getAttribute(@NotNull @NotNull String key)
      Returns the attribute with the specified key, or null if there is no such attribute in this element.
      Parameters:
      key - The key of the attribute
      Returns:
      The attribute with the specified key, or null if there is no such attribute
      See Also:
    • getAttributeAsString

      @NotNull public @NotNull String getAttributeAsString(@NotNull @NotNull String key)
      Returns the attribute with the specified key as a string.
      Parameters:
      key - The key of the attribute
      Returns:
      The value of the attribute as a string
      See Also:
    • getAttributeAsBoolean

      public boolean getAttributeAsBoolean(@NotNull @NotNull String key)
      Returns the attribute with the specified key as a boolean.
      Parameters:
      key - The key of the attribute
      Returns:
      The value of the attribute as a boolean
      See Also:
    • getAttributeAsNumber

      @NotNull public @NotNull Number getAttributeAsNumber(@NotNull @NotNull String key)
      Returns the attribute with the specified key as a number.
      Parameters:
      key - The key of the attribute
      Returns:
      The value of the attribute as a number
      See Also:
    • getAttributeAsByte

      public byte getAttributeAsByte(@NotNull @NotNull String key)
      Returns the attribute with the specified key as a byte.
      Parameters:
      key - The key of the attribute
      Returns:
      The value of the attribute as a byte
      See Also:
    • getAttributeAsShort

      public short getAttributeAsShort(@NotNull @NotNull String key)
      Returns the attribute with the specified key as a short.
      Parameters:
      key - The key of the attribute
      Returns:
      The value of the attribute as a short
      See Also:
    • getAttributeAsInteger

      public int getAttributeAsInteger(@NotNull @NotNull String key)
      Returns the attribute with the specified key as an integer.
      Parameters:
      key - The key of the attribute
      Returns:
      The value of the attribute as an integer
      See Also:
    • getAttributeAsLong

      public long getAttributeAsLong(@NotNull @NotNull String key)
      Returns the attribute with the specified key as a long.
      Parameters:
      key - The key of the attribute
      Returns:
      The value of the attribute as a long
      See Also:
    • getAttributeAsFloat

      public float getAttributeAsFloat(@NotNull @NotNull String key)
      Returns the attribute with the specified key as a float.
      Parameters:
      key - The key of the attribute
      Returns:
      The value of the attribute as a float
      See Also:
    • getAttributeAsDouble

      public double getAttributeAsDouble(@NotNull @NotNull String key)
      Returns the attribute with the specified key as a double.
      Parameters:
      key - The key of the attribute
      Returns:
      The value of the attribute as a double
      See Also:
    • getAttributeAs

      @NotNull public <T> T getAttributeAs(@NotNull @NotNull String key, @NotNull @NotNull ThrowableFunction<String,T,? extends Exception> parser)
      Returns the attribute with the specified key as the type specified by the parser.
      Type Parameters:
      T - The type of the value
      Parameters:
      key - The key of the attribute
      parser - The parser to convert the attribute value to the desired type
      Returns:
      The value of the attribute as the specified type
      See Also:
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toBaseString

      @NotNull protected @NotNull StringBuilder toBaseString(@NotNull @NotNull XmlConfig config)
      Returns a string builder with the base string representation of this xml element.
      The base string representation contains the root tag with the name and attributes.
      Parameters:
      config - The xml config to use for the string representation
      Returns:
      The string builder
      Throws:
      NullPointerException - If the xml config is null
      IllegalStateException - If the element cannot be represented as a string because of the xml config
    • toString

      @NotNull public @NotNull String toString(@NotNull @NotNull XmlConfig config)
      The string representation of this xml element.
      Parameters:
      config - The xml config to use for the string representation
      Returns:
      The string representation
      Throws:
      NullPointerException - If the xml config is null