java.lang.Object
com.thecoderscorner.menu.persist.XMLDOMHelper

public class XMLDOMHelper extends Object
A series of static helper method that make dealing with XML easier, this is because many areas of tcMenu deal with XML documents, and these functions are generally useful.
  • Constructor Details

    • XMLDOMHelper

      public XMLDOMHelper()
  • Method Details

    • loadDocumentFromPath

      public static Document loadDocumentFromPath(Path filePath) throws IOException, ParserConfigurationException, SAXException
      Load an XML document from a file system path
      Parameters:
      filePath - the path to load from
      Returns:
      an XML document if successful, otherwise throws
      Throws:
      IOException - for any IO problems
      ParserConfigurationException - if the document does not parse
      SAXException - if the document does not parse
    • loadDocumentFromData

      public static Document loadDocumentFromData(String data) throws IOException, ParserConfigurationException, SAXException
      Load an XML document from a string of data
      Parameters:
      data - the data to load from
      Returns:
      an XML document if successful, otherwise throws
      Throws:
      IOException - for any IO problems
      ParserConfigurationException - if the document does not parse
      SAXException - if the document does not parse
    • loadDocumentStream

      public static Document loadDocumentStream(InputStream stream) throws ParserConfigurationException, IOException, SAXException
      Load an XML document from a stream
      Parameters:
      stream - the steam to load from
      Returns:
      an XML document if successful, otherwise throws
      Throws:
      IOException - for any IO problems
      ParserConfigurationException - if the document does not parse
      SAXException - if the document does not parse
    • getChildElementsWithName

      public static List<Element> getChildElementsWithName(Element ele, String name)
      Find all child elements with a given name
      Parameters:
      ele - the parent element
      name - the name to search for
      Returns:
      a list of elements directly under ele that are named `name`
    • textOfElementByName

      public static String textOfElementByName(Element elem, String child)
      Get the text of an element with the name child. Saves looking up the element and then getting text.
      Parameters:
      elem - the parent element
      child - the name of the child to get the text of
      Returns:
      the text content or blank.
    • integerOfElementByName

      public static int integerOfElementByName(Element element, String child, int def)
      Gets the int value associated with the named child, or the default if it does not exist or can't be parsed.
      Parameters:
      element - the parent element
      child - the name of the child element
      def - the default value in case of problem
    • elementWithName

      public static Element elementWithName(Element elem, String child)
      Gets an element with a given name or null
      Parameters:
      elem - the parent element
      child - the child to locate
      Returns:
      the element or null
    • getAttrOrNull

      public static String getAttrOrNull(Element ele, String attr)
      Gets the text of attribute if it is available, or null
      Parameters:
      ele - the element
      attr - the attribute name
      Returns:
      the text of the attribute or null
    • getAttributeOrDefault

      public static String getAttributeOrDefault(Element elem, String val, Object def)
      Gets the text of an attribute or a default value.
      Parameters:
      elem - the element
      val - the attribute name
      def - the default value
      Returns:
      the attribute text or otherwise the default
    • transformElements

      public static <T> List<T> transformElements(Element root, String eleName, String childName, Function<Element,T> transform)
      Transforms all elements that match `childName` under either `root` if eleName is null, or under root/eleName if eleName is provided. You provide the transformation function that takes an Element and returns the desired result
      Type Parameters:
      T - can be any type you wish to return in your transformation
      Parameters:
      root - the root element to start at
      eleName - optionally, another level of indirection (can be null) EG root/eleName
      childName - the items to filter for
      transform - the transformation to apply - returns T, takes an element
      Returns:
      the transformed list or an empty list.
    • transformElements

      public static <T> List<T> transformElements(Element root, String childName, Function<Element,T> transform)
      Transforms all elements that match `childName` under `root`, there is another overload that provides indirection. You provide the transformation function that takes an Element and returns the desired result.
      Type Parameters:
      T - can be any type you wish to return in your transformation
      Parameters:
      root - the root element to start at
      childName - the items to filter for
      transform - the transformation to apply - returns T, takes an element
      Returns:
      the transformed list or an empty list.
    • writeXml

      public static void writeXml(Document doc, OutputStream output) throws TransformerException
      Writes an XML document without reformatting it, this is good if you are working with a DOM that was previously loaded from a stream, where it may be already formatted.
      Parameters:
      doc - the document
      output - the output stream to write to
      Throws:
      TransformerException - if the XML cannot be transformed
    • writeXml

      public static void writeXml(Document doc, OutputStream output, boolean prettyPrint) throws TransformerException
      Writes an XML document to a stream
      Parameters:
      doc - the document
      output - the stream to write to
      prettyPrint - if the document should be formatted
      Throws:
      TransformerException - if it cannot be written
    • newDocumentRoot

      public static Document newDocumentRoot(String name) throws ParserConfigurationException
      Creates a new document that has a root element with the name given
      Parameters:
      name - the root element name
      Returns:
      a document with root node attached
      Throws:
      ParserConfigurationException - if the document cannot be created
    • appendElementWithNameValue

      public static Element appendElementWithNameValue(Element parent, String name, Object value)
      Append a new element into the parent provided with the name given.
      Parameters:
      parent - the parent where the new element is to be placed
      name - the name of the new element
      value - the value to set as text (can be null)
      Returns:
      the newly created element