changeset 67:ed03cc0e5800

Added method to XMLUtils to copy an XML document to an output stream. artifacts/trunk@553 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 17 Jan 2010 14:00:06 +0000
parents 89e3de0ee05f
children 541814404822
files ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java
diffstat 2 files changed, 58 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Jan 15 17:39:32 2010 +0000
+++ b/ChangeLog	Sun Jan 17 14:00:06 2010 +0000
@@ -1,3 +1,8 @@
+2010-01-177	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java:
+	  Added method 'toStream()' to copy a document into an output stream.
+
 2010-01-15  Ingo Weinzierl <ingo.weinzierl@intevation.de>
 
 	* artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java,
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java	Fri Jan 15 17:39:32 2010 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java	Sun Jan 17 14:00:06 2010 +0000
@@ -1,30 +1,40 @@
 package de.intevation.artifactdatabase;
 
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Attr;
-
-import org.xml.sax.SAXException;
-
-import javax.xml.parsers.DocumentBuilderFactory;
-
-import javax.xml.parsers.ParserConfigurationException;
-
-import org.apache.log4j.Logger;
-
-import javax.xml.xpath.XPathFactory;
-import javax.xml.xpath.XPath;
-import javax.xml.xpath.XPathExpressionException;
-import javax.xml.xpath.XPathConstants;
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
 
 import javax.xml.namespace.NamespaceContext;
 import javax.xml.namespace.QName;
 
-import java.io.File;
-import java.io.IOException;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerFactoryConfigurationError;
+
+import javax.xml.transform.dom.DOMSource;
+
+import javax.xml.transform.stream.StreamResult;
+
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
+import org.apache.log4j.Logger;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import org.xml.sax.SAXException;
 
 /**
- *  @author Sascha L. Teichmann
+ *  @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
  */
 public final class XMLUtils
 {
@@ -126,7 +136,8 @@
     public static final String xpathString(
         Object root, String query, NamespaceContext namespaceContext
     ) {
-        return (String)xpath(root, query, XPathConstants.STRING, namespaceContext);
+        return (String)xpath(
+            root, query, XPathConstants.STRING, namespaceContext);
     }
 
     public static final Object xpath(
@@ -151,5 +162,27 @@
 
         return null;
     }
+
+    public static boolean toStream(Document document, OutputStream out) {
+        try {
+            Transformer transformer =
+                TransformerFactory.newInstance().newTransformer();
+            DOMSource    source = new DOMSource(document);
+            StreamResult result = new StreamResult(out);
+            transformer.transform(source, result);
+            return true;
+        }
+        catch (TransformerConfigurationException tce) {
+            logger.error(tce.getLocalizedMessage(), tce);
+        }
+        catch (TransformerFactoryConfigurationError tfce) {
+            logger.error(tfce.getLocalizedMessage(), tfce);
+        }
+        catch (TransformerException te) {
+            logger.error(te.getLocalizedMessage(), te);
+        }
+
+        return false;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org