java.lang.Object
net.luis.utils.io.data.xml.XmlReader
- All Implemented Interfaces:
AutoCloseable
A xml reader that reads xml content from a
The reader expects a xml declaration at the beginning of the content, which can be read with
After reading the declaration, the xml root element can be read with
string
or input provider
.The reader expects a xml declaration at the beginning of the content, which can be read with
readDeclaration()
.After reading the declaration, the xml root element can be read with
readXmlElement()
.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String
The pattern to match comments in xml content.private final XmlConfig
The xml config of this reader.The possible attributes of a xml declaration.private boolean
A flag to indicate if the xml declaration has been read.private final ScopedStringReader
The internal reader used to read the xml content. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new xml reader with the given string and default xml config.Constructs a new xml reader with the given string and xml config.XmlReader
(@NotNull InputProvider input) Constructs a new xml reader with the given input provider and default xml config.XmlReader
(@NotNull InputProvider input, @NotNull XmlConfig config) Constructs a new xml reader with the given input provider and xml config. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
private static @NotNull String
deleteComments
(@NotNull String string) Deletes all xml comments from the given string.private int
getClosingElement
(@NotNull ScopedStringReader xmlReader, @NotNull String name) Gets the number of characters until the closing element of the given element name.@NotNull XmlDeclaration
Reads the xml declaration from the xml content.private @NotNull XmlElements
readeXmlElements
(@NotNull ScopedStringReader xmlReader) Reads the xml elements from the given xml reader.private @NotNull XmlAttributes
readXmlAttributes
(@NotNull StringReader attributeReader) Reads the xml attributes from the given attribute reader.@NotNull XmlElement
Reads the xml root element from the xml content.private @NotNull XmlElement
readXmlElement
(@NotNull ScopedStringReader xmlReader) Reads a xml element from the given xml reader.private void
skipWhitespacesConfigBased
(@NotNull StringReader reader) Skips the next whitespace character based on the xml config.
In strict mode, only the next whitespace character is skipped; otherwise all whitespaces are skipped.
-
Field Details
-
DECLARATION_ATTRIBUTES
The possible attributes of a xml declaration. -
COMMENT_PATTERN
The pattern to match comments in xml content.- See Also:
-
config
The xml config of this reader. -
reader
The internal reader used to read the xml content. -
readDeclaration
private boolean readDeclarationA flag to indicate if the xml declaration has been read.
-
-
Constructor Details
-
XmlReader
Constructs a new xml reader with the given string and default xml config.- Parameters:
string
- The string to read the xml content from- Throws:
NullPointerException
- If the string is null
-
XmlReader
Constructs a new xml reader with the given string and xml config.- Parameters:
string
- The string to read the xml content fromconfig
- The xml config to use- Throws:
NullPointerException
- If the string or xml config is null
-
XmlReader
Constructs a new xml reader with the given input provider and default xml config.- Parameters:
input
- The input provider to read the xml content from- Throws:
NullPointerException
- If the input provider is null
-
XmlReader
Constructs a new xml reader with the given input provider and xml config.- Parameters:
input
- The input provider to read the xml content fromconfig
- The xml config to use- Throws:
NullPointerException
- If the input provider or xml config is null
-
-
Method Details
-
deleteComments
Deletes all xml comments from the given string.- Parameters:
string
- The string to delete the comments from- Returns:
- The string without any xml comments
- Throws:
NullPointerException
- If the string is null
-
readDeclaration
Reads the xml declaration from the xml content.- Returns:
- The xml declaration of the xml content
- Throws:
IllegalStateException
- If the xml declaration has already been readXmlSyntaxException
- If the xml declaration is invalid
-
readXmlElement
Reads the xml root element from the xml content.- Returns:
- The xml root element read
- Throws:
IllegalStateException
- If the xml declaration has not been readXmlSyntaxException
- If the xml content is invalid
-
readXmlElement
Reads a xml element from the given xml reader.- Parameters:
xmlReader
- The xml reader to read the xml element from- Returns:
- The xml element read
- Throws:
XmlSyntaxException
- If the xml element is invalid
-
readXmlAttributes
@NotNull private @NotNull XmlAttributes readXmlAttributes(@NotNull @NotNull StringReader attributeReader) Reads the xml attributes from the given attribute reader.- Parameters:
attributeReader
- The attribute reader to read the xml attributes from- Returns:
- The xml attributes read
- Throws:
XmlSyntaxException
- If the xml attributes are invalid
-
getClosingElement
private int getClosingElement(@NotNull @NotNull ScopedStringReader xmlReader, @NotNull @NotNull String name) Gets the number of characters until the closing element of the given element name.- Parameters:
xmlReader
- The xml reader to read the closing element fromname
- The name of the element to get the closing element for- Returns:
- The number of characters
- Throws:
XmlSyntaxException
- If any syntax error occurs while reading the closing element
-
readeXmlElements
@NotNull private @NotNull XmlElements readeXmlElements(@NotNull @NotNull ScopedStringReader xmlReader) Reads the xml elements from the given xml reader.- Parameters:
xmlReader
- The xml reader to read the xml elements from- Returns:
- The xml elements read
- Throws:
XmlSyntaxException
- If the xml elements are invalid
-
skipWhitespacesConfigBased
Skips the next whitespace character based on the xml config.
In strict mode, only the next whitespace character is skipped; otherwise all whitespaces are skipped.- Parameters:
reader
- The reader to skip the whitespace character from
-
close
- Specified by:
close
in interfaceAutoCloseable
- Throws:
Exception
-