Mercurial > dive4elements > framework
comparison artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java @ 102:4122dbc9711b
Added a new package artifacts-common for classes and utilities used in several packages.
artifacts/trunk@1285 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Wed, 02 Feb 2011 14:37:28 +0000 |
parents | |
children | 1282cf96d3eb |
comparison
equal
deleted
inserted
replaced
101:7fc0650f194c | 102:4122dbc9711b |
---|---|
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.artifactdatabase; | |
10 | |
11 import java.io.File; | |
12 import java.io.IOException; | |
13 import java.io.OutputStream; | |
14 | |
15 import javax.xml.namespace.NamespaceContext; | |
16 import javax.xml.namespace.QName; | |
17 | |
18 import javax.xml.parsers.DocumentBuilderFactory; | |
19 import javax.xml.parsers.ParserConfigurationException; | |
20 | |
21 import javax.xml.transform.Transformer; | |
22 import javax.xml.transform.TransformerConfigurationException; | |
23 import javax.xml.transform.TransformerException; | |
24 import javax.xml.transform.TransformerFactory; | |
25 import javax.xml.transform.TransformerFactoryConfigurationError; | |
26 | |
27 import javax.xml.transform.dom.DOMSource; | |
28 | |
29 import javax.xml.transform.stream.StreamResult; | |
30 | |
31 import javax.xml.xpath.XPath; | |
32 import javax.xml.xpath.XPathConstants; | |
33 import javax.xml.xpath.XPathExpressionException; | |
34 import javax.xml.xpath.XPathFactory; | |
35 | |
36 import org.apache.log4j.Logger; | |
37 | |
38 import org.w3c.dom.Attr; | |
39 import org.w3c.dom.Document; | |
40 import org.w3c.dom.Element; | |
41 | |
42 import org.xml.sax.SAXException; | |
43 | |
44 /** | |
45 * Some helper functions to ease work with XML concering namespaces, XPATH | |
46 * and so on. | |
47 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> | |
48 */ | |
49 public final class XMLUtils | |
50 { | |
51 /** | |
52 * W3C URL of XForms | |
53 */ | |
54 public static final String XFORM_URL = "http://www.w3.org/2002/xforms"; | |
55 /** | |
56 * W3C prefix of XForms | |
57 */ | |
58 public static final String XFORM_PREFIX = "xform"; | |
59 | |
60 private static Logger logger = Logger.getLogger(XMLUtils.class); | |
61 | |
62 private XMLUtils() { | |
63 } | |
64 | |
65 /** | |
66 * Helper class to generate elements and attributes with | |
67 * namespaces. | |
68 */ | |
69 public static class ElementCreator | |
70 { | |
71 /** | |
72 * owner document of the elements to be created | |
73 */ | |
74 protected Document document; | |
75 /** | |
76 * namespace to be used | |
77 */ | |
78 protected String ns; | |
79 /** | |
80 * prefix to be used | |
81 */ | |
82 protected String prefix; | |
83 | |
84 /** | |
85 * Constructor to create an element/attribute creator | |
86 * with a given namespace and namespace prefix using a | |
87 * given owner document. | |
88 * @param document The owning document | |
89 * @param ns The namespace | |
90 * @param prefix The namespace prefix | |
91 */ | |
92 public ElementCreator(Document document, String ns, String prefix) { | |
93 this.document = document; | |
94 this.ns = ns; | |
95 this.prefix = prefix; | |
96 } | |
97 | |
98 /** | |
99 * Creates a new element using the owning document with | |
100 * the this creators namespace and namespace prefix. | |
101 * @param name The name of the element | |
102 * @return The new element | |
103 */ | |
104 public Element create(String name) { | |
105 Element element = document.createElementNS(ns, name); | |
106 element.setPrefix(prefix); | |
107 return element; | |
108 } | |
109 | |
110 /** | |
111 * Adds a new attribute and its value to a given element. | |
112 * It does not set the namespace prefix. | |
113 * @param element The element to add the attribute to | |
114 * @param name The name of the attribute | |
115 * @param value The value of the attribute | |
116 */ | |
117 public void addAttr(Element element, String name, String value) { | |
118 addAttr(element, name, value, false); | |
119 } | |
120 | |
121 /** | |
122 * Adds a new attribute and its value to a given element. | |
123 * If the namespace prefix is used is decided by the 'addPrefix' flag. | |
124 * @param element The element to add the attribute to | |
125 * @param name The name of the attribute | |
126 * @param value The value of the attribute | |
127 * @param addPrefix If true the creators namespace prefix is | |
128 * set on the attribute. | |
129 */ | |
130 public void addAttr( | |
131 Element element, | |
132 String name, | |
133 String value, | |
134 boolean addPrefix | |
135 ) { | |
136 Attr attr = document.createAttributeNS(ns, name); | |
137 attr.setValue(value); | |
138 | |
139 if (addPrefix) | |
140 attr.setPrefix(prefix); | |
141 | |
142 element.setAttributeNode(attr); | |
143 } | |
144 } // class ElementCreator | |
145 | |
146 /** | |
147 * Creates a new XML document | |
148 * @return the new XML document ot null if something went wrong during | |
149 * creation. | |
150 */ | |
151 public static final Document newDocument() { | |
152 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
153 factory.setNamespaceAware(true); | |
154 | |
155 try { | |
156 return factory.newDocumentBuilder().newDocument(); | |
157 } | |
158 catch (ParserConfigurationException pce) { | |
159 logger.error(pce.getLocalizedMessage(), pce); | |
160 } | |
161 return null; | |
162 } | |
163 | |
164 /** | |
165 * Loads a XML document namespace aware from a file | |
166 * @param file The file to load. | |
167 * @return the XML document or null if something went wrong | |
168 * during loading. | |
169 */ | |
170 public static final Document parseDocument(File file) { | |
171 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
172 factory.setNamespaceAware(true); | |
173 | |
174 try { | |
175 return factory.newDocumentBuilder().parse(file); | |
176 } | |
177 catch (ParserConfigurationException pce) { | |
178 logger.error(pce.getLocalizedMessage(), pce); | |
179 } | |
180 catch (SAXException se) { | |
181 logger.error(se.getLocalizedMessage(), se); | |
182 } | |
183 catch (IOException ioe) { | |
184 logger.error(ioe.getLocalizedMessage(), ioe); | |
185 } | |
186 return null; | |
187 } | |
188 | |
189 /** | |
190 * Creates a new XPath without a namespace context. | |
191 * @return the new XPath. | |
192 */ | |
193 public static final XPath newXPath() { | |
194 return newXPath(null); | |
195 } | |
196 | |
197 /** | |
198 * Creates a new XPath with a given namespace context. | |
199 * @param namespaceContext The namespace context to be used or null | |
200 * if none should be used. | |
201 * @return The new XPath | |
202 */ | |
203 public static final XPath newXPath(NamespaceContext namespaceContext) { | |
204 XPathFactory factory = XPathFactory.newInstance(); | |
205 XPath xpath = factory.newXPath(); | |
206 if (namespaceContext != null) { | |
207 xpath.setNamespaceContext(namespaceContext); | |
208 } | |
209 return xpath; | |
210 } | |
211 | |
212 /** | |
213 * Evaluates an XPath query on a given object and returns the result | |
214 * as a given type. No namespace context is used. | |
215 * @param root The object which is used as the root of the tree to | |
216 * be searched in. | |
217 * @param query The XPath query | |
218 * @param returnTyp The type of the result. | |
219 * @return The result of type 'returnTyp' or null if something | |
220 * went wrong during XPath evaluation. | |
221 */ | |
222 public static final Object xpath( | |
223 Object root, | |
224 String query, | |
225 QName returnTyp | |
226 ) { | |
227 return xpath(root, query, returnTyp, null); | |
228 } | |
229 | |
230 /** | |
231 * Evaluates an XPath query on a given object and returns the result | |
232 * as a string. A given namespace context is used. | |
233 * @param root The object which is used as the root of the tree to | |
234 * be searched in. | |
235 * @param query The XPath query | |
236 * @param namespaceContext The namespace context to be used or null | |
237 * if none should be used. | |
238 * @return The result of the query or null if something went wrong | |
239 * during XPath evaluation. | |
240 */ | |
241 public static final String xpathString( | |
242 Object root, String query, NamespaceContext namespaceContext | |
243 ) { | |
244 return (String)xpath( | |
245 root, query, XPathConstants.STRING, namespaceContext); | |
246 } | |
247 | |
248 /** | |
249 * Evaluates an XPath query on a given object and returns the result | |
250 * as a given type. Optionally a namespace context is used. | |
251 * @param root The object which is used as the root of the tree to | |
252 * be searched in. | |
253 * @param query The XPath query | |
254 * @param returnType The type of the result. | |
255 * @param namespaceContext The namespace context to be used or null | |
256 * if none should be used. | |
257 * @return The result of type 'returnTyp' or null if something | |
258 * went wrong during XPath evaluation. | |
259 */ | |
260 public static final Object xpath( | |
261 Object root, | |
262 String query, | |
263 QName returnType, | |
264 NamespaceContext namespaceContext | |
265 ) { | |
266 if (root == null) { | |
267 return null; | |
268 } | |
269 | |
270 try { | |
271 XPath xpath = newXPath(namespaceContext); | |
272 if (xpath != null) { | |
273 return xpath.evaluate(query, root, returnType); | |
274 } | |
275 } | |
276 catch (XPathExpressionException xpee) { | |
277 logger.error(xpee.getLocalizedMessage(), xpee); | |
278 } | |
279 | |
280 return null; | |
281 } | |
282 | |
283 /** | |
284 * Streams out an XML document to a given output stream. | |
285 * @param document The document to be streamed out. | |
286 * @param out The output stream to be used. | |
287 * @return true if operation succeeded else false. | |
288 */ | |
289 public static boolean toStream(Document document, OutputStream out) { | |
290 try { | |
291 Transformer transformer = | |
292 TransformerFactory.newInstance().newTransformer(); | |
293 DOMSource source = new DOMSource(document); | |
294 StreamResult result = new StreamResult(out); | |
295 transformer.transform(source, result); | |
296 return true; | |
297 } | |
298 catch (TransformerConfigurationException tce) { | |
299 logger.error(tce.getLocalizedMessage(), tce); | |
300 } | |
301 catch (TransformerFactoryConfigurationError tfce) { | |
302 logger.error(tfce.getLocalizedMessage(), tfce); | |
303 } | |
304 catch (TransformerException te) { | |
305 logger.error(te.getLocalizedMessage(), te); | |
306 } | |
307 | |
308 return false; | |
309 } | |
310 } | |
311 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |