Now using the dom level 3 API for parsing. Also extended the test case
[utils] / support / general / src / main / java / org / wamblee / xml / DomUtils.java
index 8c8128c42f67360bcb9695796d4f852087850d1d..44a9f8ee8268a4cb946d7bc6adb7478e21a84f34 100644 (file)
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- */ 
+ */
 package org.wamblee.xml;
 
-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;
@@ -49,9 +31,28 @@ 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.
@@ -59,7 +60,8 @@ import javax.xml.validation.SchemaFactory;
  * @author Erik Brakkee
  */
 public final class DomUtils {
-    private static final Logger LOG = Logger.getLogger(DomUtils.class.getName());
+    private static final Logger LOG = Logger
+        .getLogger(DomUtils.class.getName());
 
     /**
      * Disabled default constructor.
@@ -95,15 +97,24 @@ public final class DomUtils {
      */
     public static Document read(InputStream aIs) throws XMLException {
         try {
-            DocumentBuilder builder = DocumentBuilderFactory.newInstance()
-                .newDocumentBuilder();
+            DOMImplementationRegistry registry = DOMImplementationRegistry
+                .newInstance();
 
-            return builder.parse(aIs);
-        } catch (SAXException e) {
+            DOMImplementationLS impl = (DOMImplementationLS) registry
+                .getDOMImplementation("LS");
+
+            LSParser builder = impl.createLSParser(
+                DOMImplementationLS.MODE_SYNCHRONOUS, null);
+            LSInput input = impl.createLSInput();
+            input.setByteStream(aIs);
+            return builder.parse(input);
+        } catch (IllegalAccessException e) {
             throw new XMLException(e.getMessage(), e);
-        } catch (IOException e) {
+        } catch (InstantiationException e) {
+            throw new XMLException(e.getMessage(), e);
+        } catch (ClassNotFoundException e) {
             throw new XMLException(e.getMessage(), e);
-        } catch (ParserConfigurationException e) {
+        } catch (LSException e) {
             throw new XMLException(e.getMessage(), e);
         } finally {
             try {
@@ -128,23 +139,18 @@ public final class DomUtils {
     public static Document readAndValidate(InputStream aIs, InputStream aSchema)
         throws XMLException {
         try {
+            Document doc = read(aIs);
             final Schema schema = SchemaFactory.newInstance(
                 XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(
                 new StreamSource(aSchema));
+            Validator validator = schema.newValidator();
+            validator.validate(new DOMSource(doc));
 
-            final DocumentBuilderFactory factory = DocumentBuilderFactory
-                .newInstance();
-            factory.setValidating(true);
-            factory.setNamespaceAware(true);
-            factory.setSchema(schema);
-
-            return factory.newDocumentBuilder().parse(aIs);
+            return doc;
         } 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();
@@ -171,8 +177,15 @@ public final class DomUtils {
      */
     public static void serialize(Document aDocument, OutputStream aOs)
         throws IOException {
-        XMLSerializer serializer = new XMLSerializer(aOs, new OutputFormat());
-        serializer.serialize(aDocument);
+        try {
+            TransformerFactory factory = TransformerFactory.newInstance();
+            Transformer identityTransform = factory.newTransformer();
+            DOMSource source = new DOMSource(aDocument);
+            StreamResult result = new StreamResult(aOs);
+            identityTransform.transform(source, result);
+        } catch (TransformerException e) {
+            throw new IOException(e.getMessage(), e);
+        }
     }
 
     /**
@@ -191,32 +204,6 @@ public final class DomUtils {
         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.