java.lang.Object
net.luis.utils.io.data.xml.XmlElement
- Direct Known Subclasses:
XmlContainer
,XmlValue
Represents a xml element.
A xml element can be a self-closing element, a container element or a value 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 Summary
FieldsModifier and TypeFieldDescriptionprivate final XmlAttributes
The attributes of the xml element.private final String
The name of the xml element. -
Constructor Summary
ConstructorsConstructorDescriptionXmlElement
(@NotNull String name) Constructs a new xml element with the specified name and no attributes.XmlElement
(@NotNull String name, @NotNull XmlAttributes attributes) Constructs a new xml element with the specified name and attributes. -
Method Summary
Modifier and TypeMethodDescription@Nullable XmlAttribute
addAttribute
(@NotNull String key, boolean value) Adds a new attribute with the given name and boolean value to this element.@Nullable XmlAttribute
addAttribute
(@NotNull String key, byte value) Adds a new attribute with the given name and byte value to this element.@Nullable XmlAttribute
addAttribute
(@NotNull String key, double value) Adds a new attribute with the given name and double value to this element.@Nullable XmlAttribute
addAttribute
(@NotNull String key, float value) Adds a new attribute with the given name and float value to this element.@Nullable XmlAttribute
addAttribute
(@NotNull String key, int value) Adds a new attribute with the given name and integer value to this element.@Nullable XmlAttribute
addAttribute
(@NotNull String key, long value) Adds a new attribute with the given name and long value to this element.@Nullable XmlAttribute
addAttribute
(@NotNull String key, short value) Adds a new attribute with the given name and short value to this element.@Nullable XmlAttribute
addAttribute
(@NotNull String key, @Nullable Number value) Adds a new attribute with the given name and number value to this element.@Nullable XmlAttribute
addAttribute
(@NotNull String key, @Nullable String value) Adds a new attribute with the given name and string value to this element.@Nullable XmlAttribute
addAttribute
(@NotNull XmlAttribute attribute) Adds the specified attribute to the element.boolean
@NotNull XmlContainer
Converts this xml element to a xml container.@NotNull XmlValue
Converts this xml element to a xml value.@Nullable XmlAttribute
getAttribute
(@NotNull String key) Returns the attribute with the specified key, or null if there is no such attribute in this element.<T> T
getAttributeAs
(@NotNull String key, @NotNull ThrowableFunction<String, T, ? extends Exception> parser) Returns the attribute with the specified key as the type specified by the parser.boolean
getAttributeAsBoolean
(@NotNull String key) Returns the attribute with the specified key as a boolean.byte
getAttributeAsByte
(@NotNull String key) Returns the attribute with the specified key as a byte.double
getAttributeAsDouble
(@NotNull String key) Returns the attribute with the specified key as a double.float
getAttributeAsFloat
(@NotNull String key) Returns the attribute with the specified key as a float.int
getAttributeAsInteger
(@NotNull String key) Returns the attribute with the specified key as an integer.long
getAttributeAsLong
(@NotNull String key) Returns the attribute with the specified key as a long.@NotNull Number
getAttributeAsNumber
(@NotNull String key) Returns the attribute with the specified key as a number.short
getAttributeAsShort
(@NotNull String key) Returns the attribute with the specified key as a short.@NotNull String
getAttributeAsString
(@NotNull String key) Returns the attribute with the specified key as a string.@NotNull XmlAttributes
Returns the attributes of the xml element.protected @NotNull String
Returns the type of the xml element.
This is used internally for error messages.@NotNull String
getName()
Returns the name of the xml element.int
hashCode()
boolean
Checks if the xml element is self-closing.boolean
Checks if the xml element is a container.boolean
Checks if the xml element is a value.protected @NotNull StringBuilder
toBaseString
(@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.toString()
@NotNull String
The string representation of this xml element.
-
Field Details
-
name
The name of the xml element. -
attributes
The attributes of the xml element.
-
-
Constructor Details
-
XmlElement
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 nullIllegalArgumentException
- If the name is invalid
-
XmlElement
Constructs a new xml element with the specified name and attributes.- Parameters:
name
- The name of the xml elementattributes
- The attributes of the xml element- Throws:
NullPointerException
- If the name or attributes are nullIllegalArgumentException
- If the name is invalid
-
-
Method Details
-
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
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
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
Returns the name of the xml element.- Returns:
- The element name
-
getAttributes
Returns the attributes of the xml element.- Returns:
- The element attributes (modifiable)
-
addAttribute
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 attributevalue
- The value of the attribute- Returns:
- The previous attribute with the same key, or null if there was no previous attribute
- See Also:
-
addAttribute
Adds a new attribute with the given name and boolean value to this element.- Parameters:
key
- The key of the attributevalue
- 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 attributevalue
- The value of the attribute- Returns:
- The previous attribute with the same key, or null if there was no previous attribute
- See Also:
-
addAttribute
Adds a new attribute with the given name and byte value to this element.- Parameters:
key
- The key of the attributevalue
- The value of the attribute- Returns:
- The previous attribute with the same key, or null if there was no previous attribute
- See Also:
-
addAttribute
Adds a new attribute with the given name and short value to this element.- Parameters:
key
- The key of the attributevalue
- The value of the attribute- Returns:
- The previous attribute with the same key, or null if there was no previous attribute
- See Also:
-
addAttribute
Adds a new attribute with the given name and integer value to this element.- Parameters:
key
- The key of the attributevalue
- The value of the attribute- Returns:
- The previous attribute with the same key, or null if there was no previous attribute
- See Also:
-
addAttribute
Adds a new attribute with the given name and long value to this element.- Parameters:
key
- The key of the attributevalue
- The value of the attribute- Returns:
- The previous attribute with the same key, or null if there was no previous attribute
- See Also:
-
addAttribute
Adds a new attribute with the given name and float value to this element.- Parameters:
key
- The key of the attributevalue
- The value of the attribute- Returns:
- The previous attribute with the same key, or null if there was no previous attribute
- See Also:
-
addAttribute
Adds a new attribute with the given name and double value to this element.- Parameters:
key
- The key of the attributevalue
- The value of the attribute- Returns:
- The previous attribute with the same key, or null if there was no previous attribute
- See Also:
-
getAttribute
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
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
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
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
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
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
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
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
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
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 attributeparser
- The parser to convert the attribute value to the desired type- Returns:
- The value of the attribute as the specified type
- See Also:
-
equals
-
hashCode
public int hashCode() -
toString
-
toBaseString
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 nullIllegalStateException
- If the element cannot be represented as a string because of the xml config
-
toString
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
-