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