comparison src/main/java/de/intevation/artifacts/httpclient/utils/XMLUtils.java @ 0:a1db30b33f43

Moved the experimental branch 'work-on-gwt' of the console-client to an own module 'http-client' and removed the console-client. http-client/trunk@1323 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 17 Feb 2011 10:31:40 +0000
parents
children 0359c4f3b4a1
comparison
equal deleted inserted replaced
-1:000000000000 0:a1db30b33f43
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8
9 package de.intevation.artifacts.httpclient.utils;
10
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.OutputStream;
14 import java.io.StringBufferInputStream;
15 import java.io.StringWriter;
16
17 import javax.xml.namespace.NamespaceContext;
18 import javax.xml.namespace.QName;
19 import javax.xml.parsers.DocumentBuilder;
20 import javax.xml.parsers.DocumentBuilderFactory;
21 import javax.xml.parsers.ParserConfigurationException;
22 import javax.xml.transform.Transformer;
23 import javax.xml.transform.TransformerConfigurationException;
24 import javax.xml.transform.TransformerException;
25 import javax.xml.transform.TransformerFactory;
26 import javax.xml.transform.TransformerFactoryConfigurationError;
27 import javax.xml.transform.dom.DOMSource;
28 import javax.xml.transform.stream.StreamResult;
29 import javax.xml.xpath.XPath;
30 import javax.xml.xpath.XPathConstants;
31 import javax.xml.xpath.XPathExpressionException;
32 import javax.xml.xpath.XPathFactory;
33
34 import org.apache.log4j.Logger;
35 import org.w3c.dom.Attr;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38 import org.w3c.dom.Node;
39 import org.w3c.dom.NodeList;
40 import org.xml.sax.SAXException;
41
42 /**
43 * This class provides many helper-Methods for handling different kinds of
44 * XML-stuff.
45 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
46 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
47 */
48 public class XMLUtils {
49
50 /**
51 * the logger, used to log exceptions and additonaly information
52 */
53 private static Logger logger = Logger.getLogger(XMLUtils.class);
54
55 /**
56 * Constructor
57 */
58 public XMLUtils() {
59 }
60
61 /**
62 * Class which could be used to create XML-Elements
63 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
64 *
65 */
66 public static class ElementCreator {
67
68 /**
69 * The document the elements should be placed in.
70 */
71 protected Document document;
72
73 /**
74 * The namespace that should be used.
75 */
76 protected String ns;
77
78 /**
79 * The prefix of the namespace that should be used.
80 */
81 protected String prefix;
82
83 /**
84 * Constructor
85 * @param document the document the elements should be placed in
86 * @param ns the namespace that should be used
87 * @param prefix the prefix of the namespace that should be used
88 */
89 public ElementCreator(Document document, String ns, String prefix) {
90 this.document = document;
91 this.ns = ns;
92 this.prefix = prefix;
93 }
94
95 /**
96 * Creates a new element using the given name.
97 * @param name the name of the new element.
98 * @return the new element
99 */
100 public Element create(String name) {
101 Element element = document.createElementNS(ns, name);
102 element.setPrefix(prefix);
103 return element;
104 }
105
106 /**
107 * Adds a new attribute to the given element.
108 * @param element the element where the attribute should be placed in.
109 * @param name the name of the attribute
110 * @param value the value of the attribute
111 */
112 public void addAttr(Element element, String name, String value) {
113 Attr attr = document.createAttributeNS(ns, name);
114 attr.setValue(value);
115 attr.setPrefix(prefix);
116 element.setAttributeNode(attr);
117 }
118 } // class ElementCreator
119
120 /**
121 * Creates a new document.
122 * @return the new document
123 */
124 public static Document newDocument() {
125 try {
126 return DocumentBuilderFactory.newInstance().newDocumentBuilder()
127 .newDocument();
128 } catch (ParserConfigurationException pce) {
129 logger.error(pce.getLocalizedMessage(), pce);
130 }
131 return null;
132 }
133
134 /**
135 * Creates a new <code>XPath</code>-expression
136 * @return the new <code>XPath</code>-expression
137 */
138 public static XPath newXPath() {
139 return newXPath(null);
140 }
141
142 /**
143 * Creates a new <code>XPath</code>-expression
144 * @param namespaceContext the namespacecontext that should be used.
145 * @return the new <code>XPath</code>-expression
146 */
147 public static XPath newXPath(NamespaceContext namespaceContext) {
148 XPathFactory factory = XPathFactory.newInstance();
149 XPath xpath = factory.newXPath();
150 if (namespaceContext != null) {
151 xpath.setNamespaceContext(namespaceContext);
152 }
153 return xpath;
154 }
155
156 /**
157 * Fetch the value of an element or attribute from the given resource
158 * using the query.
159 * @param root the source where the value should be fetch from
160 * @param query the query that should be used to fetch the value
161 * @param namespaceContext the namespacecontext that must match to
162 * fetch the value.
163 * @return the value
164 */
165 public static final String xpathString(Object root, String query,
166 NamespaceContext namespaceContext) {
167 return (String) xpath(root, query, XPathConstants.STRING,
168 namespaceContext);
169 }
170
171 /**
172 * Fetch the object rom the given resource using the query.
173 * @param root the source where the value should be fetch from
174 * @param query the query that should be used to fetch the object
175 * @param returnType the Type that must be used to return the object,
176 * @param namespaceContext the namespacecontext that must match to
177 * fetch the object.
178 * @return the value
179 */
180 public static final Object xpath(Object root, String query,
181 QName returnType,
182 NamespaceContext namespaceContext) {
183 if (root == null) {
184 return null;
185 }
186 try {
187 XPath xpath = XMLUtils.newXPath(namespaceContext);
188 if (xpath != null) {
189 return xpath.evaluate(query, root, returnType);
190 }
191 } catch (XPathExpressionException xpee) {
192 logger.error(xpee.getLocalizedMessage(), xpee);
193 }
194 return null;
195 }
196
197 /**
198 * Fetch the object rom the given resource using the query
199 * and the default <code>ArtifactNamespaceContext</code>
200 * @param root the source where the value should be fetch from
201 * @param query the query that should be used to fetch the object
202 * @param returnType the Type that must be used to return the object
203 * @return the value
204 */
205 public static Object getXPath(Object root, String query, QName returnType) {
206 return getXPath(root,query,returnType,ArtifactNamespaceContext.INSTANCE);
207 }
208
209 /**
210 * Fetch the object rom the given resource using the query
211 * and the default <code>ArtifactNamespaceContext</code>
212 * @param root the source where the value should be fetch from
213 * @param query the query that should be used to fetch the object
214 * @param returnType the Type that must be used to return the object.
215 * @param context the namespacecontext that must match to
216 * fetch the object.
217 * @return the value
218 */
219 public static Object getXPath(
220 Object root, String query, QName returnType, NamespaceContext context
221 ) {
222 return xpath(root, query, returnType, context);
223 }
224
225 /**
226 * Fetch a Nodeset value from a XML-Fragment or XML-Document using the
227 * given query.
228 * @param root the source where the String should be fetched from
229 * @param query the query that should be used,
230 * @return the Nodeset fetched from the source
231 */
232 public static NodeList getNodeSetXPath(Object root, String query) {
233 return (NodeList) getXPath(root, query, XPathConstants.NODESET);
234 }
235
236 /**
237 * Fetch a Node from a XML-Fragment or XML-Document using the
238 * given query.
239 * @param root the source where the Node should be fetched from
240 * @param query the query that should be used,
241 * @return the Node fetched from the source
242 */
243 public static Node getNodeXPath(Object root, String query) {
244 return (Node) getXPath(root, query, XPathConstants.NODE);
245 }
246
247 /**
248 * Fetch a String value from a XML-Fragment or XML-Document using the
249 * given query.
250 * @param root the source where the String should be fetched from
251 * @param xpath the query that should be used,
252 * @return the String fetched from the source
253 */
254 public static String getStringXPath(Object root, String xpath) {
255 return getStringXPath(root, xpath, null);
256 }
257
258 /**
259 * Fetch a String value from a XML-Fragment or XML-Document using the
260 * given query.
261 * @param root the source where the String should be fetched from
262 * @param query the query that should be used,
263 * @param def the default-value that will be returned id no value was found
264 * @return the String fetched from the source
265 */
266 public static String getStringXPath(Object root, String query, String def) {
267 String s = (String) getXPath(root, query, XPathConstants.STRING);
268 return s == null || s.length() == 0 ? def : s;
269 }
270
271 /**
272 * Reads an XML-document from a given <code>InputStream</code>
273 * @param inputStream the <code>InputStream</code> where the document
274 * should be read from
275 * @return the document that could be read.
276 */
277 public static Document readDocument(InputStream inputStream) {
278 Document returnValue = null;
279 try {
280 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
281 .newInstance();
282 docBuilderFactory.setNamespaceAware(true);
283 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
284 returnValue = docBuilder.parse(inputStream);
285 } catch (ParserConfigurationException e) {
286 logger.error(e, e);
287 } catch (SAXException e) {
288 logger.error(e, e);
289 } catch (IOException e) {
290 logger.error(e, e);
291 }
292 return returnValue;
293 }
294
295 /**
296 * Writes a given Document to an <code>OutputStream</code>
297 * @param document the document that should be written
298 * @param out the stream where the document should be written to,
299 * @return true if it was successful, false if not.
300 */
301 public static boolean toStream(Document document, OutputStream out) {
302 try {
303 Transformer transformer =
304 TransformerFactory.newInstance().newTransformer();
305 DOMSource source = new DOMSource(document);
306 StreamResult result = new StreamResult(out);
307 transformer.transform(source, result);
308 return true;
309 }
310 catch (TransformerConfigurationException tce) {
311 logger.error(tce.getLocalizedMessage(), tce);
312 }
313 catch (TransformerFactoryConfigurationError tfce) {
314 logger.error(tfce.getLocalizedMessage(), tfce);
315 }
316 catch (TransformerException te) {
317 logger.error(te.getLocalizedMessage(), te);
318 }
319 return false;
320 }
321 }
322 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org