(no commit message)
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sun, 15 Aug 2010 16:17:39 +0000 (16:17 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sun, 15 Aug 2010 16:17:39 +0000 (16:17 +0000)
support/general/src/main/java/org/wamblee/xml/DomUtils.java

index 0da97dd42913e02830f6a2500c82c8b76f899bf5..3d1c2d8ffae0cc0b38031627db6b7131becf5d7b 100644 (file)
  */
 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,10 +31,23 @@ 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 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;
+
 /**
  * Some basic XML utilities for common reoccuring tasks for DOM documents.
  * 
@@ -172,8 +167,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);
+        }
     }
 
     /**
@@ -192,32 +194,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.