Package at.gv.egovernment.moaspss.util
Class DOMUtils
java.lang.Object
at.gv.egovernment.moaspss.util.DOMUtils
Various utility functions for handling XML DOM trees.
The parsing methods in this class make use of some features internal to the
Xerces DOM parser, mainly for performance reasons. As soon as JAXP (currently
at version 1.2) is better at schema handling, it should be used as the parser
interface.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddSchemaToPool(InputStream inputStream, String systemId) Preparse a schema and add it to the schema pool.static booleanCheck, that each attribute node in the givenNodeListhas its parent in theNodeListas well.static SetcollectNamespaceURIs(Element context) Collect all the namespace URIs used in the subtree of a given element.static ListgetChildElements(Element parent) Returns all child elements of the given element.static ElementSelects the (first) element from a node list and returns it.static MapgetNamespaceDeclarations(Node node) Build the namespace prefix to namespace URL mapping in effect for a given node.static StringReturn the text that a node contains.static booleanisAncestor(Element candAnc, Node cand) Check, if a given DOM element is an ancestor of a given node.static voidlocalizeNamespaceDeclarations(Element context) Add all namespace declarations declared in the parent(s) of a given element and used in the subtree of the given element to the given element.static DocumentFragmentnodeList2DocumentFragment(NodeList nodeList) Convert an unstructuredNodeListinto aDocumentFragment.static byte[]nodeToByteArray(Node node) Returns a byte array from given node.static DocumentparseDocument(InputStream inputStream, boolean validating, String externalSchemaLocations, String externalNoNamespaceSchemaLocation) Parse an XML document from anInputStream.static DocumentparseDocument(InputStream inputStream, boolean validating, String externalSchemaLocations, String externalNoNamespaceSchemaLocation, EntityResolver entityResolver, ErrorHandler errorHandler) Parse an XML document from anInputStream.static DocumentparseDocument(String xmlString, boolean validating, String externalSchemaLocations, String externalNoNamespaceSchemaLocation) Parse an UTF-8 encoded XML document from aString.static DocumentparseDocument(String xmlString, String encoding, boolean validating, String externalSchemaLocations, String externalNoNamespaceSchemaLocation) Parse an XML document from aString.static DocumentparseDocumentSimple(InputStream inputStream) Parse an XML document from anInputStream.static ElementparseXmlNonValidating(InputStream inputStream) A convenience method to parse an XML document non validating.static ElementparseXmlValidating(InputStream inputStream) A convenience method to parse an XML document validating.static StringserializeNode(Node node) Serialize the given DOM node.static StringserializeNode(Node node, boolean omitXmlDeclaration) Serialize the given DOM node.static StringserializeNode(Node node, boolean omitXmlDeclaration, String lineSeperator) Serialize the given DOM node.static byte[]serializeNode(Node node, String xmlEncoding) Serialize the given DOM node to a byte array.static byte[]serializeNode(Node node, String xmlEncoding, boolean omitDeclaration) Serialize the given DOM node to a byte array.static byte[]serializeNode(Node node, String xmlEncoding, boolean omitDeclaration, String lineSeperator) Serialize the given DOM node to a byte array.static booleanvalidateElement(Element element, String externalSchemaLocations, String externalNoNamespaceSchemaLocation) Schema validate a given DOM element.static booleanvalidateElement(Element element, String externalSchemaLocations, String externalNoNamespaceSchemaLocation, EntityResolver entityResolver) Schema validate a given DOM element.
-
Constructor Details
-
DOMUtils
public DOMUtils()
-
-
Method Details
-
addSchemaToPool
Preparse a schema and add it to the schema pool. The method only adds the schema to the pool if a schema having the samesystemId(namespace URI) is not already present in the pool.- Parameters:
inputStream- AnInputStreamproviding the contents of the schema.systemId- The systemId (namespace URI) to use for the schema.- Throws:
IOException- An error occurred reading the schema.
-
parseDocument
public static Document parseDocument(InputStream inputStream, boolean validating, String externalSchemaLocations, String externalNoNamespaceSchemaLocation, EntityResolver entityResolver, ErrorHandler errorHandler) throws SAXException, IOException, ParserConfigurationException Parse an XML document from anInputStream.- Parameters:
inputStream- TheInputStreamcontaining the XML document.validating- Iftrue, parse validating.externalSchemaLocations- AStringcontaining namespace URI to schema location pairs, the same way it is accepted by thexsi: schemaLocationattribute.externalNoNamespaceSchemaLocation- The schema location of the schema for elements without a namespace, the same way it is accepted by thexsi:noNamespaceSchemaLocationattribute.entityResolver- AnEntityResolverto resolve external entities (schemas and DTDs). Ifnull, it will not be set.errorHandler- AnErrorHandlerto decide what to do with parsing errors. Ifnull, it will not be set.- Returns:
- The parsed XML document as a DOM tree.
- Throws:
SAXException- An error occurred parsing the document.IOException- An error occurred reading the document.ParserConfigurationException- An error occurred configuring the XML parser.
-
parseDocumentSimple
public static Document parseDocumentSimple(InputStream inputStream) throws SAXException, IOException, ParserConfigurationException Parse an XML document from anInputStream.- Parameters:
inputStream- TheInputStreamcontaining the XML document.- Returns:
- The parsed XML document as a DOM tree.
- Throws:
SAXException- An error occurred parsing the document.IOException- An error occurred reading the document.ParserConfigurationException- An error occurred configuring the XML parser.
-
parseDocument
public static Document parseDocument(InputStream inputStream, boolean validating, String externalSchemaLocations, String externalNoNamespaceSchemaLocation) throws SAXException, IOException, ParserConfigurationException Parse an XML document from anInputStream. It uses aMOAEntityResolveras theEntityResolverand aMOAErrorHandleras theErrorHandler.- Parameters:
inputStream- TheInputStreamcontaining the XML document.validating- Iftrue, parse validating.externalSchemaLocations- AStringcontaining namespace URI to schema location pairs, the same way it is accepted by thexsi: schemaLocationattribute.externalNoNamespaceSchemaLocation- The schema location of the schema for elements without a namespace, the same way it is accepted by thexsi:noNamespaceSchemaLocationattribute.- Returns:
- The parsed XML document as a DOM tree.
- Throws:
SAXException- An error occurred parsing the document.IOException- An error occurred reading the document.ParserConfigurationException- An error occurred configuring the XML parser.
-
parseDocument
public static Document parseDocument(String xmlString, String encoding, boolean validating, String externalSchemaLocations, String externalNoNamespaceSchemaLocation) throws SAXException, IOException, ParserConfigurationException Parse an XML document from aString. It uses aMOAEntityResolveras theEntityResolverand aMOAErrorHandleras theErrorHandler.- Parameters:
xmlString- TheStringcontaining the XML document.encoding- The encoding of the XML document.validating- Iftrue, parse validating.externalSchemaLocations- AStringcontaining namespace URI to schema location pairs, the same way it is accepted by thexsi: schemaLocationattribute.externalNoNamespaceSchemaLocation- The schema location of the schema for elements without a namespace, the same way it is accepted by thexsi:noNamespaceSchemaLocationattribute.- Returns:
- The parsed XML document as a DOM tree.
- Throws:
SAXException- An error occurred parsing the document.IOException- An error occurred reading the document.ParserConfigurationException- An error occurred configuring the XML parser.
-
parseDocument
public static Document parseDocument(String xmlString, boolean validating, String externalSchemaLocations, String externalNoNamespaceSchemaLocation) throws SAXException, IOException, ParserConfigurationException Parse an UTF-8 encoded XML document from aString.- Parameters:
xmlString- TheStringcontaining the XML document.validating- Iftrue, parse validating.externalSchemaLocations- AStringcontaining namespace URI to schema location pairs, the same way it is accepted by thexsi: schemaLocationattribute.externalNoNamespaceSchemaLocation- The schema location of the schema for elements without a namespace, the same way it is accepted by thexsi:noNamespaceSchemaLocationattribute.- Returns:
- The parsed XML document as a DOM tree.
- Throws:
SAXException- An error occurred parsing the document.IOException- An error occurred reading the document.ParserConfigurationException- An error occurred configuring the XML parser.
-
parseXmlValidating
public static Element parseXmlValidating(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException A convenience method to parse an XML document validating.- Parameters:
inputStream- TheInputStreamcontaining the XML document.- Returns:
- The root element of the parsed XML document.
- Throws:
SAXException- An error occurred parsing the document.IOException- An error occurred reading the document.ParserConfigurationException- An error occurred configuring the XML parser.
-
parseXmlNonValidating
public static Element parseXmlNonValidating(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException A convenience method to parse an XML document non validating.- Parameters:
inputStream- TheInputStreamcontaining the XML document.- Returns:
- The root element of the parsed XML document.
- Throws:
SAXException- An error occurred parsing the document.IOException- An error occurred reading the document.ParserConfigurationException- An error occurred configuring the XML parser.
-
validateElement
public static boolean validateElement(Element element, String externalSchemaLocations, String externalNoNamespaceSchemaLocation) throws ParserConfigurationException, IOException, SAXException, TransformerException Schema validate a given DOM element.- Parameters:
element- The element to validate.externalSchemaLocations- AStringcontaining namespace URI to schema location pairs, the same way it is accepted by thexsi: schemaLocationattribute.externalNoNamespaceSchemaLocation- The schema location of the schema for elements without a namespace, the same way it is accepted by thexsi:noNamespaceSchemaLocationattribute.- Returns:
true, if theelementvalidates against the schemas declared in it.- Throws:
SAXException- An error occurred parsing the document.IOException- An error occurred reading the document from its serialized representation.ParserConfigurationException- An error occurred configuring the XMLTransformerException- An error occurred serializing the element.
-
validateElement
public static boolean validateElement(Element element, String externalSchemaLocations, String externalNoNamespaceSchemaLocation, EntityResolver entityResolver) throws ParserConfigurationException, IOException, SAXException, TransformerException Schema validate a given DOM element.- Parameters:
element- The element to validate.externalSchemaLocations- AStringcontaining namespace URI to schema location pairs, the same way it is accepted by thexsi: schemaLocationattribute.externalNoNamespaceSchemaLocation- The schema location of the schema for elements without a namespace, the same way it is accepted by thexsi:noNamespaceSchemaLocationattribute.- Returns:
true, if theelementvalidates against the schemas declared in it.- Throws:
SAXException- An error occurred parsing the document.IOException- An error occurred reading the document from its serialized representation.ParserConfigurationException- An error occurred configuring the XMLTransformerException- An error occurred serializing the element.
-
serializeNode
Serialize the given DOM node. The node will be serialized using the UTF-8 encoding.- Parameters:
node- The node to serialize.- Returns:
- String The
Stringrepresentation of the given DOM node. - Throws:
TransformerException- An error occurred transforming the node to aString.IOException- An IO error occurred writing the node to a byte array.
-
serializeNode
public static String serializeNode(Node node, boolean omitXmlDeclaration) throws TransformerException, IOException Serialize the given DOM node. The node will be serialized using the UTF-8 encoding.- Parameters:
node- The node to serialize.omitXmlDeclaration- The boolean value for omitting the XML Declaration.- Returns:
- String The
Stringrepresentation of the given DOM node. - Throws:
TransformerException- An error occurred transforming the node to aString.IOException- An IO error occurred writing the node to a byte array.
-
serializeNode
public static String serializeNode(Node node, boolean omitXmlDeclaration, String lineSeperator) throws TransformerException, IOException Serialize the given DOM node. The node will be serialized using the UTF-8 encoding.- Parameters:
node- The node to serialize.omitXmlDeclaration- The boolean value for omitting the XML Declaration.lineSeperator- Sets the line seperator String of the parser- Returns:
- String The
Stringrepresentation of the given DOM node. - Throws:
TransformerException- An error occurred transforming the node to aString.IOException- An IO error occurred writing the node to a byte array.
-
serializeNode
public static byte[] serializeNode(Node node, String xmlEncoding) throws TransformerException, IOException Serialize the given DOM node to a byte array.- Parameters:
node- The node to serialize.xmlEncoding- The XML encoding to use.- Returns:
- The serialized node, as a byte array. Using a compatible encoding
this can easily be converted into a
String. - Throws:
TransformerException- An error occurred transforming the node to a byte array.IOException- An IO error occurred writing the node to a byte array.
-
serializeNode
public static byte[] serializeNode(Node node, String xmlEncoding, boolean omitDeclaration) throws TransformerException, IOException Serialize the given DOM node to a byte array.- Parameters:
node- The node to serialize.xmlEncoding- The XML encoding to use.omitDeclaration- The boolean value for omitting the XML Declaration.- Returns:
- The serialized node, as a byte array. Using a compatible encoding
this can easily be converted into a
String. - Throws:
TransformerException- An error occurred transforming the node to a byte array.IOException- An IO error occurred writing the node to a byte array.
-
serializeNode
public static byte[] serializeNode(Node node, String xmlEncoding, boolean omitDeclaration, String lineSeperator) throws TransformerException, IOException Serialize the given DOM node to a byte array.- Parameters:
node- The node to serialize.xmlEncoding- The XML encoding to use.omitDeclaration- The boolean value for omitting the XML Declaration.lineSeperator- Sets the line seperator String of the parser- Returns:
- The serialized node, as a byte array. Using a compatible encoding
this can easily be converted into a
String. - Throws:
TransformerException- An error occurred transforming the node to a byte array.IOException- An IO error occurred writing the node to a byte array.
-
getText
Return the text that a node contains. This routine:- Ignores comments and processing instructions.
- Concatenates TEXT nodes, CDATA nodes, and the results recursively processing EntityRef nodes.
- Ignores any element nodes in the sublist. (Other possible options are to recurse into element sublists or throw an exception.)
- Parameters:
node- A DOM node from which to extract text.- Returns:
- A String representing its contents.
-
getNamespaceDeclarations
Build the namespace prefix to namespace URL mapping in effect for a given node.- Parameters:
node- The context node for which build the map.- Returns:
- The namespace prefix to namespace URL mapping ( a
Stringvalue toStringvalue mapping).
-
localizeNamespaceDeclarations
Add all namespace declarations declared in the parent(s) of a given element and used in the subtree of the given element to the given element.- Parameters:
context- The element to which to add the namespaces.
-
collectNamespaceURIs
Collect all the namespace URIs used in the subtree of a given element.- Parameters:
context- The element that should be searched for namespace URIs.- Returns:
- All namespace URIs used in the subtree of
context, including the ones used incontextitself.
-
checkAttributeParentsInNodeList
Check, that each attribute node in the givenNodeListhas its parent in theNodeListas well.- Parameters:
nodes- TheNodeListto check.- Returns:
true, if each attribute node innodeshas its parent innodesas well.
-
nodeList2DocumentFragment
public static DocumentFragment nodeList2DocumentFragment(NodeList nodeList) throws ParserConfigurationException Convert an unstructuredNodeListinto aDocumentFragment.- Parameters:
nodeList- Contains the node list to be converted into a DOM DocumentFragment.- Returns:
- the resulting DocumentFragment. The DocumentFragment will be backed by a new DOM Document, i.e. all noded of the node list will be cloned.
- Throws:
ParserConfigurationException- An error occurred creating the DocumentFragment.
-
isAncestor
Check, if a given DOM element is an ancestor of a given node.- Parameters:
candAnc- The DOM element to check for being the ancestor.cand- The node to check for being the child.- Returns:
true, ifcandAncis an (indirect) ancestor ofcand;falseotherwise.
-
getElementFromNodeList
Selects the (first) element from a node list and returns it.- Parameters:
nl- The NodeList to get the element from.- Returns:
- The (first) element included in the node list or
nullif the node list isnullor empty or no element is included in the list.
-
getChildElements
Returns all child elements of the given element.- Parameters:
parent- The element to get the child elements from.- Returns:
- A list including all child elements of the given element. Maybe empty if the parent element has no child elements.
-
nodeToByteArray
Returns a byte array from given node.- Parameters:
node-- Returns:
- Throws:
TransformerException
-