Class XmlReader

java.lang.Object
net.luis.utils.io.data.xml.XmlReader
All Implemented Interfaces:
AutoCloseable

public class XmlReader extends Object implements AutoCloseable
A xml reader that reads xml content from a 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 Details

    • DECLARATION_ATTRIBUTES

      private static final List<String> DECLARATION_ATTRIBUTES
      The possible attributes of a xml declaration.
    • COMMENT_PATTERN

      private static final String COMMENT_PATTERN
      The pattern to match comments in xml content.
      See Also:
    • config

      private final XmlConfig config
      The xml config of this reader.
    • reader

      private final ScopedStringReader reader
      The internal reader used to read the xml content.
    • readDeclaration

      private boolean readDeclaration
      A flag to indicate if the xml declaration has been read.
  • Constructor Details

    • XmlReader

      public XmlReader(@NotNull @NotNull String string)
      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

      public XmlReader(@NotNull @NotNull String string, @NotNull @NotNull XmlConfig config)
      Constructs a new xml reader with the given string and xml config.
      Parameters:
      string - The string to read the xml content from
      config - The xml config to use
      Throws:
      NullPointerException - If the string or xml config is null
    • XmlReader

      public XmlReader(@NotNull @NotNull InputProvider input)
      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

      public XmlReader(@NotNull @NotNull InputProvider input, @NotNull @NotNull XmlConfig config)
      Constructs a new xml reader with the given input provider and xml config.
      Parameters:
      input - The input provider to read the xml content from
      config - The xml config to use
      Throws:
      NullPointerException - If the input provider or xml config is null
  • Method Details

    • deleteComments

      @NotNull private static @NotNull String deleteComments(@NotNull @NotNull String string)
      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

      @NotNull public @NotNull XmlDeclaration 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 read
      XmlSyntaxException - If the xml declaration is invalid
    • readXmlElement

      @NotNull public @NotNull XmlElement 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 read
      XmlSyntaxException - If the xml content is invalid
    • readXmlElement

      @NotNull private @NotNull XmlElement readXmlElement(@NotNull @NotNull ScopedStringReader xmlReader)
      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 from
      name - 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

      private void skipWhitespacesConfigBased(@NotNull @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.
      Parameters:
      reader - The reader to skip the whitespace character from
    • close

      public void close() throws Exception
      Specified by:
      close in interface AutoCloseable
      Throws:
      Exception