added XMLDocument, XMLSchema, and XSLTransformation classes.
[utils] / support / general / src / main / java / org / wamblee / xml / DomUtils.java
index d5e3bee47e1bc8b74612748829d8948ed46120a6..f283ee07d9d51ff344d9f803fede157a70eba521 100644 (file)
@@ -1,12 +1,12 @@
 /*
- * Copyright 2005 the original author or authors.
- *
+ * Copyright 2005-2010 the original author or authors.
+ * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- *
+ * 
  *      http://www.apache.org/licenses/LICENSE-2.0
- *
+ * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  */
 package org.wamblee.xml;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-import org.apache.xml.serialize.OutputFormat;
-import org.apache.xml.serialize.XMLSerializer;
-
-import org.dom4j.DocumentException;
-
-import org.dom4j.io.DOMReader;
-import org.dom4j.io.DOMWriter;
-
-import org.w3c.dom.Attr;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.NamedNodeMap;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-
-import org.xml.sax.SAXException;
-
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javax.xml.XMLConstants;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
 import javax.xml.validation.Schema;
 import javax.xml.validation.SchemaFactory;
+import javax.xml.validation.Validator;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.bootstrap.DOMImplementationRegistry;
+import org.w3c.dom.ls.DOMImplementationLS;
+import org.w3c.dom.ls.LSException;
+import org.w3c.dom.ls.LSInput;
+import org.w3c.dom.ls.LSParser;
+import org.xml.sax.SAXException;
 
 /**
  * Some basic XML utilities for common reoccuring tasks for DOM documents.
@@ -60,7 +60,8 @@ import javax.xml.validation.SchemaFactory;
  * @author Erik Brakkee
  */
 public final class DomUtils {
-    private static final Log LOG = LogFactory.getLog(DomUtils.class);
+    private static final Logger LOG = Logger
+        .getLogger(DomUtils.class.getName());
 
     /**
      * Disabled default constructor.
@@ -71,153 +72,32 @@ public final class DomUtils {
     }
 
     /**
-     * Parses an XML document from a string.
-     * 
-     * @param aDocument
-     *            document.
-     * 
-     * @return
-     * 
-     */
-    public static Document read(String aDocument) throws XMLException {
-        ByteArrayInputStream is = new ByteArrayInputStream(aDocument.getBytes());
-
-        return read(is);
-    }
-
-    /**
-     * Parses an XML document from a stream.
-     * 
-     * @param aIs
-     *            Input stream.
-     * 
-     * @return
-     * 
-     */
-    public static Document read(InputStream aIs) throws XMLException {
-        try {
-            DocumentBuilder builder = DocumentBuilderFactory.newInstance()
-                .newDocumentBuilder();
-
-            return builder.parse(aIs);
-        } catch (SAXException e) {
-            throw new XMLException(e.getMessage(), e);
-        } catch (IOException e) {
-            throw new XMLException(e.getMessage(), e);
-        } catch (ParserConfigurationException e) {
-            throw new XMLException(e.getMessage(), e);
-        } finally {
-            try {
-                aIs.close();
-            } catch (Exception e) {
-                LOG.warn("Error closing XML file", e);
-            }
-        }
-    }
-
-    /**
-     * Reads and validates a document against a schema.
-     * 
-     * @param aIs
-     *            Input stream.
-     * @param aSchema
-     *            Schema.
-     * 
-     * @return Parsed and validated document.
-     * 
+     * Gets a dom level 3 implementation. 
+     * @return Dom implementation.
+     * @throws ClassNotFoundException
+     * @throws InstantiationException
+     * @throws IllegalAccessException
      */
-    public static Document readAndValidate(InputStream aIs, InputStream aSchema)
-        throws XMLException {
+    public static DOMImplementationLS getDomImplementationLS() {
+        final String message = "Could not get Dom level 3 implementation";
         try {
-            final Schema schema = SchemaFactory.newInstance(
-                XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(
-                new StreamSource(aSchema));
-
-            final DocumentBuilderFactory factory = DocumentBuilderFactory
+            DOMImplementationRegistry registry = DOMImplementationRegistry
                 .newInstance();
-            factory.setValidating(true);
-            factory.setNamespaceAware(true);
-            factory.setSchema(schema);
-
-            return factory.newDocumentBuilder().parse(aIs);
-        } catch (SAXException e) {
-            throw new XMLException(e.getMessage(), e);
-        } catch (IOException e) {
-            throw new XMLException(e.getMessage(), e);
-        } catch (ParserConfigurationException e) {
-            throw new XMLException(e.getMessage(), e);
-        } finally {
-            try {
-                aSchema.close();
-            } catch (Exception e) {
-                LOG.warn("Error closing schema", e);
-            }
 
-            try {
-                aIs.close();
-            } catch (Exception e) {
-                LOG.warn("Error closing XML file", e);
-            }
+            DOMImplementationLS impl = (DOMImplementationLS) registry
+                .getDOMImplementation("LS");
+            return impl;
+        } catch (ClassCastException e) {
+            throw new RuntimeException(message, e);
+        } catch (ClassNotFoundException e) {
+            throw new RuntimeException(message, e);
+        } catch (InstantiationException e) {
+            throw new RuntimeException(message, e);
+        } catch (IllegalAccessException e) {
+            throw new RuntimeException(message, e);
         }
     }
 
-    /**
-     * Serializes an XML document to a stream.
-     * 
-     * @param aDocument
-     *            Document to serialize.
-     * @param aOs
-     *            Output stream.
-     * 
-     */
-    public static void serialize(Document aDocument, OutputStream aOs)
-        throws IOException {
-        XMLSerializer serializer = new XMLSerializer(aOs, new OutputFormat());
-        serializer.serialize(aDocument);
-    }
-
-    /**
-     * Serializes an XML document.
-     * 
-     * @param aDocument
-     *            Document to serialize.
-     * 
-     * @return Serialized document.
-     * 
-     */
-    public static String serialize(Document aDocument) throws IOException {
-        ByteArrayOutputStream os = new ByteArrayOutputStream();
-        serialize(aDocument, os);
-
-        return os.toString();
-    }
-
-    /**
-     * Converts a dom4j document into a w3c DOM document.
-     * 
-     * @param aDocument
-     *            Document to convert.
-     * 
-     * @return W3C DOM document.
-     * 
-     */
-    public static Document convert(org.dom4j.Document aDocument)
-        throws DocumentException {
-        return new DOMWriter().write(aDocument);
-    }
-
-    /**
-     * Converts a W3C DOM document into a dom4j document.
-     * 
-     * @param aDocument
-     *            Document to convert.
-     * 
-     * @return Dom4j document.
-     */
-    public static org.dom4j.Document convert(Document aDocument) {
-        return new DOMReader().read(aDocument);
-    }
-
     /**
      * Removes duplicate attributes from a DOM tree.This is useful for
      * postprocessing the output of JTidy as a workaround for a bug in JTidy.