Class XMLUtilities

java.lang.Object
com.iizix.XMLUtilities

public class XMLUtilities extends Object
Helper class containing utility methods used when processing XML documents.
Author:
Christopher Mindus
  • Field Details

  • Constructor Details

    • XMLUtilities

      public XMLUtilities()
  • Method Details

    • createDocument

      public static Document createDocument(boolean namespaceAware)
      Creates a new empty Document, namespace aware or not, from the calling thread's cached factory of that kind (CC-FIX-VTHREAD-SMALLS, VF-D-3 of CC-FIX-VTHREAD-FOLLOWUP). The save path of IIZI's files (PropFactory.createDocument, not namespace aware) and Utilities.createDocument (namespace aware) resolved a factory per document before, as the load path did until DL-D-4: the lookup, the cache and its thread confinement are those of newDocumentBuilder(boolean). The builder is created per document, as the parser is per parse.
      Parameters:
      namespaceAware - true for a namespace aware document, false for one that is not (IIZI's own files).
      Returns:
      The new Document.
      Throws:
      FactoryConfigurationError - If no factory can be resolved, at the thread's first document of the kind.
    • createDocument

      public static Document createDocument()
      Creates a new empty Document from the document builder factory of foreign XML (namespace aware). This method should not be used to create .iizi* documents: the save path uses createDocument(boolean).
      Returns:
      The new Document.
    • createLoader

      public static Transformer createLoader(URL schemaURL) throws IOException
      Creates a transformer from an XSL schema used to load multiple XML files to be validated.
      Parameters:
      schemaURL - The schema URL for the XSL schema.
      Returns:
      The XML transformer to use.
      Throws:
      IOException - For failures in creating the transformer for the URL.
    • loadXMLDocument

      public static Document loadXMLDocument(String data) throws IOException, SAXException
      Loads an XML document from a String into a Document.
      Parameters:
      data - The String.
      Returns:
      the loaded document.
      Throws:
      IOException - for file errors.
      SAXException - a SAX exception when loading/parsing the XML file.
    • loadXMLDocument

      public static Document loadXMLDocument(File file) throws IOException, SAXException
      Loads an XML document from a file into a Document without schema validation.
      Parameters:
      file - The file to load.
      Returns:
      the loaded document.
      Throws:
      IOException - for file errors.
      SAXException - a SAX exception when loading/parsing the XML file.
    • loadXMLDocument

      public static Document loadXMLDocument(URL url) throws IOException, SAXException
      Loads an XML document from a file into a Document without schema validation.
      Parameters:
      url - The file to load.
      Returns:
      the loaded document.
      Throws:
      IOException - for file errors.
      SAXException - a SAX exception when loading/parsing the XML file.
    • loadXMLDocument

      public static Document loadXMLDocument(InputStream in) throws IOException, SAXException
      Loads an XML document from a file into a Document without schema validation.
      Parameters:
      in - The input stream.
      Returns:
      the loaded document.
      Throws:
      IOException - for file errors.
      SAXException - a SAX exception when loading/parsing the XML file.
    • loadForeignXMLDocument

      public static Document loadForeignXMLDocument(String data) throws IOException, SAXException
      Loads XML that IIZI did not write, such as an SVG asset, from a String; see newForeignDocumentBuilder().
      Parameters:
      data - The String.
      Returns:
      the loaded document.
      Throws:
      IOException - for file errors.
      SAXException - a SAX exception when loading/parsing the XML.
    • loadForeignXMLDocument

      public static Document loadForeignXMLDocument(File file) throws IOException, SAXException
      Loads XML that IIZI did not write, such as an SVG asset, from a file; see newForeignDocumentBuilder().
      Parameters:
      file - The file to load.
      Returns:
      the loaded document.
      Throws:
      IOException - for file errors.
      SAXException - a SAX exception when loading/parsing the XML.
    • loadForeignXMLDocument

      public static Document loadForeignXMLDocument(URL url) throws IOException, SAXException
      Loads XML that IIZI did not write, such as an SVG asset, from a URL; see newForeignDocumentBuilder().
      Parameters:
      url - The URL to load.
      Returns:
      the loaded document.
      Throws:
      IOException - for file errors.
      SAXException - a SAX exception when loading/parsing the XML.
    • getXMLDocument

      public static String getXMLDocument(Document document) throws TransformerException
      Gets a Document as a String.
      Parameters:
      document - The document to save.
      Returns:
      The XML representation of the document.
      Throws:
      TransformerException - For document errors.
      TransformerFactoryConfigurationError - For XML factory errors.
    • getNodeString

      public static String getNodeString(Node node) throws TransformerException
      Gets a Node as a String.
      Parameters:
      node - The document to save.
      Returns:
      The XML representation of the node.
      Throws:
      TransformerException - For document errors.
      TransformerFactoryConfigurationError - For XML factory errors.
    • saveXMLDocument

      public static void saveXMLDocument(Document document, File file) throws IOException, TransformerException
      Saves a Document to a File.
      Parameters:
      document - The document to save.
      file - The file to use for document saving.
      Throws:
      IOException - For file errors.
      TransformerException - For document errors.
    • saveXMLDocument

      public static void saveXMLDocument(Document document, OutputStream output) throws IOException, TransformerException
      Saves a Document to an output stream.
      Parameters:
      document - The document to save.
      output - The output stream.
      Throws:
      IOException - For file errors.
      TransformerException - For document errors.
    • saveXMLDocument

      public static void saveXMLDocument(Document document, Writer writer) throws IOException, TransformerException
      Saves a Document to a writer.
      Parameters:
      document - The document to save.
      writer - The writer.
      Throws:
      IOException - For file errors.
      TransformerException - For document errors.
    • list

      public static void list(Document doc)
      Lists a Document.
      Parameters:
      doc - The document to list.
    • list

      public static void list(Node node, int indent)
      Lists a Node and it's tree.
      Parameters:
      node - The node to list.
      indent - The indent level.
    • getAttributeString

      public static String getAttributeString(Node node, String name)
      Gets an attribute from a node.
      Parameters:
      node - the node.
      name - name of attribute.
      Returns:
      the string value of the attribute or null if it's not found.
    • getAttributeString

      public static String getAttributeString(NamedNodeMap namedNodeMap, String name)
      Gets an attribute from a node attribute list.
      Parameters:
      namedNodeMap - the node's attributes.
      name - name of attribute.
      Returns:
      the string value of the attribute or null if it's not found.
    • getAttributeString

      public static String getAttributeString(Node node, String name, String defaultValue)
      Gets an attribute from a node.
      Parameters:
      node - the node.
      name - name of attribute.
      defaultValue - the default value to use if attribute is not found.
      Returns:
      the string value of the attribute or defaultValue if it's not found.
    • getAttributeString

      public static String getAttributeString(NamedNodeMap namedNodeMap, String name, String defaultValue)
      Gets an attribute from a node attribute list.
      Parameters:
      namedNodeMap - the node's attributes.
      name - name of attribute.
      defaultValue - the default value to use if attribute is not found.
      Returns:
      the string value of the attribute or defaultValue if it's not found.
    • getFirstElement

      public static Element getFirstElement(Element parent, String name)
      Gets the first Element with the specified name.
      Parameters:
      parent - The parent element.
      name - The name.
      Returns:
      null if not found.