Package org.faktorips.runtime.internal
Class XmlUtil
java.lang.Object
org.faktorips.runtime.internal.XmlUtil
A collection of utility methods for XML DOM handling.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final StringUsed for the project org.faktorips.devtools.stdbuilder because it uses jaxb-api as dependency.static final Stringstatic final Stringstatic final StringName of the XML element the containing the elements for the extension property values.static final Stringstatic final StringUsed for the project org.faktorips.devtools.stdbuilder because it uses jaxb-api as dependency. -
Method Summary
Modifier and TypeMethodDescriptionfindFirstElement(Node parent) Returns the first Element nodefindFirstElement(Node parent, String tagName) static final StringgetCDATAorTextContent(Node node) Returns the node's first CDATA section if the node has one.getChildElements(Node parent, String... tagNames) Returns all child elements with any of the given tag name(s).static final DocumentBuilderstatic final ElementgetElement(Node parent, String tagName, int index) Returns the child element with the given tag name and index.getElements(Node parent, String tagName) Returns all child elements with the given tag name.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 valuestatic final CDATASectiongetFirstCDataSection(Node node) Returns the node's first CDATA section ornullif the node hasn't got one.static final ElementgetFirstElement(Node parent) Returns the first Element nodestatic final ElementgetFirstElement(Node parent, String tagName) static final TextgetTextNode(Node node) Returns the node's text child node ornullif the node hasn't got a text node.static final StringgetValueFromNode(Element elem, String nodeName) Returns the value of the first element with the given node name, starts searching by the given Elementstatic final StringnodeToString(Node node, String encoding, String lineSeparator) Transforms the given node to a String.static final StringnodeToString(Node node, String encoding, String lineSeparator, boolean escapeBlanks) Transforms the given node to a String.static final voidnodeToWriter(Node node, Writer writer, String encoding) Transforms the given node to a string and writes in to the given writer.static final Documentstatic voidwriteXMLtoFile(File file, Document doc, String doctype, int indentWidth, String encoding) Writes an XML document to a file.static voidwriteXMLtoResult(Result res, Document doc, String doctype, int indentWidth, String encoding) Writes an XML document to a DOM result object.static voidwriteXMLtoStream(OutputStream os, Document doc, String doctype, int indentWidth, String encoding) Writes an XML document to a file.
-
Field Details
-
FAKTOR_IPS_SCHEMA_URL
- See Also:
-
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 asXMLConstantsfrom 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
Used for the project org.faktorips.devtools.stdbuilder because it uses jaxb-api as dependency. Jaxb-api uses the same javax.xml package asXMLConstantsfrom 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
Name of the XML element the containing the elements for the extension property values.- See Also:
-
XML_IPS_DEFAULT_NAMESPACE
- See Also:
-
XML_ATTRIBUTE_SPACE
- See Also:
-
XML_ATTRIBUTE_SPACE_VALUE
- See Also:
-
-
Method Details
-
getFirstElement
-
findFirstElement
-
getFirstElement
Returns the first Element node -
findFirstElement
Returns the first Element node -
getElement
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
Returns all child elements with the given tag name. Considers only direct children. UseElement.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
Returns all child elements with any of the given tag name(s). Considers only direct children. UseElement.getElementsByTagName(String)to search all descendants.- Parameters:
parent- the parent nodetagNames- the desired element tag names- Returns:
- all child elements with the matching tag name
-
getTextNode
Returns the node's text child node ornullif the node hasn't got a text node. -
getFirstCDataSection
Returns the node's first CDATA section ornullif the node hasn't got one. -
getCDATAorTextContent
Returns the node's first CDATA section if the node has one. If not, this returns the node's text child node ornullif the node hasn't got a text node. -
getValueFromNode
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 beginsnodeName- 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
- Returns:
- a
DocumentBuilderin a thread safe manner (ThreadLocal).
-
parseDocument
- Throws:
SAXExceptionIOException
-
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 nodeencoding- the used encodingescapeBlanks- 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
OutputStreamWriteris created with a char set / encoding. With aStringWriterno 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
-