aboutsummaryrefslogtreecommitdiff
path: root/moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/DOMUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/DOMUtils.java')
-rw-r--r--moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/DOMUtils.java1146
1 files changed, 602 insertions, 544 deletions
diff --git a/moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/DOMUtils.java b/moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/DOMUtils.java
index c5550ad..2f96196 100644
--- a/moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/DOMUtils.java
+++ b/moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/DOMUtils.java
@@ -21,7 +21,6 @@
* that you distribute must include a readable copy of the "NOTICE" text file.
*/
-
package at.gv.egovernment.moaspss.util;
import java.io.ByteArrayInputStream;
@@ -51,6 +50,7 @@ import javax.xml.transform.stream.StreamResult;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.parsers.SAXParser;
import org.apache.xerces.parsers.XMLGrammarPreparser;
+import org.apache.xerces.util.SecurityManager;
import org.apache.xerces.util.SymbolTable;
import org.apache.xerces.util.XMLGrammarPoolImpl;
import org.apache.xerces.xni.grammars.XMLGrammarDescription;
@@ -72,12 +72,12 @@ import at.gv.egovernment.moaspss.logging.Logger;
/**
* 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.
- *
+ * 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.
+ *
* @author Patrick Peck
* @version $Id$
*/
@@ -85,54 +85,61 @@ public class DOMUtils {
/** Feature URI for namespace aware parsing. */
private static final String NAMESPACES_FEATURE =
- "http://xml.org/sax/features/namespaces";
+ "http://xml.org/sax/features/namespaces";
/** Feature URI for validating parsing. */
private static final String VALIDATION_FEATURE =
- "http://xml.org/sax/features/validation";
+ "http://xml.org/sax/features/validation";
/** Feature URI for schema validating parsing. */
private static final String SCHEMA_VALIDATION_FEATURE =
- "http://apache.org/xml/features/validation/schema";
+ "http://apache.org/xml/features/validation/schema";
/** Feature URI for normalization of element/attribute values. */
private static final String NORMALIZED_VALUE_FEATURE =
- "http://apache.org/xml/features/validation/schema/normalized-value";
+ "http://apache.org/xml/features/validation/schema/normalized-value";
/** Feature URI for parsing ignorable whitespace. */
private static final String INCLUDE_IGNORABLE_WHITESPACE_FEATURE =
- "http://apache.org/xml/features/dom/include-ignorable-whitespace";
+ "http://apache.org/xml/features/dom/include-ignorable-whitespace";
/** Feature URI for creating EntityReference nodes in the DOM tree. */
private static final String CREATE_ENTITY_REF_NODES_FEATURE =
- "http://apache.org/xml/features/dom/create-entity-ref-nodes";
+ "http://apache.org/xml/features/dom/create-entity-ref-nodes";
/** Property URI for providing external schema locations. */
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. */
+ "http://apache.org/xml/properties/schema/external-schemaLocation";
+ /**
+ * 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";
-
+ "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation";
+
private static final String EXTERNAL_GENERAL_ENTITIES_FEATURE =
- "http://xml.org/sax/features/external-general-entities";
-
+ "http://xml.org/sax/features/external-general-entities";
+
private static final String EXTERNAL_PARAMETER_ENTITIES_FEATURE =
- "http://xml.org/sax/features/external-parameter-entities";
-
+ "http://xml.org/sax/features/external-parameter-entities";
+
private static final String DISALLOW_DOCTYPE_FEATURE =
- "http://apache.org/xml/features/disallow-doctype-decl";
-
-
-
+ "http://apache.org/xml/features/disallow-doctype-decl";
+
+ // Security Manager feature for XERCES XML parser
+ private static final String SECURITY_MANAGER =
+ org.apache.xerces.impl.Constants.XERCES_PROPERTY_PREFIX
+ + org.apache.xerces.impl.Constants.SECURITY_MANAGER_PROPERTY;
+
/** 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;
+ org.apache.xerces.impl.Constants.XERCES_PROPERTY_PREFIX
+ + org.apache.xerces.impl.Constants.XMLGRAMMAR_POOL_PROPERTY;
/** A prime number for initializing the symbol table. */
private static final int BIG_PRIME = 2039;
/** Symbol table for the grammar pool. */
private static SymbolTable symbolTable = new SymbolTable(BIG_PRIME);
/** 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 */
- private static Set grammarNamespaces;
+ /**
+ * Set holding the NamespaceURIs of the grammarPool, to prevent multiple entries
+ * of same grammars to the pool
+ */
+ private static Set grammarNamespaces;
static {
grammarPool.lockPool();
@@ -140,38 +147,38 @@ 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
- * <code>systemId</code> (namespace URI) is not already present in the pool.
- *
- * @param inputStream An <code>InputStream</code> providing the contents of
- * the schema.
- * @param systemId The systemId (namespace URI) to use for the schema.
+ * 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 <code>systemId</code>
+ * (namespace URI) is not already present in the pool.
+ *
+ * @param inputStream An <code>InputStream</code> 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(InputStream inputStream, String systemId)
- throws IOException {
+ throws IOException {
XMLGrammarPreparser preparser;
- if (!grammarNamespaces.contains(systemId)) {
+ if (!grammarNamespaces.contains(systemId)) {
grammarNamespaces.add(systemId);
-
+
// unlock the pool so that we can add another grammar
grammarPool.unlockPool();
-
+
// prepare the preparser
preparser = new XMLGrammarPreparser(symbolTable);
preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
preparser.setProperty(GRAMMAR_POOL, grammarPool);
preparser.setFeature(NAMESPACES_FEATURE, true);
preparser.setFeature(VALIDATION_FEATURE, true);
-
+
// add the grammar to the pool
preparser.preparseGrammar(
- XMLGrammarDescription.XML_SCHEMA,
- new XMLInputSource(null, systemId, null, inputStream, null));
-
+ XMLGrammarDescription.XML_SCHEMA,
+ new XMLInputSource(null, systemId, null, inputStream, null));
+
// lock the pool again so that schemas are not added automatically
grammarPool.lockPool();
}
@@ -179,34 +186,43 @@ public class DOMUtils {
/**
* Parse an XML document from an <code>InputStream</code>.
- *
- * @param inputStream The <code>InputStream</code> containing the XML
- * document.
- * @param validating If <code>true</code>, parse validating.
- * @param externalSchemaLocations A <code>String</code> containing namespace
- * URI to schema location pairs, the same way it is accepted by the <code>xsi:
- * schemaLocation</code> attribute.
- * @param externalNoNamespaceSchemaLocation The schema location of the
- * schema for elements without a namespace, the same way it is accepted by the
- * <code>xsi:noNamespaceSchemaLocation</code> attribute.
- * @param entityResolver An <code>EntityResolver</code> to resolve external
- * entities (schemas and DTDs). If <code>null</code>, it will not be set.
- * @param errorHandler An <code>ErrorHandler</code> to decide what to do
- * with parsing errors. If <code>null</code>, it will not be set.
+ *
+ * @param inputStream The <code>InputStream</code>
+ * containing the XML document.
+ * @param validating If <code>true</code>, parse
+ * validating.
+ * @param externalSchemaLocations A <code>String</code> containing
+ * namespace URI to schema location
+ * pairs, the same way it is accepted
+ * by the <code>xsi:
+ * schemaLocation</code> attribute.
+ * @param externalNoNamespaceSchemaLocation The schema location of the schema
+ * for elements without a namespace,
+ * the same way it is accepted by the
+ * <code>xsi:noNamespaceSchemaLocation</code>
+ * attribute.
+ * @param entityResolver An <code>EntityResolver</code> to
+ * resolve external entities (schemas
+ * and DTDs). If <code>null</code>, it
+ * will not be set.
+ * @param errorHandler An <code>ErrorHandler</code> to
+ * decide what to do with parsing
+ * errors. If <code>null</code>, 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 SAXException An error occurred parsing the document.
+ * @throws IOException An error occurred reading the document.
* @throws ParserConfigurationException An error occurred configuring the XML
- * parser.
+ * parser.
*/
public static Document parseDocument(
- InputStream inputStream,
- boolean validating,
- String externalSchemaLocations,
- String externalNoNamespaceSchemaLocation,
- EntityResolver entityResolver,
- ErrorHandler errorHandler)
- throws SAXException, IOException, ParserConfigurationException {
+ InputStream inputStream,
+ boolean validating,
+ String externalSchemaLocations,
+ String externalNoNamespaceSchemaLocation,
+ EntityResolver entityResolver,
+ ErrorHandler errorHandler)
+ throws SAXException, IOException, ParserConfigurationException {
DOMParser parser;
@@ -218,72 +234,77 @@ public class DOMUtils {
// }
// }
+ // 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 == Logger.isDebugEnabled()) {
+ final int len = inputStream.available();
+ buffer = new byte[len];
+ inputStream.read(buffer);
+ baStream = new ByteArrayInputStream(buffer);
+ }
- //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 == Logger.isDebugEnabled()) {
- int len = inputStream.available();
- buffer = new byte[len];
- inputStream.read(buffer);
- baStream = new ByteArrayInputStream(buffer);
- }
-
// create the DOM parser
if (symbolTable != null) {
parser = new DOMParser(symbolTable, grammarPool);
} else {
parser = new DOMParser();
}
-
+
// set parser features and properties
try {
- parser.setFeature(NAMESPACES_FEATURE, true);
- parser.setFeature(VALIDATION_FEATURE, validating);
- parser.setFeature(SCHEMA_VALIDATION_FEATURE, validating);
- parser.setFeature(NORMALIZED_VALUE_FEATURE, false);
- parser.setFeature(INCLUDE_IGNORABLE_WHITESPACE_FEATURE, true);
- parser.setFeature(CREATE_ENTITY_REF_NODES_FEATURE, false);
- parser.setFeature(EXTERNAL_GENERAL_ENTITIES_FEATURE, false);
- parser.setFeature(EXTERNAL_PARAMETER_ENTITIES_FEATURE, false);
-
- //fix XXE problem
- //parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
-
- if (validating) {
- if (externalSchemaLocations != null) {
- parser.setProperty(
- EXTERNAL_SCHEMA_LOCATION_PROPERTY,
- externalSchemaLocations);
- }
- if (externalNoNamespaceSchemaLocation != null) {
- parser.setProperty(
- EXTERNAL_NO_NAMESPACE_SCHEMA_LOCATION_PROPERTY,
- externalNoNamespaceSchemaLocation);
- }
- }
-
- // set entity resolver and error handler
- if (entityResolver != null) {
- parser.setEntityResolver(entityResolver);
- }
- if (errorHandler != null) {
- parser.setErrorHandler(errorHandler);
- }
-
- // parse the document and return it
- // if debug is enabled: use copy of strem (baStream) else use orig stream
-
- if(null != baStream)
- parser.parse(new InputSource(baStream));
- else
- parser.parse(new InputSource(inputStream));
- } catch(SAXException e) {
- if(true == Logger.isDebugEnabled() && null != buffer) {
- String xmlContent = new String(buffer);
- Logger.debug("SAXException in:\n" + xmlContent);
- }
- throw(e);
+ parser.setFeature(NAMESPACES_FEATURE, true);
+ parser.setFeature(VALIDATION_FEATURE, validating);
+ parser.setFeature(SCHEMA_VALIDATION_FEATURE, validating);
+ parser.setFeature(NORMALIZED_VALUE_FEATURE, false);
+ parser.setFeature(INCLUDE_IGNORABLE_WHITESPACE_FEATURE, true);
+ parser.setFeature(CREATE_ENTITY_REF_NODES_FEATURE, false);
+ parser.setFeature(EXTERNAL_GENERAL_ENTITIES_FEATURE, false);
+ parser.setFeature(EXTERNAL_PARAMETER_ENTITIES_FEATURE, false);
+
+ final SecurityManager xmlParserSecManager = new org.apache.xerces.util.SecurityManager();
+ parser.setProperty(SECURITY_MANAGER, xmlParserSecManager);
+
+ // fix XXE problem
+ // parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl",
+ // true);
+
+ if (validating) {
+ if (externalSchemaLocations != null) {
+ parser.setProperty(
+ EXTERNAL_SCHEMA_LOCATION_PROPERTY,
+ externalSchemaLocations);
+ }
+ if (externalNoNamespaceSchemaLocation != null) {
+ parser.setProperty(
+ EXTERNAL_NO_NAMESPACE_SCHEMA_LOCATION_PROPERTY,
+ externalNoNamespaceSchemaLocation);
+ }
+ }
+
+ // set entity resolver and error handler
+ if (entityResolver != null) {
+ parser.setEntityResolver(entityResolver);
+ }
+ if (errorHandler != null) {
+ parser.setErrorHandler(errorHandler);
+ }
+
+ // parse the document and return it
+ // if debug is enabled: use copy of strem (baStream) else use orig stream
+
+ if (null != baStream) {
+ parser.parse(new InputSource(baStream));
+ } else {
+ parser.parse(new InputSource(inputStream));
+ }
+ } catch (final SAXException e) {
+ if (true == Logger.isDebugEnabled() && null != buffer) {
+ final String xmlContent = new String(buffer);
+ Logger.debug("SAXException in:\n" + xmlContent);
+ }
+ throw e;
}
return parser.getDocument();
@@ -291,31 +312,40 @@ public class DOMUtils {
/**
* Parse an XML document from an <code>InputStream</code>.
- *
- * @param inputStream The <code>InputStream</code> containing the XML
- * document.
- * @param validating If <code>true</code>, parse validating.
- * @param externalSchemaLocations A <code>String</code> containing namespace
- * URI to schema location pairs, the same way it is accepted by the <code>xsi:
- * schemaLocation</code> attribute.
- * @param externalNoNamespaceSchemaLocation The schema location of the
- * schema for elements without a namespace, the same way it is accepted by the
- * <code>xsi:noNamespaceSchemaLocation</code> attribute.
- * @param entityResolver An <code>EntityResolver</code> to resolve external
- * entities (schemas and DTDs). If <code>null</code>, it will not be set.
- * @param errorHandler An <code>ErrorHandler</code> to decide what to do
- * with parsing errors. If <code>null</code>, it will not be set.
+ *
+ * @param inputStream The <code>InputStream</code>
+ * containing the XML document.
+ * @param validating If <code>true</code>, parse
+ * validating.
+ * @param externalSchemaLocations A <code>String</code> containing
+ * namespace URI to schema location
+ * pairs, the same way it is accepted
+ * by the <code>xsi:
+ * schemaLocation</code> attribute.
+ * @param externalNoNamespaceSchemaLocation The schema location of the schema
+ * for elements without a namespace,
+ * the same way it is accepted by the
+ * <code>xsi:noNamespaceSchemaLocation</code>
+ * attribute.
+ * @param entityResolver An <code>EntityResolver</code> to
+ * resolve external entities (schemas
+ * and DTDs). If <code>null</code>, it
+ * will not be set.
+ * @param errorHandler An <code>ErrorHandler</code> to
+ * decide what to do with parsing
+ * errors. If <code>null</code>, 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 SAXException An error occurred parsing the document.
+ * @throws IOException An error occurred reading the document.
* @throws ParserConfigurationException An error occurred configuring the XML
- * parser.
+ * parser.
*/
public static Document parseDocumentSimple(InputStream inputStream)
- throws SAXException, IOException, ParserConfigurationException {
+ throws SAXException, IOException, ParserConfigurationException {
DOMParser parser;
-
+
parser = new DOMParser();
// set parser features and properties
parser.setFeature(NAMESPACES_FEATURE, true);
@@ -324,181 +354,200 @@ public class DOMUtils {
parser.setFeature(NORMALIZED_VALUE_FEATURE, false);
parser.setFeature(INCLUDE_IGNORABLE_WHITESPACE_FEATURE, true);
parser.setFeature(CREATE_ENTITY_REF_NODES_FEATURE, false);
-
+
+ final SecurityManager xmlParserSecManager = new org.apache.xerces.util.SecurityManager();
+ parser.setProperty(SECURITY_MANAGER, xmlParserSecManager);
+
parser.parse(new InputSource(inputStream));
-
+
return parser.getDocument();
}
-
/**
* Parse an XML document from an <code>InputStream</code>.
- *
+ *
* It uses a <code>MOAEntityResolver</code> as the <code>EntityResolver</code>
* and a <code>MOAErrorHandler</code> as the <code>ErrorHandler</code>.
- *
- * @param inputStream The <code>InputStream</code> containing the XML
- * document.
- * @param validating If <code>true</code>, parse validating.
- * @param externalSchemaLocations A <code>String</code> containing namespace
- * URI to schema location pairs, the same way it is accepted by the <code>xsi:
- * schemaLocation</code> attribute.
- * @param externalNoNamespaceSchemaLocation The schema location of the
- * schema for elements without a namespace, the same way it is accepted by the
- * <code>xsi:noNamespaceSchemaLocation</code> attribute.
+ *
+ * @param inputStream The <code>InputStream</code>
+ * containing the XML document.
+ * @param validating If <code>true</code>, parse
+ * validating.
+ * @param externalSchemaLocations A <code>String</code> containing
+ * namespace URI to schema location
+ * pairs, the same way it is accepted
+ * by the <code>xsi:
+ * schemaLocation</code> attribute.
+ * @param externalNoNamespaceSchemaLocation The schema location of the schema
+ * for elements without a namespace,
+ * the same way it is accepted by the
+ * <code>xsi:noNamespaceSchemaLocation</code>
+ * 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 SAXException An error occurred parsing the document.
+ * @throws IOException An error occurred reading the document.
* @throws ParserConfigurationException An error occurred configuring the XML
- * parser.
+ * parser.
*/
public static Document parseDocument(
- InputStream inputStream,
- boolean validating,
- String externalSchemaLocations,
- String externalNoNamespaceSchemaLocation)
- throws SAXException, IOException, ParserConfigurationException {
-
-
-
+ InputStream inputStream,
+ boolean validating,
+ String externalSchemaLocations,
+ String externalNoNamespaceSchemaLocation)
+ throws SAXException, IOException, ParserConfigurationException {
+
return parseDocument(
- inputStream,
- validating,
- externalSchemaLocations,
- externalNoNamespaceSchemaLocation,
- new MOAEntityResolver(),
- new MOAErrorHandler());
+ inputStream,
+ validating,
+ externalSchemaLocations,
+ externalNoNamespaceSchemaLocation,
+ new MOAEntityResolver(),
+ new MOAErrorHandler());
}
/**
* Parse an XML document from a <code>String</code>.
- *
+ *
* It uses a <code>MOAEntityResolver</code> as the <code>EntityResolver</code>
* and a <code>MOAErrorHandler</code> as the <code>ErrorHandler</code>.
- *
- * @param xmlString The <code>String</code> containing the XML document.
- * @param encoding The encoding of the XML document.
- * @param validating If <code>true</code>, parse validating.
- * @param externalSchemaLocations A <code>String</code> containing namespace
- * URI to schema location pairs, the same way it is accepted by the <code>xsi:
- * schemaLocation</code> attribute.
- * @param externalNoNamespaceSchemaLocation The schema location of the
- * schema for elements without a namespace, the same way it is accepted by the
- * <code>xsi:noNamespaceSchemaLocation</code> attribute.
+ *
+ * @param xmlString The <code>String</code> containing
+ * the XML document.
+ * @param encoding The encoding of the XML document.
+ * @param validating If <code>true</code>, parse
+ * validating.
+ * @param externalSchemaLocations A <code>String</code> containing
+ * namespace URI to schema location
+ * pairs, the same way it is accepted
+ * by the <code>xsi:
+ * schemaLocation</code> attribute.
+ * @param externalNoNamespaceSchemaLocation The schema location of the schema
+ * for elements without a namespace,
+ * the same way it is accepted by the
+ * <code>xsi:noNamespaceSchemaLocation</code>
+ * 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 SAXException An error occurred parsing the document.
+ * @throws IOException An error occurred reading the document.
* @throws ParserConfigurationException An error occurred configuring the XML
- * parser.
+ * parser.
*/
public static Document parseDocument(
- String xmlString,
- String encoding,
- boolean validating,
- String externalSchemaLocations,
- String externalNoNamespaceSchemaLocation)
- throws SAXException, IOException, ParserConfigurationException {
-
- InputStream in = new ByteArrayInputStream(xmlString.getBytes(encoding));
+ String xmlString,
+ String encoding,
+ boolean validating,
+ String externalSchemaLocations,
+ String externalNoNamespaceSchemaLocation)
+ throws SAXException, IOException, ParserConfigurationException {
+
+ final InputStream in = new ByteArrayInputStream(xmlString.getBytes(encoding));
return parseDocument(
- in,
- validating,
- externalSchemaLocations,
- externalNoNamespaceSchemaLocation);
+ in,
+ validating,
+ externalSchemaLocations,
+ externalNoNamespaceSchemaLocation);
}
/**
* Parse an UTF-8 encoded XML document from a <code>String</code>.
- *
- * @param xmlString The <code>String</code> containing the XML document.
- * @param validating If <code>true</code>, parse validating.
- * @param externalSchemaLocations A <code>String</code> containing namespace
- * URI to schema location pairs, the same way it is accepted by the <code>xsi:
- * schemaLocation</code> attribute.
- * @param externalNoNamespaceSchemaLocation The schema location of the
- * schema for elements without a namespace, the same way it is accepted by the
- * <code>xsi:noNamespaceSchemaLocation</code> attribute.
+ *
+ * @param xmlString The <code>String</code> containing
+ * the XML document.
+ * @param validating If <code>true</code>, parse
+ * validating.
+ * @param externalSchemaLocations A <code>String</code> containing
+ * namespace URI to schema location
+ * pairs, the same way it is accepted
+ * by the <code>xsi:
+ * schemaLocation</code> attribute.
+ * @param externalNoNamespaceSchemaLocation The schema location of the schema
+ * for elements without a namespace,
+ * the same way it is accepted by the
+ * <code>xsi:noNamespaceSchemaLocation</code>
+ * 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 SAXException An error occurred parsing the document.
+ * @throws IOException An error occurred reading the document.
* @throws ParserConfigurationException An error occurred configuring the XML
- * parser.
+ * parser.
*/
public static Document parseDocument(
- String xmlString,
- boolean validating,
- String externalSchemaLocations,
- String externalNoNamespaceSchemaLocation)
- throws SAXException, IOException, ParserConfigurationException {
+ String xmlString,
+ boolean validating,
+ String externalSchemaLocations,
+ String externalNoNamespaceSchemaLocation)
+ throws SAXException, IOException, ParserConfigurationException {
return parseDocument(
- xmlString,
- "UTF-8",
- validating,
- externalSchemaLocations,
- externalNoNamespaceSchemaLocation);
+ xmlString,
+ "UTF-8",
+ validating,
+ externalSchemaLocations,
+ externalNoNamespaceSchemaLocation);
}
/**
* A convenience method to parse an XML document validating.
- *
- * @param inputStream The <code>InputStream</code> containing the XML
- * document.
+ *
+ * @param inputStream The <code>InputStream</code> 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 SAXException An error occurred parsing the document.
+ * @throws IOException An error occurred reading the document.
* @throws ParserConfigurationException An error occurred configuring the XML
- * parser.
+ * parser.
*/
public static Element parseXmlValidating(InputStream inputStream)
- throws ParserConfigurationException, SAXException, IOException {
+ throws ParserConfigurationException, SAXException, IOException {
return DOMUtils
- .parseDocument(inputStream, true, Constants.ALL_SCHEMA_LOCATIONS, null)
- .getDocumentElement();
+ .parseDocument(inputStream, true, Constants.ALL_SCHEMA_LOCATIONS, null)
+ .getDocumentElement();
}
-
+
/**
* A convenience method to parse an XML document non validating.
- *
- * @param inputStream The <code>InputStream</code> containing the XML
- * document.
+ *
+ * @param inputStream The <code>InputStream</code> 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 SAXException An error occurred parsing the document.
+ * @throws IOException An error occurred reading the document.
* @throws ParserConfigurationException An error occurred configuring the XML
- * parser.
+ * parser.
*/
public static Element parseXmlNonValidating(InputStream inputStream)
- throws ParserConfigurationException, SAXException, IOException {
+ throws ParserConfigurationException, SAXException, IOException {
return DOMUtils
- .parseDocument(inputStream, false, Constants.ALL_SCHEMA_LOCATIONS, null)
- .getDocumentElement();
+ .parseDocument(inputStream, false, Constants.ALL_SCHEMA_LOCATIONS, null)
+ .getDocumentElement();
}
/**
* Schema validate a given DOM element.
- *
- * @param element The element to validate.
- * @param externalSchemaLocations A <code>String</code> containing namespace
- * URI to schema location pairs, the same way it is accepted by the <code>xsi:
- * schemaLocation</code> attribute.
- * @param externalNoNamespaceSchemaLocation The schema location of the
- * schema for elements without a namespace, the same way it is accepted by the
- * <code>xsi:noNamespaceSchemaLocation</code> attribute.
- * @return <code>true</code>, if the <code>element</code> 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 <code>String</code> containing
+ * namespace URI to schema location
+ * pairs, the same way it is accepted
+ * by the <code>xsi:
+ * schemaLocation</code> attribute.
+ * @param externalNoNamespaceSchemaLocation The schema location of the schema
+ * for elements without a namespace,
+ * the same way it is accepted by the
+ * <code>xsi:noNamespaceSchemaLocation</code>
+ * attribute.
+ * @return <code>true</code>, if the <code>element</code> 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(
- Element element,
- String externalSchemaLocations,
- String externalNoNamespaceSchemaLocation)
- throws
- ParserConfigurationException,
+ Element element,
+ String externalSchemaLocations,
+ String externalNoNamespaceSchemaLocation)
+ throws ParserConfigurationException,
IOException,
SAXException,
TransformerException {
@@ -522,16 +571,16 @@ public class DOMUtils {
parser.setFeature(SCHEMA_VALIDATION_FEATURE, true);
parser.setFeature(EXTERNAL_GENERAL_ENTITIES_FEATURE, false);
parser.setFeature(DISALLOW_DOCTYPE_FEATURE, true);
-
+
if (externalSchemaLocations != null) {
parser.setProperty(
- EXTERNAL_SCHEMA_LOCATION_PROPERTY,
- externalSchemaLocations);
+ EXTERNAL_SCHEMA_LOCATION_PROPERTY,
+ externalSchemaLocations);
}
if (externalNoNamespaceSchemaLocation != null) {
parser.setProperty(
- EXTERNAL_NO_NAMESPACE_SCHEMA_LOCATION_PROPERTY,
- "externalNoNamespaceSchemaLocation");
+ EXTERNAL_NO_NAMESPACE_SCHEMA_LOCATION_PROPERTY,
+ "externalNoNamespaceSchemaLocation");
}
// set up entity resolver and error handler
@@ -543,32 +592,35 @@ public class DOMUtils {
return true;
}
-
/**
* Schema validate a given DOM element.
- *
- * @param element The element to validate.
- * @param externalSchemaLocations A <code>String</code> containing namespace
- * URI to schema location pairs, the same way it is accepted by the <code>xsi:
- * schemaLocation</code> attribute.
- * @param externalNoNamespaceSchemaLocation The schema location of the
- * schema for elements without a namespace, the same way it is accepted by the
- * <code>xsi:noNamespaceSchemaLocation</code> attribute.
- * @return <code>true</code>, if the <code>element</code> 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 <code>String</code> containing
+ * namespace URI to schema location
+ * pairs, the same way it is accepted
+ * by the <code>xsi:
+ * schemaLocation</code> attribute.
+ * @param externalNoNamespaceSchemaLocation The schema location of the schema
+ * for elements without a namespace,
+ * the same way it is accepted by the
+ * <code>xsi:noNamespaceSchemaLocation</code>
+ * attribute.
+ * @return <code>true</code>, if the <code>element</code> 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(
- Element element,
- String externalSchemaLocations,
- String externalNoNamespaceSchemaLocation,
- EntityResolver entityResolver)
- throws
- ParserConfigurationException,
+ Element element,
+ String externalSchemaLocations,
+ String externalNoNamespaceSchemaLocation,
+ EntityResolver entityResolver)
+ throws ParserConfigurationException,
IOException,
SAXException,
TransformerException {
@@ -590,16 +642,19 @@ public class DOMUtils {
parser.setFeature(NAMESPACES_FEATURE, true);
parser.setFeature(VALIDATION_FEATURE, true);
parser.setFeature(SCHEMA_VALIDATION_FEATURE, true);
-
+
+ final SecurityManager xmlParserSecManager = new org.apache.xerces.util.SecurityManager();
+ parser.setProperty(SECURITY_MANAGER, xmlParserSecManager);
+
if (externalSchemaLocations != null) {
parser.setProperty(
- EXTERNAL_SCHEMA_LOCATION_PROPERTY,
- externalSchemaLocations);
+ EXTERNAL_SCHEMA_LOCATION_PROPERTY,
+ externalSchemaLocations);
}
if (externalNoNamespaceSchemaLocation != null) {
parser.setProperty(
- EXTERNAL_NO_NAMESPACE_SCHEMA_LOCATION_PROPERTY,
- "externalNoNamespaceSchemaLocation");
+ EXTERNAL_NO_NAMESPACE_SCHEMA_LOCATION_PROPERTY,
+ "externalNoNamespaceSchemaLocation");
}
// set up entity resolver and error handler
@@ -610,122 +665,127 @@ public class DOMUtils {
parser.parse(new InputSource(new ByteArrayInputStream(docBytes)));
return true;
}
-
+
/**
* Serialize the given DOM node.
- *
+ *
* The node will be serialized using the UTF-8 encoding.
- *
+ *
* @param node The node to serialize.
- * @return String The <code>String</code> representation of the given DOM
- * node.
- * @throws TransformerException An error occurred transforming the
- * node to a <code>String</code>.
- * @throws IOException An IO error occurred writing the node to a byte array.
+ * @return String The <code>String</code> representation of the given DOM node.
+ * @throws TransformerException An error occurred transforming the node to a
+ * <code>String</code>.
+ * @throws IOException An IO error occurred writing the node to a byte
+ * array.
*/
public static String serializeNode(Node node)
- throws TransformerException, IOException {
+ 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 <code>String</code> representation of the given DOM
- * node.
- * @throws TransformerException An error occurred transforming the
- * node to a <code>String</code>.
- * @throws IOException An IO error occurred writing the node to a byte array.
+ * @return String The <code>String</code> representation of the given DOM node.
+ * @throws TransformerException An error occurred transforming the node to a
+ * <code>String</code>.
+ * @throws IOException An IO error occurred writing the node to a byte
+ * array.
*/
public static String serializeNode(Node node, boolean omitXmlDeclaration)
- throws TransformerException, IOException {
+ throws TransformerException, IOException {
return new String(serializeNode(node, "UTF-8", omitXmlDeclaration), "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.
- * @param lineSeperator Sets the line seperator String of the parser
- * @return String The <code>String</code> representation of the given DOM
- * node.
- * @throws TransformerException An error occurred transforming the
- * node to a <code>String</code>.
- * @throws IOException An IO error occurred writing the node to a byte array.
+ * @param lineSeperator Sets the line seperator String of the parser
+ * @return String The <code>String</code> representation of the given DOM node.
+ * @throws TransformerException An error occurred transforming the node to a
+ * <code>String</code>.
+ * @throws IOException An IO error occurred writing the node to a byte
+ * array.
*/
public static String serializeNode(Node node, boolean omitXmlDeclaration, String lineSeperator)
- throws TransformerException, IOException {
+ throws TransformerException, IOException {
return new String(serializeNode(node, "UTF-8", omitXmlDeclaration, lineSeperator), "UTF-8");
}
-
+
/**
* 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 <code>String</code>.
- * @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.
+ * this can easily be converted into a <code>String</code>.
+ * @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(Node node, String xmlEncoding)
- throws TransformerException, IOException {
+ throws TransformerException, IOException {
return serializeNode(node, xmlEncoding, false);
}
-
+
/**
* 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 <code>String</code>.
- * @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.
+ * this can easily be converted into a <code>String</code>.
+ * @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(Node node, String xmlEncoding, boolean omitDeclaration)
- throws TransformerException, IOException {
+ 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
+ * @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 <code>String</code>.
- * @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.
+ * this can easily be converted into a <code>String</code>.
+ * @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(Node node, String xmlEncoding, boolean omitDeclaration, String lineSeperator)
- throws TransformerException, IOException {
+ public static byte[] serializeNode(Node node, String xmlEncoding, boolean omitDeclaration,
+ String lineSeperator)
+ throws TransformerException, IOException {
- TransformerFactory transformerFactory = TransformerFactory.newInstance();
- Transformer transformer = transformerFactory.newTransformer();
- ByteArrayOutputStream bos = new ByteArrayOutputStream(16384);
+ final TransformerFactory transformerFactory = TransformerFactory.newInstance();
+ final Transformer transformer = transformerFactory.newTransformer();
+ final ByteArrayOutputStream bos = new ByteArrayOutputStream(16384);
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.ENCODING, xmlEncoding);
- String omit = omitDeclaration ? "yes" : "no";
+ final String omit = omitDeclaration ? "yes" : "no";
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
+ if (null != lineSeperator) {
+ transformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", lineSeperator);// does not
+ // work for
+ // xalan <=
+ // 2.5.1
}
transformer.transform(new DOMSource(node), new StreamResult(bos));
@@ -736,30 +796,30 @@ public class DOMUtils {
}
/**
- * Return the text that a node contains.
- *
- * This routine:
- * <ul>
- * <li>Ignores comments and processing instructions.</li>
- * <li>Concatenates TEXT nodes, CDATA nodes, and the results recursively
- * processing EntityRef nodes.</li>
- * <li>Ignores any element nodes in the sublist. (Other possible options are
- * to recurse into element sublists or throw an exception.)</li>
- * </ul>
- *
- * @param node A DOM node from which to extract text.
- * @return A String representing its contents.
- */
+ * Return the text that a node contains.
+ *
+ * This routine:
+ * <ul>
+ * <li>Ignores comments and processing instructions.</li>
+ * <li>Concatenates TEXT nodes, CDATA nodes, and the results recursively
+ * processing EntityRef nodes.</li>
+ * <li>Ignores any element nodes in the sublist. (Other possible options are to
+ * recurse into element sublists or throw an exception.)</li>
+ * </ul>
+ *
+ * @param node A DOM node from which to extract text.
+ * @return A String representing its contents.
+ */
public static String getText(Node node) {
if (!node.hasChildNodes()) {
return "";
}
- StringBuffer result = new StringBuffer();
- NodeList list = node.getChildNodes();
+ final StringBuffer result = new StringBuffer();
+ final NodeList list = node.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
- Node subnode = list.item(i);
+ final Node subnode = list.item(i);
if (subnode.getNodeType() == Node.TEXT_NODE) {
result.append(subnode.getNodeValue());
} else if (subnode.getNodeType() == Node.CDATA_SECTION_NODE) {
@@ -776,28 +836,28 @@ public class DOMUtils {
/**
* 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 <code>String</code> value to <code>String</code> value mapping).
+ * @return The namespace prefix to namespace URL mapping ( a <code>String</code>
+ * value to <code>String</code> value mapping).
*/
public static Map getNamespaceDeclarations(Node node) {
- Map nsDecls = new HashMap();
+ final Map nsDecls = new HashMap();
int i;
do {
if (node.hasAttributes()) {
- NamedNodeMap attrs = node.getAttributes();
+ final NamedNodeMap attrs = node.getAttributes();
for (i = 0; i < attrs.getLength(); i++) {
- Attr attr = (Attr) attrs.item(i);
+ final Attr attr = (Attr) attrs.item(i);
// add prefix mapping if none exists
if ("xmlns".equals(attr.getPrefix())
- || "xmlns".equals(attr.getName())) {
+ || "xmlns".equals(attr.getName())) {
- String nsPrefix =
- attr.getPrefix() != null ? attr.getLocalName() : "";
+ final String nsPrefix =
+ attr.getPrefix() != null ? attr.getLocalName() : "";
if (nsDecls.get(nsPrefix) == null) {
nsDecls.put(nsPrefix, attr.getValue());
@@ -811,26 +871,26 @@ 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.
*/
public static void localizeNamespaceDeclarations(Element context) {
- Node parent = context.getParentNode();
+ final Node parent = context.getParentNode();
if (parent != null) {
- Map namespaces = getNamespaceDeclarations(context.getParentNode());
- Set nsUris = collectNamespaceURIs(context);
+ final Map namespaces = getNamespaceDeclarations(context.getParentNode());
+ final Set nsUris = collectNamespaceURIs(context);
Iterator iter;
for (iter = namespaces.entrySet().iterator(); iter.hasNext();) {
- Map.Entry e = (Map.Entry) iter.next();
+ final Map.Entry e = (Map.Entry) iter.next();
if (nsUris.contains(e.getValue())) {
- String prefix = (String) e.getKey();
- String nsUri = (String) e.getValue();
- String nsAttrName = "".equals(prefix) ? "xmlns" : "xmlns:" + prefix;
+ final String prefix = (String) e.getKey();
+ final String nsUri = (String) e.getValue();
+ final String nsAttrName = "".equals(prefix) ? "xmlns" : "xmlns:" + prefix;
context.setAttributeNS(Constants.XMLNS_NS_URI, nsAttrName, nsUri);
}
@@ -840,13 +900,13 @@ 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 <code>context</code>,
- * including the ones used in <code>context</code> itself.
+ * including the ones used in <code>context</code> itself.
*/
public static Set collectNamespaceURIs(Element context) {
- Set result = new HashSet();
+ final Set result = new HashSet();
collectNamespaceURIsImpl(context, result);
return result;
@@ -854,14 +914,14 @@ public class DOMUtils {
/**
* A recursive method to do the work of <code>collectNamespaceURIs</code>.
- *
+ *
* @param context The context element to evaluate.
- * @param result The result, passed as a parameter to avoid unnecessary
- * instantiations of <code>Set</code>.
+ * @param result The result, passed as a parameter to avoid unnecessary
+ * instantiations of <code>Set</code>.
*/
private static void collectNamespaceURIsImpl(Element context, Set result) {
- NamedNodeMap attrs = context.getAttributes();
- NodeList childNodes = context.getChildNodes();
+ final NamedNodeMap attrs = context.getAttributes();
+ final NodeList childNodes = context.getChildNodes();
String nsUri;
int i;
@@ -881,7 +941,7 @@ public class DOMUtils {
// add all namespaces from subelements
for (i = 0; i < childNodes.getLength(); i++) {
- Node node = childNodes.item(i);
+ final Node node = childNodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
collectNamespaceURIsImpl((Element) node, result);
@@ -892,13 +952,13 @@ public class DOMUtils {
/**
* Check, that each attribute node in the given <code>NodeList</code> has its
* parent in the <code>NodeList</code> as well.
- *
+ *
* @param nodes The <code>NodeList</code> to check.
- * @return <code>true</code>, if each attribute node in <code>nodes</code>
- * has its parent in <code>nodes</code> as well.
+ * @return <code>true</code>, if each attribute node in <code>nodes</code> has
+ * its parent in <code>nodes</code> as well.
*/
public static boolean checkAttributeParentsInNodeList(NodeList nodes) {
- Set nodeSet = new HashSet();
+ final Set nodeSet = new HashSet();
int i;
// put the nodes into the nodeSet
@@ -908,11 +968,11 @@ public class DOMUtils {
// check that each attribute node's parent is in the node list
for (i = 0; i < nodes.getLength(); i++) {
- Node n = nodes.item(i);
+ final Node n = nodes.item(i);
if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
- Attr attr = (Attr) n;
- Element owner = attr.getOwnerElement();
+ final Attr attr = (Attr) n;
+ final Element owner = attr.getOwnerElement();
if (owner == null) {
if (!isNamespaceDeclaration(attr)) {
@@ -930,29 +990,28 @@ public class DOMUtils {
}
/**
- * Convert an unstructured <code>NodeList</code> into a
+ * Convert an unstructured <code>NodeList</code> into a
* <code>DocumentFragment</code>.
*
- * @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.
+ * @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.
+ * 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 each Element or Attr node in the node list is namespace
- * aware.
+ * @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(NodeList nodeList)
- throws ParserConfigurationException {
+ throws ParserConfigurationException {
- DocumentBuilder builder =
- DocumentBuilderFactory.newInstance().newDocumentBuilder();
- Document doc = builder.newDocument();
- DocumentFragment result = doc.createDocumentFragment();
+ final DocumentBuilder builder =
+ DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ final Document doc = builder.newDocument();
+ final DocumentFragment result = doc.createDocumentFragment();
if (null == nodeList || nodeList.getLength() == 0) {
return result;
@@ -960,116 +1019,114 @@ public class DOMUtils {
int currPos = 0;
currPos =
- nodeList2DocumentFragment(nodeList, currPos, result, null, null) + 1;
+ nodeList2DocumentFragment(nodeList, currPos, result, null, null) + 1;
while (currPos < nodeList.getLength()) {
currPos =
- nodeList2DocumentFragment(nodeList, currPos, result, null, null) + 1;
+ nodeList2DocumentFragment(nodeList, currPos, result, null, null) + 1;
}
return result;
}
/**
* Helper method for the <code>nodeList2DocumentFragment</code>.
- *
- * @param nodeList The <code>NodeList</code> to convert.
- * @param currPos The current position in the <code>nodeList</code>.
- * @param result The resulting <code>DocumentFragment</code>.
- * @param currOrgElem The current original element.
+ *
+ * @param nodeList The <code>NodeList</code> to convert.
+ * @param currPos The current position in the <code>nodeList</code>.
+ * @param result The resulting <code>DocumentFragment</code>.
+ * @param currOrgElem The current original element.
* @param currClonedElem The current cloned element.
* @return The current position.
*/
private static int nodeList2DocumentFragment(
- NodeList nodeList,
- int currPos,
- DocumentFragment result,
- Element currOrgElem,
- Element currClonedElem) {
+ NodeList nodeList,
+ int currPos,
+ DocumentFragment result,
+ Element currOrgElem,
+ Element currClonedElem) {
while (currPos < nodeList.getLength()) {
- Node currentNode = nodeList.item(currPos);
+ final Node currentNode = nodeList.item(currPos);
switch (currentNode.getNodeType()) {
- case Node.COMMENT_NODE :
- case Node.PROCESSING_INSTRUCTION_NODE :
- case Node.TEXT_NODE :
- {
- // Append current node either to resulting DocumentFragment or to
- // current cloned Element
- if (null == currClonedElem) {
- result.appendChild(
- result.getOwnerDocument().importNode(currentNode, false));
- } else {
- // Stop processing if current Node is not a descendant of
- // current Element
- if (!isAncestor(currOrgElem, currentNode)) {
- return --currPos;
- }
-
- currClonedElem.appendChild(
+ case Node.COMMENT_NODE:
+ case Node.PROCESSING_INSTRUCTION_NODE:
+ case Node.TEXT_NODE: {
+ // Append current node either to resulting DocumentFragment or to
+ // current cloned Element
+ if (null == currClonedElem) {
+ result.appendChild(
result.getOwnerDocument().importNode(currentNode, false));
+ } else {
+ // Stop processing if current Node is not a descendant of
+ // current Element
+ if (!isAncestor(currOrgElem, currentNode)) {
+ return --currPos;
}
- break;
+
+ currClonedElem.appendChild(
+ result.getOwnerDocument().importNode(currentNode, false));
}
+ break;
+ }
- case Node.ELEMENT_NODE :
- {
- Element nextCurrOrgElem = (Element) currentNode;
- Element nextCurrClonedElem =
+ case Node.ELEMENT_NODE: {
+ final Element nextCurrOrgElem = (Element) currentNode;
+ final Element nextCurrClonedElem =
result.getOwnerDocument().createElementNS(
- nextCurrOrgElem.getNamespaceURI(),
- nextCurrOrgElem.getNodeName());
-
- // Append current Node either to resulting DocumentFragment or to
- // current cloned Element
- if (null == currClonedElem) {
- result.appendChild(nextCurrClonedElem);
- currOrgElem = nextCurrOrgElem;
- currClonedElem = nextCurrClonedElem;
- } else {
- // Stop processing if current Node is not a descendant of
- // current Element
- if (!isAncestor(currOrgElem, currentNode)) {
- return --currPos;
- }
-
- currClonedElem.appendChild(nextCurrClonedElem);
+ nextCurrOrgElem.getNamespaceURI(),
+ nextCurrOrgElem.getNodeName());
+
+ // Append current Node either to resulting DocumentFragment or to
+ // current cloned Element
+ if (null == currClonedElem) {
+ result.appendChild(nextCurrClonedElem);
+ currOrgElem = nextCurrOrgElem;
+ currClonedElem = nextCurrClonedElem;
+ } else {
+ // Stop processing if current Node is not a descendant of
+ // current Element
+ if (!isAncestor(currOrgElem, currentNode)) {
+ return --currPos;
}
- // Process current Node (of type Element) recursively
- currPos =
+ currClonedElem.appendChild(nextCurrClonedElem);
+ }
+
+ // Process current Node (of type Element) recursively
+ currPos =
nodeList2DocumentFragment(
- nodeList,
- ++currPos,
- result,
- nextCurrOrgElem,
- nextCurrClonedElem);
+ nodeList,
+ ++currPos,
+ result,
+ nextCurrOrgElem,
+ nextCurrClonedElem);
- break;
- }
+ break;
+ }
- case Node.ATTRIBUTE_NODE :
- {
- Attr currAttr = (Attr) currentNode;
+ case Node.ATTRIBUTE_NODE: {
+ final Attr currAttr = (Attr) currentNode;
- // GK 20030411: Hack to overcome problems with IAIK IXSIL
- if (currAttr.getOwnerElement() == null)
- break;
- if (currClonedElem == null)
- break;
+ // GK 20030411: Hack to overcome problems with IAIK IXSIL
+ if (currAttr.getOwnerElement() == null) {
+ break;
+ }
+ if (currClonedElem == null) {
+ break;
+ }
- // currClonedElem must be the owner Element of currAttr if
- // preconditions are met
- currClonedElem.setAttributeNS(
+ // currClonedElem must be the owner Element of currAttr if
+ // preconditions are met
+ currClonedElem.setAttributeNS(
currAttr.getNamespaceURI(),
currAttr.getNodeName(),
currAttr.getValue());
- break;
- }
+ break;
+ }
- default :
- {
- // All other nodes will be ignored
- }
+ default: {
+ // All other nodes will be ignored
+ }
}
currPos++;
@@ -1080,10 +1137,10 @@ public class DOMUtils {
/**
* Check, if the given attribute is a namespace declaration.
- *
+ *
* @param attr The attribute to check.
* @return <code>true</code>, if the attribute is a namespace declaration,
- * <code>false</code> otherwise.
+ * <code>false</code> otherwise.
*/
private static boolean isNamespaceDeclaration(Attr attr) {
return Constants.XMLNS_NS_URI.equals(attr.getNamespaceURI());
@@ -1091,81 +1148,82 @@ public class DOMUtils {
/**
* 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 <code>true</code>, if <code>candAnc</code> is an (indirect)
- * ancestor of <code>cand</code>; <code>false</code> otherwise.
+ * @param cand The node to check for being the child.
+ * @return <code>true</code>, if <code>candAnc</code> is an (indirect) ancestor
+ * of <code>cand</code>; <code>false</code> otherwise.
*/
public static boolean isAncestor(Element candAnc, Node cand) {
Node currPar = cand.getParentNode();
while (currPar != null) {
- if (candAnc == currPar)
+ if (candAnc == currPar) {
return true;
+ }
currPar = currPar.getParentNode();
}
return false;
}
-
+
/**
* 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 <code>null</code>
- * if the node list is <code>null</code> or empty or no element is
- * included in the list.
+ *
+ * @param nl The NodeList to get the element from.
+ * @return The (first) element included in the node list or <code>null</code> if
+ * the node list is <code>null</code> or empty or no element is included
+ * in the list.
*/
- public static Element getElementFromNodeList (NodeList nl) {
- if ((nl == null) || (nl.getLength() == 0)) {
+ public static Element getElementFromNodeList(NodeList nl) {
+ if (nl == null || nl.getLength() == 0) {
return null;
}
- for (int i=0; i<nl.getLength(); i++) {
- Node node = nl.item(i);
- if (node.getNodeType() == Node.ELEMENT_NODE) {
- return (Element)node;
+ for (int i = 0; i < nl.getLength(); i++) {
+ final Node node = nl.item(i);
+ if (node.getNodeType() == Node.ELEMENT_NODE) {
+ return (Element) node;
}
}
return null;
}
-
+
/**
* Returns all child elements of the given element.
- *
- * @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.
+ *
+ * @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.
*/
- public static List getChildElements (Element parent) {
- Vector v = new Vector();
- NodeList nl = parent.getChildNodes();
- int length = nl.getLength();
- for (int i=0; i < length; i++) {
- Node node = nl.item(i);
+ public static List getChildElements(Element parent) {
+ final Vector v = new Vector();
+ final NodeList nl = parent.getChildNodes();
+ final int length = nl.getLength();
+ for (int i = 0; i < length; i++) {
+ final Node node = nl.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
- v.add((Element)node);
+ v.add(node);
}
}
return v;
}
-
+
/**
* Returns a byte array from given node.
+ *
* @param node
* @return
* @throws TransformerException
*/
public static byte[] nodeToByteArray(Node node) throws TransformerException {
- Source source = new DOMSource(node);
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- //StringWriter stringWriter = new StringWriter();
- Result result = new StreamResult(out);
- TransformerFactory factory = TransformerFactory.newInstance();
- Transformer transformer = factory.newTransformer();
- transformer.transform(source, result);
- return out.toByteArray();
+ final Source source = new DOMSource(node);
+ final ByteArrayOutputStream out = new ByteArrayOutputStream();
+ // StringWriter stringWriter = new StringWriter();
+ final Result result = new StreamResult(out);
+ final TransformerFactory factory = TransformerFactory.newInstance();
+ final Transformer transformer = factory.newTransformer();
+ transformer.transform(source, result);
+ return out.toByteArray();
}
-
}