comparison artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java @ 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 8b72676698b5
children 48d1a9a082c2
comparison
equal deleted inserted replaced
66:89e3de0ee05f 67:ed03cc0e5800
1 package de.intevation.artifactdatabase; 1 package de.intevation.artifactdatabase;
2 2
3 import org.w3c.dom.Document; 3 import java.io.File;
4 import org.w3c.dom.Element; 4 import java.io.IOException;
5 import org.w3c.dom.Attr; 5 import java.io.OutputStream;
6
7 import org.xml.sax.SAXException;
8
9 import javax.xml.parsers.DocumentBuilderFactory;
10
11 import javax.xml.parsers.ParserConfigurationException;
12
13 import org.apache.log4j.Logger;
14
15 import javax.xml.xpath.XPathFactory;
16 import javax.xml.xpath.XPath;
17 import javax.xml.xpath.XPathExpressionException;
18 import javax.xml.xpath.XPathConstants;
19 6
20 import javax.xml.namespace.NamespaceContext; 7 import javax.xml.namespace.NamespaceContext;
21 import javax.xml.namespace.QName; 8 import javax.xml.namespace.QName;
22 9
23 import java.io.File; 10 import javax.xml.parsers.DocumentBuilderFactory;
24 import java.io.IOException; 11 import javax.xml.parsers.ParserConfigurationException;
12
13 import javax.xml.transform.Transformer;
14 import javax.xml.transform.TransformerConfigurationException;
15 import javax.xml.transform.TransformerException;
16 import javax.xml.transform.TransformerFactory;
17 import javax.xml.transform.TransformerFactoryConfigurationError;
18
19 import javax.xml.transform.dom.DOMSource;
20
21 import javax.xml.transform.stream.StreamResult;
22
23 import javax.xml.xpath.XPath;
24 import javax.xml.xpath.XPathConstants;
25 import javax.xml.xpath.XPathExpressionException;
26 import javax.xml.xpath.XPathFactory;
27
28 import org.apache.log4j.Logger;
29
30 import org.w3c.dom.Attr;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
33
34 import org.xml.sax.SAXException;
25 35
26 /** 36 /**
27 * @author Sascha L. Teichmann 37 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
28 */ 38 */
29 public final class XMLUtils 39 public final class XMLUtils
30 { 40 {
31 public static final String XFORM_URL = "http://www.w3.org/2002/xforms"; 41 public static final String XFORM_URL = "http://www.w3.org/2002/xforms";
32 public static final String XFORM_PREFIX = "xform"; 42 public static final String XFORM_PREFIX = "xform";
124 } 134 }
125 135
126 public static final String xpathString( 136 public static final String xpathString(
127 Object root, String query, NamespaceContext namespaceContext 137 Object root, String query, NamespaceContext namespaceContext
128 ) { 138 ) {
129 return (String)xpath(root, query, XPathConstants.STRING, namespaceContext); 139 return (String)xpath(
140 root, query, XPathConstants.STRING, namespaceContext);
130 } 141 }
131 142
132 public static final Object xpath( 143 public static final Object xpath(
133 Object root, 144 Object root,
134 String query, 145 String query,
149 logger.error(xpee.getLocalizedMessage(), xpee); 160 logger.error(xpee.getLocalizedMessage(), xpee);
150 } 161 }
151 162
152 return null; 163 return null;
153 } 164 }
165
166 public static boolean toStream(Document document, OutputStream out) {
167 try {
168 Transformer transformer =
169 TransformerFactory.newInstance().newTransformer();
170 DOMSource source = new DOMSource(document);
171 StreamResult result = new StreamResult(out);
172 transformer.transform(source, result);
173 return true;
174 }
175 catch (TransformerConfigurationException tce) {
176 logger.error(tce.getLocalizedMessage(), tce);
177 }
178 catch (TransformerFactoryConfigurationError tfce) {
179 logger.error(tfce.getLocalizedMessage(), tfce);
180 }
181 catch (TransformerException te) {
182 logger.error(te.getLocalizedMessage(), te);
183 }
184
185 return false;
186 }
154 } 187 }
155 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: 188 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org