Class XmlUtil

java.lang.Object
org.faktorips.runtime.internal.XmlUtil

public class XmlUtil extends Object
A collection of utility methods for XML DOM handling.
  • Field Details

    • FAKTOR_IPS_SCHEMA_URL

      public static final String FAKTOR_IPS_SCHEMA_URL
      See Also:
    • W3C_XML_SCHEMA_INSTANCE_NS_URI

      public static final String W3C_XML_SCHEMA_INSTANCE_NS_URI
      Used for the project org.faktorips.devtools.stdbuilder because it uses jaxb-api as dependency. Jaxb-api uses the same javax.xml package as XMLConstants from the JDK and therefore causes the error: The package javax.xml is accessible from more than one module: <unnamed>, java.xml
      See Also:
    • XMLNS_ATTRIBUTE

      public static final String XMLNS_ATTRIBUTE
      Used for the project org.faktorips.devtools.stdbuilder because it uses jaxb-api as dependency. Jaxb-api uses the same javax.xml package as XMLConstants from the JDK and therefore causes the error: The package javax.xml is accessible from more than one module: <unnamed>, java.xml
      See Also:
    • XML_EXT_PROPERTIES_ELEMENT

      public static final String XML_EXT_PROPERTIES_ELEMENT
      Name of the XML element the containing the elements for the extension property values.
      See Also:
    • XML_IPS_DEFAULT_NAMESPACE

      public static final String XML_IPS_DEFAULT_NAMESPACE
      See Also:
    • XML_ATTRIBUTE_SPACE

      public static final String XML_ATTRIBUTE_SPACE
      See Also:
    • XML_ATTRIBUTE_SPACE_VALUE

      public static final String XML_ATTRIBUTE_SPACE_VALUE
      See Also:
  • Method Details

    • getFirstElement

      public static final Element getFirstElement(Node parent, String tagName)
      See Also:
    • findFirstElement

      public static final Optional<Element> findFirstElement(Node parent, String tagName)
    • getFirstElement

      public static final Element getFirstElement(Node parent)
      Returns the first Element node
      See Also:
    • findFirstElement

      public static final Optional<Element> findFirstElement(Node parent)
      Returns the first Element node
    • getElement

      public static final Element getElement(Node parent, String tagName, int index)
      Returns the child element with the given tag name and index. The index is the position of the element considering all child elements with the given tag name.
      Parameters:
      parent - The parent node.
      tagName - the element tag name.
      index - The 0 based position of the child.
      Returns:
      The element at the specified index
      Throws:
      IndexOutOfBoundsException - if no element exists at the specified index.
    • getElements

      public static final List<Element> getElements(Node parent, String tagName)
      Returns all child elements with the given tag name. Considers only direct children. Use Element.getElementsByTagName(String) to search all descendants.
      Parameters:
      parent - The parent node.
      tagName - the element tag name.
      Returns:
      all child elements with the matching tag name
    • getChildElements

      public static List<Element> getChildElements(Node parent, String... tagNames)
      Returns all child elements with any of the given tag name(s). Considers only direct children. Use Element.getElementsByTagName(String) to search all descendants.
      Parameters:
      parent - the parent node
      tagNames - the desired element tag names
      Returns:
      all child elements with the matching tag name
    • getTextNode

      public static final Text getTextNode(Node node)
      Returns the node's text child node or null if the node hasn't got a text node.
    • getFirstCDataSection

      public static final CDATASection getFirstCDataSection(Node node)
      Returns the node's first CDATA section or null if the node hasn't got one.
    • getCDATAorTextContent

      public static final String getCDATAorTextContent(Node node)
      Returns the node's first CDATA section if the node has one. If not, this returns the node's text child node or null if the node hasn't got a text node.
    • getValueFromNode

      public static final String getValueFromNode(Element elem, String nodeName)
      Returns the value of the first element with the given node name, starts searching by the given Element
      Parameters:
      elem - The first element (root or parent) element the search begins
      nodeName - The name searching for
    • getElementsFromNode

      public static final List<Element> getElementsFromNode(Element elem, String nodeName, String attributeName, String attributeValue)
      Returns a list of element's with the following criteria:
      • the node name must be equal to the given node name
      • the node must contain an attribute with the attribute name
      • the value of the attribute (with the given name) must be equal to the given value
    • writeXMLtoFile

      public static void writeXMLtoFile(File file, Document doc, String doctype, int indentWidth, String encoding) throws TransformerException
      Writes an XML document to a file.

      See also the DOMUtil.java example.

      Throws:
      TransformerException
    • writeXMLtoStream

      public static void writeXMLtoStream(OutputStream os, Document doc, String doctype, int indentWidth, String encoding) throws TransformerException
      Writes an XML document to a file.

      See also the DOMUtil.java example.

      Throws:
      TransformerException
    • writeXMLtoResult

      public static void writeXMLtoResult(Result res, Document doc, String doctype, int indentWidth, String encoding) throws TransformerException
      Writes an XML document to a DOM result object.

      See also the DOMUtil.java example.

      Throws:
      TransformerException
    • getDocumentBuilder

      public static final DocumentBuilder getDocumentBuilder()
      Returns:
      a DocumentBuilder in a thread safe manner (ThreadLocal).
    • parseDocument

      public static final Document parseDocument(InputStream is) throws SAXException, IOException
      Throws:
      SAXException
      IOException
    • nodeToString

      public static final String nodeToString(Node node, String encoding, String lineSeparator) throws TransformerException
      Transforms the given node to a String.
      Throws:
      TransformerException
    • nodeToString

      public static final String nodeToString(Node node, String encoding, String lineSeparator, boolean escapeBlanks) throws TransformerException
      Transforms the given node to a String.
      Parameters:
      node - the node
      encoding - the used encoding
      escapeBlanks - if non standard blanks should be escaped
      Returns:
      the node as String
      Throws:
      TransformerException - for errors that occurred during the transformation process
    • nodeToWriter

      public static final void nodeToWriter(Node node, Writer writer, String encoding) throws TransformerException
      Transforms the given node to a string and writes in to the given writer.

      The encoding that is used to transforms the string into bytes is defined by the writer. E.g. a OutputStreamWriter is created with a char set / encoding. With a StringWriter no encoding is necessary.

      However, to get the encoding option set in the XML header e.g. <?xml version="1.0" encoding="Cp1252"?>, it is necessary to pass the encoding to this method. Note that this method does not check, if the writer's encoding and the given encoding are the same (as the encoding is not available from the writer).

      Throws:
      TransformerException