From 3fada6cef21c9b16467177d866df778203b51b4d Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 5 Dec 2019 09:52:48 +0100 Subject: some code code-style modifications active code-quality checks! --- .../at/gv/egiz/eaaf/core/impl/utils/DomUtils.java | 523 ++++++++++++--------- 1 file changed, 291 insertions(+), 232 deletions(-) (limited to 'eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/utils/DomUtils.java') diff --git a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/utils/DomUtils.java b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/utils/DomUtils.java index aafea776..e1a02c64 100644 --- a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/utils/DomUtils.java +++ b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/utils/DomUtils.java @@ -17,8 +17,6 @@ * works that you distribute must include a readable copy of the "NOTICE" text file. */ - - package at.gv.egiz.eaaf.core.impl.utils; import java.io.ByteArrayInputStream; @@ -34,6 +32,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.Vector; + import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -45,7 +44,7 @@ import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; -import at.gv.egiz.eaaf.core.api.data.XMLNamespaceConstants; + import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.xerces.parsers.DOMParser; @@ -70,14 +69,17 @@ import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; +import at.gv.egiz.eaaf.core.api.data.XmlNamespaceConstants; + /** * 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. - *

+ *

+ * 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. + *

* */ public class DomUtils { @@ -103,7 +105,8 @@ public class DomUtils { private static final String EXTERNAL_SCHEMA_LOCATION_PROPERTY = "http://apache.org/xml/properties/schema/external-schemaLocation"; /** - * Property URI for providing the external schema location for elements without a namespace. + * Property URI for providing the external schema location for elements without + * a namespace. */ private static final String EXTERNAL_NO_NAMESPACE_SCHEMA_LOCATION_PROPERTY = "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation"; @@ -117,8 +120,6 @@ public class DomUtils { public static final String DISALLOW_DOCTYPE_FEATURE = "http://apache.org/xml/features/disallow-doctype-decl"; - - /** Property URI for the Xerces grammar pool. */ private static final String GRAMMAR_POOL = org.apache.xerces.impl.Constants.XERCES_PROPERTY_PREFIX + org.apache.xerces.impl.Constants.XMLGRAMMAR_POOL_PROPERTY; @@ -129,8 +130,8 @@ public class DomUtils { /** Xerces schema grammar pool. */ private static XMLGrammarPool grammarPool = new XMLGrammarPoolImpl(); /** - * Set holding the NamespaceURIs of the grammarPool, to prevent multiple entries of same grammars - * to the pool. + * Set holding the NamespaceURIs of the grammarPool, to prevent multiple entries + * of same grammars to the pool. */ private static Set grammarNamespaces; @@ -140,12 +141,13 @@ public class DomUtils { } /** - * Preparse a schema and add it to the schema pool. The method only adds the schema to the pool if - * a schema having the same systemId (namespace URI) is not already present in the - * pool. + * Preparse a schema and add it to the schema pool. The method only adds the + * schema to the pool if a schema having the same systemId + * (namespace URI) is not already present in the pool. * - * @param inputStream An InputStream providing the contents of the schema. - * @param systemId The systemId (namespace URI) to use for the schema. + * @param inputStream An InputStream providing the contents of the + * schema. + * @param systemId The systemId (namespace URI) to use for the schema. * @throws IOException An error occurred reading the schema. */ public static void addSchemaToPool(final InputStream inputStream, final String systemId) @@ -178,24 +180,32 @@ public class DomUtils { /** * Parse an XML document from an InputStream. * - *

- * It uses a MOAEntityResolver as the EntityResolver and a - * MOAErrorHandler as the ErrorHandler. - *

+ *

+ * It uses a MOAEntityResolver as the EntityResolver + * and a MOAErrorHandler as the ErrorHandler. + *

* - * @param inputStream The InputStream containing the XML document. - * @param validating If true, parse validating. - * @param externalSchemaLocations A String containing namespace URI to schema - * location pairs, the same way it is accepted by the xsi: - * schemaLocation attribute. - * @param externalNoNamespaceSchemaLocation The schema location of the schema for elements without - * a namespace, the same way it is accepted by the - * xsi:noNamespaceSchemaLocation attribute. - * @param parserFeatures Map of features that should be set into XML parser + * @param inputStream The InputStream + * containing the XML document. + * @param validating If true, parse + * validating. + * @param externalSchemaLocations A String containing + * namespace URI to schema location + * pairs, the same way it is accepted + * by the xsi: + * schemaLocation attribute. + * @param externalNoNamespaceSchemaLocation The schema location of the schema + * for elements without a namespace, + * the same way it is accepted by the + * xsi:noNamespaceSchemaLocation + * attribute. + * @param parserFeatures Map of features that should be set + * into XML parser * @return The parsed XML document as a DOM tree. - * @throws SAXException An error occurred parsing the document. - * @throws IOException An error occurred reading the document. - * @throws ParserConfigurationException An error occurred configuring the XML parser. + * @throws SAXException An error occurred parsing the document. + * @throws IOException An error occurred reading the document. + * @throws ParserConfigurationException An error occurred configuring the XML + * parser. */ public static Document parseDocument(final InputStream inputStream, final boolean validating, final String externalSchemaLocations, final String externalNoNamespaceSchemaLocation, @@ -209,24 +219,31 @@ public class DomUtils { /** * Parse an XML document from a String. * - *

- * It uses a MOAEntityResolver as the EntityResolver and a - * MOAErrorHandler as the ErrorHandler. - *

+ *

+ * It uses a MOAEntityResolver as the EntityResolver + * and a MOAErrorHandler as the ErrorHandler. + *

* - * @param xmlString The String containing the XML document. - * @param encoding The encoding of the XML document. - * @param validating If true, parse validating. - * @param externalSchemaLocations A String containing namespace URI to schema - * location pairs, the same way it is accepted by the xsi: - * schemaLocation attribute. - * @param externalNoNamespaceSchemaLocation The schema location of the schema for elements without - * a namespace, the same way it is accepted by the - * xsi:noNamespaceSchemaLocation attribute. + * @param xmlString The String containing + * the XML document. + * @param encoding The encoding of the XML document. + * @param validating If true, parse + * validating. + * @param externalSchemaLocations A String containing + * namespace URI to schema location + * pairs, the same way it is accepted + * by the xsi: + * schemaLocation attribute. + * @param externalNoNamespaceSchemaLocation The schema location of the schema + * for elements without a namespace, + * the same way it is accepted by the + * xsi:noNamespaceSchemaLocation + * attribute. * @return The parsed XML document as a DOM tree. - * @throws SAXException An error occurred parsing the document. - * @throws IOException An error occurred reading the document. - * @throws ParserConfigurationException An error occurred configuring the XML parser. + * @throws SAXException An error occurred parsing the document. + * @throws IOException An error occurred reading the document. + * @throws ParserConfigurationException An error occurred configuring the XML + * parser. */ public static Document parseDocument(final String xmlString, final String encoding, final boolean validating, final String externalSchemaLocations, @@ -241,24 +258,31 @@ public class DomUtils { /** * Parse an XML document from a String. * - *

- * It uses a MOAEntityResolver as the EntityResolver and a - * MOAErrorHandler as the ErrorHandler. - *

+ *

+ * It uses a MOAEntityResolver as the EntityResolver + * and a MOAErrorHandler as the ErrorHandler. + *

* - * @param xmlString The String containing the XML document. - * @param encoding The encoding of the XML document. - * @param validating If true, parse validating. - * @param externalSchemaLocations A String containing namespace URI to schema - * location pairs, the same way it is accepted by the xsi: - * schemaLocation attribute. - * @param externalNoNamespaceSchemaLocation The schema location of the schema for elements without - * a namespace, the same way it is accepted by the - * xsi:noNamespaceSchemaLocation attribute. + * @param xmlString The String containing + * the XML document. + * @param encoding The encoding of the XML document. + * @param validating If true, parse + * validating. + * @param externalSchemaLocations A String containing + * namespace URI to schema location + * pairs, the same way it is accepted + * by the xsi: + * schemaLocation attribute. + * @param externalNoNamespaceSchemaLocation The schema location of the schema + * for elements without a namespace, + * the same way it is accepted by the + * xsi:noNamespaceSchemaLocation + * attribute. * @return The parsed XML document as a DOM tree. - * @throws SAXException An error occurred parsing the document. - * @throws IOException An error occurred reading the document. - * @throws ParserConfigurationException An error occurred configuring the XML parser. + * @throws SAXException An error occurred parsing the document. + * @throws IOException An error occurred reading the document. + * @throws ParserConfigurationException An error occurred configuring the XML + * parser. */ public static Document parseDocument(final String xmlString, final String encoding, final boolean validating, final String externalSchemaLocations, @@ -273,18 +297,25 @@ public class DomUtils { /** * Parse an UTF-8 encoded XML document from a String. * - * @param xmlString The String containing the XML document. - * @param validating If true, parse validating. - * @param externalSchemaLocations A String containing namespace URI to schema - * location pairs, the same way it is accepted by the xsi: - * schemaLocation attribute. - * @param externalNoNamespaceSchemaLocation The schema location of the schema for elements without - * a namespace, the same way it is accepted by the - * xsi:noNamespaceSchemaLocation attribute. + * @param xmlString The String containing + * the XML document. + * @param validating If true, parse + * validating. + * @param externalSchemaLocations A String containing + * namespace URI to schema location + * pairs, the same way it is accepted + * by the xsi: + * schemaLocation attribute. + * @param externalNoNamespaceSchemaLocation The schema location of the schema + * for elements without a namespace, + * the same way it is accepted by the + * xsi:noNamespaceSchemaLocation + * attribute. * @return The parsed XML document as a DOM tree. - * @throws SAXException An error occurred parsing the document. - * @throws IOException An error occurred reading the document. - * @throws ParserConfigurationException An error occurred configuring the XML parser. + * @throws SAXException An error occurred parsing the document. + * @throws IOException An error occurred reading the document. + * @throws ParserConfigurationException An error occurred configuring the XML + * parser. */ public static Document parseDocument(final String xmlString, final boolean validating, final String externalSchemaLocations, final String externalNoNamespaceSchemaLocation) @@ -297,22 +328,33 @@ public class DomUtils { /** * Parse an XML document from an InputStream. * - * @param inputStream The InputStream containing the XML document. - * @param validating If true, parse validating. - * @param externalSchemaLocations A String containing namespace URI to schema - * location pairs, the same way it is accepted by the xsi: - * schemaLocation attribute. - * @param externalNoNamespaceSchemaLocation The schema location of the schema for elements without - * a namespace, the same way it is accepted by the - * xsi:noNamespaceSchemaLocation attribute. - * @param entityResolver An EntityResolver to resolve external entities (schemas and - * DTDs). If null, it will not be set. - * @param errorHandler An ErrorHandler to decide what to do with parsing errors. If - * null, it will not be set. + * @param inputStream The InputStream + * containing the XML document. + * @param validating If true, parse + * validating. + * @param externalSchemaLocations A String containing + * namespace URI to schema location + * pairs, the same way it is accepted + * by the xsi: + * schemaLocation attribute. + * @param externalNoNamespaceSchemaLocation The schema location of the schema + * for elements without a namespace, + * the same way it is accepted by the + * xsi:noNamespaceSchemaLocation + * attribute. + * @param entityResolver An EntityResolver to + * resolve external entities (schemas + * and DTDs). If null, it + * will not be set. + * @param errorHandler An ErrorHandler to + * decide what to do with parsing + * errors. If null, it + * will not be set. * @return The parsed XML document as a DOM tree. - * @throws SAXException An error occurred parsing the document. - * @throws IOException An error occurred reading the document. - * @throws ParserConfigurationException An error occurred configuring the XML parser. + * @throws SAXException An error occurred parsing the document. + * @throws IOException An error occurred reading the document. + * @throws ParserConfigurationException An error occurred configuring the XML + * parser. */ public static Document parseDocument(final InputStream inputStream, final boolean validating, final String externalSchemaLocations, final String externalNoNamespaceSchemaLocation, @@ -330,8 +372,8 @@ public class DomUtils { // } // } - - // if Debug is enabled make a copy of inputStream to enable debug output in case of SAXException + // if Debug is enabled make a copy of inputStream to enable debug output in case + // of SAXException byte[] buffer = null; ByteArrayInputStream baStream = null; if (true == log.isDebugEnabled()) { @@ -340,8 +382,6 @@ public class DomUtils { } - - // create the DOM parser if (symbolTable != null) { parser = new DOMParser(symbolTable, grammarPool); @@ -379,8 +419,8 @@ public class DomUtils { } // fix XXE problem - // parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); - + // parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", + // true); if (validating) { if (externalSchemaLocations != null) { @@ -423,9 +463,9 @@ public class DomUtils { * * @param inputStream data to parse * @return Element - * @throws SAXException In case of an error - * @throws IOException In case of an error - * @throws ParserConfigurationException In case of an error + * @throws SAXException In case of an error + * @throws IOException In case of an error + * @throws ParserConfigurationException In case of an error */ public static Document parseDocumentSimple(final InputStream inputStream) throws SAXException, IOException, ParserConfigurationException { @@ -446,62 +486,56 @@ public class DomUtils { return parser.getDocument(); } - - - - - - - - - - /** * A convenience method to parse an XML document validating. * * @param inputStream The InputStream containing the XML document. * @return The root element of the parsed XML document. - * @throws SAXException An error occurred parsing the document. - * @throws IOException An error occurred reading the document. - * @throws ParserConfigurationException An error occurred configuring the XML parser. + * @throws SAXException An error occurred parsing the document. + * @throws IOException An error occurred reading the document. + * @throws ParserConfigurationException An error occurred configuring the XML + * parser. */ public static Element parseXmlValidating(final InputStream inputStream) throws ParserConfigurationException, SAXException, IOException { return DomUtils - .parseDocument(inputStream, true, XMLNamespaceConstants.ALL_SCHEMA_LOCATIONS, null, null) + .parseDocument(inputStream, true, XmlNamespaceConstants.ALL_SCHEMA_LOCATIONS, null, null) .getDocumentElement(); } /** * A convenience method to parse an XML document validating. * - * @param inputStream The InputStream containing the XML document. + * @param inputStream The InputStream containing the XML + * document. * @param parserFeatures Set additional features to XML parser * @return The root element of the parsed XML document. - * @throws SAXException An error occurred parsing the document. - * @throws IOException An error occurred reading the document. - * @throws ParserConfigurationException An error occurred configuring the XML parser. + * @throws SAXException An error occurred parsing the document. + * @throws IOException An error occurred reading the document. + * @throws ParserConfigurationException An error occurred configuring the XML + * parser. */ public static Element parseXmlValidating(final InputStream inputStream, final Map parserFeatures) throws ParserConfigurationException, SAXException, IOException { - return DomUtils.parseDocument(inputStream, true, XMLNamespaceConstants.ALL_SCHEMA_LOCATIONS, + return DomUtils.parseDocument(inputStream, true, XmlNamespaceConstants.ALL_SCHEMA_LOCATIONS, null, parserFeatures).getDocumentElement(); } /** - * A convenience method to parse an XML document non validating. This method disallow DocType - * declarations + * A convenience method to parse an XML document non validating. This method + * disallow DocType declarations * * @param inputStream The InputStream containing the XML document. * @return The root element of the parsed XML document. - * @throws SAXException An error occurred parsing the document. - * @throws IOException An error occurred reading the document. - * @throws ParserConfigurationException An error occurred configuring the XML parser. + * @throws SAXException An error occurred parsing the document. + * @throws IOException An error occurred reading the document. + * @throws ParserConfigurationException An error occurred configuring the XML + * parser. */ public static Element parseXmlNonValidating(final InputStream inputStream) throws ParserConfigurationException, SAXException, IOException { - return DomUtils.parseDocument(inputStream, false, XMLNamespaceConstants.ALL_SCHEMA_LOCATIONS, + return DomUtils.parseDocument(inputStream, false, XmlNamespaceConstants.ALL_SCHEMA_LOCATIONS, null, Collections.unmodifiableMap(new HashMap() { private static final long serialVersionUID = 1L; { @@ -514,19 +548,25 @@ public class DomUtils { /** * Schema validate a given DOM element. * - * @param element The element to validate. - * @param externalSchemaLocations A String containing namespace URI to schema - * location pairs, the same way it is accepted by the xsi: - * schemaLocation attribute. - * @param externalNoNamespaceSchemaLocation The schema location of the schema for elements without - * a namespace, the same way it is accepted by the - * xsi:noNamespaceSchemaLocation attribute. - * @return true, if the element validates against the schemas declared - * in it. - * @throws SAXException An error occurred parsing the document. - * @throws IOException An error occurred reading the document from its serialized representation. + * @param element The element to validate. + * @param externalSchemaLocations A String containing + * namespace URI to schema location + * pairs, the same way it is accepted + * by the xsi: + * schemaLocation attribute. + * @param externalNoNamespaceSchemaLocation The schema location of the schema + * for elements without a namespace, + * the same way it is accepted by the + * xsi:noNamespaceSchemaLocation + * attribute. + * @return true, if the element validates against the + * schemas declared in it. + * @throws SAXException An error occurred parsing the document. + * @throws IOException An error occurred reading the document + * from its serialized representation. * @throws ParserConfigurationException An error occurred configuring the XML - * @throws TransformerException An error occurred serializing the element. + * @throws TransformerException An error occurred serializing the + * element. */ public static boolean validateElement(final Element element, final String externalSchemaLocations, final String externalNoNamespaceSchemaLocation) @@ -552,7 +592,6 @@ public class DomUtils { parser.setFeature(EXTERNAL_GENERAL_ENTITIES_FEATURE, false); parser.setFeature(DISALLOW_DOCTYPE_FEATURE, true); - if (externalSchemaLocations != null) { parser.setProperty(EXTERNAL_SCHEMA_LOCATION_PROPERTY, externalSchemaLocations); } @@ -569,23 +608,28 @@ public class DomUtils { return true; } - /** * Schema validate a given DOM element. * - * @param element The element to validate. - * @param externalSchemaLocations A String containing namespace URI to schema - * location pairs, the same way it is accepted by the xsi: - * schemaLocation attribute. - * @param externalNoNamespaceSchemaLocation The schema location of the schema for elements without - * a namespace, the same way it is accepted by the - * xsi:noNamespaceSchemaLocation attribute. - * @return true, if the element validates against the schemas declared - * in it. - * @throws SAXException An error occurred parsing the document. - * @throws IOException An error occurred reading the document from its serialized representation. + * @param element The element to validate. + * @param externalSchemaLocations A String containing + * namespace URI to schema location + * pairs, the same way it is accepted + * by the xsi: + * schemaLocation attribute. + * @param externalNoNamespaceSchemaLocation The schema location of the schema + * for elements without a namespace, + * the same way it is accepted by the + * xsi:noNamespaceSchemaLocation + * attribute. + * @return true, if the element validates against the + * schemas declared in it. + * @throws SAXException An error occurred parsing the document. + * @throws IOException An error occurred reading the document + * from its serialized representation. * @throws ParserConfigurationException An error occurred configuring the XML - * @throws TransformerException An error occurred serializing the element. + * @throws TransformerException An error occurred serializing the + * element. */ public static boolean validateElement(final Element element, final String externalSchemaLocations, final String externalNoNamespaceSchemaLocation, final EntityResolver entityResolver) @@ -628,32 +672,35 @@ public class DomUtils { /** * Serialize the given DOM node. * - *

+ *

* The node will be serialized using the UTF-8 encoding. - *

+ *

* * @param node The node to serialize. * @return String The String representation of the given DOM node. - * @throws TransformerException An error occurred transforming the node to a String. - * @throws IOException An IO error occurred writing the node to a byte array. + * @throws TransformerException An error occurred transforming the node to a + * String. + * @throws IOException An IO error occurred writing the node to a byte + * array. */ public static String serializeNode(final Node node) throws TransformerException, IOException { return new String(serializeNode(node, "UTF-8", false), "UTF-8"); } - /** * Serialize the given DOM node. * - *

+ *

* The node will be serialized using the UTF-8 encoding. - *

+ *

* - * @param node The node to serialize. + * @param node The node to serialize. * @param omitXmlDeclaration The boolean value for omitting the XML Declaration. * @return String The String representation of the given DOM node. - * @throws TransformerException An error occurred transforming the node to a String. - * @throws IOException An IO error occurred writing the node to a byte array. + * @throws TransformerException An error occurred transforming the node to a + * String. + * @throws IOException An IO error occurred writing the node to a byte + * array. */ public static String serializeNode(final Node node, final boolean omitXmlDeclaration) throws TransformerException, IOException { @@ -663,16 +710,18 @@ public class DomUtils { /** * Serialize the given DOM node. * - *

+ *

* The node will be serialized using the UTF-8 encoding. - *

+ *

* - * @param node The node to serialize. + * @param node The node to serialize. * @param omitXmlDeclaration The boolean value for omitting the XML Declaration. - * @param lineSeperator Sets the line seperator String of the parser + * @param lineSeperator Sets the line seperator String of the parser * @return String The String representation of the given DOM node. - * @throws TransformerException An error occurred transforming the node to a String. - * @throws IOException An IO error occurred writing the node to a byte array. + * @throws TransformerException An error occurred transforming the node to a + * String. + * @throws IOException An IO error occurred writing the node to a byte + * array. */ public static String serializeNode(final Node node, final boolean omitXmlDeclaration, final String lineSeperator) throws TransformerException, IOException { @@ -682,12 +731,14 @@ public class DomUtils { /** * Serialize the given DOM node to a byte array. * - * @param node The node to serialize. + * @param node The node to serialize. * @param xmlEncoding The XML encoding to use. - * @return 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. - * @throws IOException An IO error occurred writing the node to a byte array. + * @return 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. + * @throws IOException An IO error occurred writing the node to a byte + * array. */ public static byte[] serializeNode(final Node node, final String xmlEncoding) throws TransformerException, IOException { @@ -697,31 +748,34 @@ public class DomUtils { /** * Serialize the given DOM node to a byte array. * - * @param node The node to serialize. - * @param xmlEncoding The XML encoding to use. + * @param node The node to serialize. + * @param xmlEncoding The XML encoding to use. * @param omitDeclaration The boolean value for omitting the XML Declaration. - * @return 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. - * @throws IOException An IO error occurred writing the node to a byte array. + * @return 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. + * @throws IOException An IO error occurred writing the node to a byte + * array. */ public static byte[] serializeNode(final Node node, final String xmlEncoding, final boolean omitDeclaration) throws TransformerException, IOException { return serializeNode(node, xmlEncoding, omitDeclaration, null); } - /** * Serialize the given DOM node to a byte array. * - * @param node The node to serialize. - * @param xmlEncoding The XML encoding to use. + * @param node The node to serialize. + * @param xmlEncoding The XML encoding to use. * @param omitDeclaration The boolean value for omitting the XML Declaration. - * @param lineSeperator Sets the line seperator String of the parser - * @return 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. - * @throws IOException An IO error occurred writing the node to a byte array. + * @param lineSeperator Sets the line seperator String of the parser + * @return 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. + * @throws IOException An IO error occurred writing the node to a byte + * array. */ public static byte[] serializeNode(final Node node, final String xmlEncoding, final boolean omitDeclaration, final String lineSeperator) @@ -737,7 +791,7 @@ public class DomUtils { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omit); if (null != lineSeperator) { transformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", lineSeperator); - // does not work for xalan <= 2.5.1 + // does not work for xalan <= 2.5.1 } transformer.transform(new DOMSource(node), new StreamResult(bos)); @@ -750,15 +804,15 @@ public class DomUtils { /** * 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.)
  • + *
  • 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.)
  • *
* * @param node A DOM node from which to extract text. @@ -788,11 +842,12 @@ public class DomUtils { } /** - * Build the namespace prefix to namespace URL mapping in effect for a given node. + * Build the namespace prefix to namespace URL mapping in effect for a given + * node. * * @param node The context node for which build the map. - * @return The namespace prefix to namespace URL mapping ( a String value to - * String value mapping). + * @return The namespace prefix to namespace URL mapping ( a String + * value to String value mapping). */ public static Map getNamespaceDeclarations(Node node) { final Map nsDecls = new HashMap(); @@ -822,8 +877,8 @@ public class DomUtils { } /** - * 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. + * 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. * * @param context The element to which to add the namespaces. */ @@ -843,7 +898,7 @@ public class DomUtils { final String nsUri = (String) e.getValue(); final String nsAttrName = "".equals(prefix) ? "xmlns" : "xmlns:" + prefix; - context.setAttributeNS(XMLNamespaceConstants.XMLNS_NS_URI, nsAttrName, nsUri); + context.setAttributeNS(XmlNamespaceConstants.XMLNS_NS_URI, nsAttrName, nsUri); } } } @@ -853,8 +908,8 @@ public class DomUtils { * Collect all the namespace URIs used in the subtree of a given element. * * @param context The element that should be searched for namespace URIs. - * @return All namespace URIs used in the subtree of context, including the ones used - * in context itself. + * @return All namespace URIs used in the subtree of context, + * including the ones used in context itself. */ public static Set collectNamespaceUris(final Element context) { final Set result = new HashSet(); @@ -867,8 +922,8 @@ public class DomUtils { * A recursive method to do the work of collectNamespaceURIs. * * @param context The context element to evaluate. - * @param result The result, passed as a parameter to avoid unnecessary instantiations of - * Set. + * @param result The result, passed as a parameter to avoid unnecessary + * instantiations of Set. */ private static void collectNamespaceUrisImpl(final Element context, final Set result) { final NamedNodeMap attrs = context.getAttributes(); @@ -878,14 +933,14 @@ public class DomUtils { // add the namespace of the context element nsUri = context.getNamespaceURI(); - if (nsUri != null && nsUri != XMLNamespaceConstants.XMLNS_NS_URI) { + if (nsUri != null && nsUri != XmlNamespaceConstants.XMLNS_NS_URI) { result.add(nsUri); } // add all namespace URIs from attributes for (i = 0; i < attrs.getLength(); i++) { nsUri = attrs.item(i).getNamespaceURI(); - if (nsUri != null && nsUri != XMLNamespaceConstants.XMLNS_NS_URI) { + if (nsUri != null && nsUri != XmlNamespaceConstants.XMLNS_NS_URI) { result.add(nsUri); } } @@ -901,12 +956,12 @@ public class DomUtils { } /** - * Check, that each attribute node in the given NodeList has its parent in the - * NodeList as well. + * Check, that each attribute node in the given NodeList has its + * parent in the NodeList as well. * * @param nodes The NodeList to check. - * @return true, if each attribute node in nodes has its parent in - * nodes as well. + * @return true, if each attribute node in nodes has + * its parent in nodes as well. */ public static boolean checkAttributeParentsInNodeList(final NodeList nodes) { final Set nodeSet = new HashSet(); @@ -939,15 +994,19 @@ public class DomUtils { } /** - * Convert an unstructured NodeList into a DocumentFragment. + * Convert an unstructured NodeList into a + * DocumentFragment. * - * @param nodeList Contains the node list to be converted into a DOM DocumentFragment. - * @return 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. + * @param nodeList Contains the node list to be converted into a DOM + * DocumentFragment. + * @return 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. * @precondition The nodes in the node list appear in document order - * @precondition for each Attr node in the node list, the owning Element is in the node list as - * well. + * @precondition for each Attr node in the node list, the owning Element is in + * the node list as well. * @precondition each Element or Attr node in the node list is namespace aware. */ public static DocumentFragment nodeList2DocumentFragment(final NodeList nodeList) @@ -973,10 +1032,10 @@ public class DomUtils { /** * Helper method for the nodeList2DocumentFragment. * - * @param nodeList The NodeList to convert. - * @param currPos The current position in the nodeList. - * @param result The resulting DocumentFragment. - * @param currOrgElem The current original element. + * @param nodeList The NodeList to convert. + * @param currPos The current position in the nodeList. + * @param result The resulting DocumentFragment. + * @param currOrgElem The current original element. * @param currClonedElem The current cloned element. * @return The current position. */ @@ -1052,7 +1111,7 @@ public class DomUtils { } default: { - log.trace("Node type: {} not supported", currentNode.getNodeType()); + log.trace("Node type: {} not supported", currentNode.getNodeType()); // All other nodes will be ignored } } @@ -1067,20 +1126,20 @@ public class DomUtils { * Check, if the given attribute is a namespace declaration. * * @param attr The attribute to check. - * @return true, if the attribute is a namespace declaration, false - * otherwise. + * @return true, if the attribute is a namespace declaration, + * false otherwise. */ private static boolean isNamespaceDeclaration(final Attr attr) { - return XMLNamespaceConstants.XMLNS_NS_URI.equals(attr.getNamespaceURI()); + return XmlNamespaceConstants.XMLNS_NS_URI.equals(attr.getNamespaceURI()); } /** * Check, if a given DOM element is an ancestor of a given node. * * @param candAnc The DOM element to check for being the ancestor. - * @param cand The node to check for being the child. - * @return true, if candAnc is an (indirect) ancestor of - * cand; false otherwise. + * @param cand The node to check for being the child. + * @return true, if candAnc is an (indirect) ancestor + * of cand; false otherwise. */ public static boolean isAncestor(final Element candAnc, final Node cand) { Node currPar = cand.getParentNode(); @@ -1098,8 +1157,9 @@ public class DomUtils { * Selects the (first) element from a node list and returns it. * * @param nl The NodeList to get the element from. - * @return The (first) element included in the node list or null if the node list is - * null or empty or no element is included in the list. + * @return The (first) element included in the node list or null if + * the node list is null or empty or no element is included + * in the list. */ public static Element getElementFromNodeList(final NodeList nl) { if (nl == null || nl.getLength() == 0) { @@ -1119,8 +1179,8 @@ public class DomUtils { * * @param parent The element to get the child elements from. * - * @return A list including all child elements of the given element. Maybe empty if the parent - * element has no child elements. + * @return A list including all child elements of the given element. Maybe empty + * if the parent element has no child elements. */ public static List getChildElements(final Element parent) { final Vector v = new Vector(); @@ -1153,5 +1213,4 @@ public class DomUtils { return out.toByteArray(); } - } -- cgit v1.2.3