Mercurial > dive4elements > river
comparison flys-aft/src/main/java/org/dive4elements/etl/utils/XML.java @ 5824:06643e440d1e
Moved directories to org.dive4elements.etl
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 11:35:06 +0200 |
parents | flys-aft/src/main/java/de/intevation/utils/XML.java@f939e1e6cfa4 |
children |
comparison
equal
deleted
inserted
replaced
5823:52e966cc7d35 | 5824:06643e440d1e |
---|---|
1 package de.intevation.utils; | |
2 | |
3 import java.io.BufferedInputStream; | |
4 import java.io.File; | |
5 import java.io.FileInputStream; | |
6 import java.io.IOException; | |
7 import java.io.InputStream; | |
8 import java.io.OutputStream; | |
9 import java.io.StringWriter; | |
10 | |
11 import java.util.HashMap; | |
12 import java.util.Map; | |
13 | |
14 import javax.xml.namespace.NamespaceContext; | |
15 import javax.xml.namespace.QName; | |
16 | |
17 import javax.xml.parsers.DocumentBuilderFactory; | |
18 import javax.xml.parsers.ParserConfigurationException; | |
19 | |
20 import javax.xml.transform.Transformer; | |
21 import javax.xml.transform.TransformerConfigurationException; | |
22 import javax.xml.transform.TransformerException; | |
23 import javax.xml.transform.TransformerFactory; | |
24 import javax.xml.transform.TransformerFactoryConfigurationError; | |
25 | |
26 import javax.xml.transform.dom.DOMResult; | |
27 import javax.xml.transform.dom.DOMSource; | |
28 | |
29 import javax.xml.transform.stream.StreamResult; | |
30 import javax.xml.transform.stream.StreamSource; | |
31 | |
32 import javax.xml.xpath.XPath; | |
33 import javax.xml.xpath.XPathExpressionException; | |
34 import javax.xml.xpath.XPathFactory; | |
35 import javax.xml.xpath.XPathVariableResolver; | |
36 | |
37 import org.apache.log4j.Logger; | |
38 | |
39 import org.w3c.dom.Document; | |
40 | |
41 import org.xml.sax.SAXException; | |
42 | |
43 public final class XML | |
44 { | |
45 /** Logger for this class. */ | |
46 private static Logger log = Logger.getLogger(XML.class); | |
47 | |
48 public static class MapXPathVariableResolver | |
49 implements XPathVariableResolver | |
50 { | |
51 protected Map<String, String> variables; | |
52 | |
53 | |
54 public MapXPathVariableResolver() { | |
55 this.variables = new HashMap<String, String>(); | |
56 } | |
57 | |
58 | |
59 public MapXPathVariableResolver(Map<String, String> variables) { | |
60 this.variables = variables; | |
61 } | |
62 | |
63 | |
64 public void addVariable(String name, String value) { | |
65 variables.put(name, value); | |
66 } | |
67 | |
68 | |
69 @Override | |
70 public Object resolveVariable(QName variableName) { | |
71 String key = variableName.getLocalPart(); | |
72 return variables.get(key); | |
73 } | |
74 } // class MapXPathVariableResolver | |
75 | |
76 private XML() { | |
77 } | |
78 | |
79 /** | |
80 * Creates a new XML document | |
81 * @return the new XML document ot null if something went wrong during | |
82 * creation. | |
83 */ | |
84 public static final Document newDocument() { | |
85 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
86 factory.setNamespaceAware(true); | |
87 | |
88 try { | |
89 return factory.newDocumentBuilder().newDocument(); | |
90 } | |
91 catch (ParserConfigurationException pce) { | |
92 log.error(pce.getLocalizedMessage(), pce); | |
93 } | |
94 return null; | |
95 } | |
96 | |
97 /** | |
98 * Loads a XML document namespace aware from a file | |
99 * @param file The file to load. | |
100 * @return the XML document or null if something went wrong | |
101 * during loading. | |
102 */ | |
103 public static final Document parseDocument(File file) { | |
104 return parseDocument(file, Boolean.TRUE); | |
105 } | |
106 | |
107 public static final Document parseDocument(File file, Boolean namespaceAware) { | |
108 InputStream inputStream = null; | |
109 try { | |
110 inputStream = new BufferedInputStream(new FileInputStream(file)); | |
111 return parseDocument(inputStream, namespaceAware); | |
112 } | |
113 catch (IOException ioe) { | |
114 log.error(ioe.getLocalizedMessage(), ioe); | |
115 } | |
116 finally { | |
117 if (inputStream != null) { | |
118 try { inputStream.close(); } | |
119 catch (IOException ioe) {} | |
120 } | |
121 } | |
122 return null; | |
123 } | |
124 | |
125 | |
126 public static final Document parseDocument(InputStream inputStream) { | |
127 return parseDocument(inputStream, Boolean.TRUE); | |
128 } | |
129 | |
130 public static final Document parseDocument( | |
131 InputStream inputStream, | |
132 Boolean namespaceAware | |
133 ) { | |
134 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
135 | |
136 if (namespaceAware != null) { | |
137 factory.setNamespaceAware(namespaceAware.booleanValue()); | |
138 } | |
139 | |
140 try { | |
141 return factory.newDocumentBuilder().parse(inputStream); | |
142 } | |
143 catch (ParserConfigurationException pce) { | |
144 log.error(pce.getLocalizedMessage(), pce); | |
145 } | |
146 catch (SAXException se) { | |
147 log.error(se.getLocalizedMessage(), se); | |
148 } | |
149 catch (IOException ioe) { | |
150 log.error(ioe.getLocalizedMessage(), ioe); | |
151 } | |
152 return null; | |
153 } | |
154 | |
155 | |
156 /** | |
157 * Creates a new XPath without a namespace context. | |
158 * @return the new XPath. | |
159 */ | |
160 public static final XPath newXPath() { | |
161 return newXPath(null, null); | |
162 } | |
163 | |
164 /** | |
165 * Creates a new XPath with a given namespace context. | |
166 * @param namespaceContext The namespace context to be used or null | |
167 * if none should be used. | |
168 * @return The new XPath | |
169 */ | |
170 public static final XPath newXPath( | |
171 NamespaceContext namespaceContext, | |
172 XPathVariableResolver resolver) | |
173 { | |
174 XPathFactory factory = XPathFactory.newInstance(); | |
175 XPath xpath = factory.newXPath(); | |
176 if (namespaceContext != null) { | |
177 xpath.setNamespaceContext(namespaceContext); | |
178 } | |
179 | |
180 if (resolver != null) { | |
181 xpath.setXPathVariableResolver(resolver); | |
182 } | |
183 return xpath; | |
184 } | |
185 | |
186 /** | |
187 * Evaluates an XPath query on a given object and returns the result | |
188 * as a given type. No namespace context is used. | |
189 * @param root The object which is used as the root of the tree to | |
190 * be searched in. | |
191 * @param query The XPath query | |
192 * @param returnTyp The type of the result. | |
193 * @return The result of type 'returnTyp' or null if something | |
194 * went wrong during XPath evaluation. | |
195 */ | |
196 public static final Object xpath( | |
197 Object root, | |
198 String query, | |
199 QName returnTyp | |
200 ) { | |
201 return xpath(root, query, returnTyp, null); | |
202 } | |
203 | |
204 /** | |
205 * Evaluates an XPath query on a given object and returns the result | |
206 * as a given type. Optionally a namespace context is used. | |
207 * @param root The object which is used as the root of the tree to | |
208 * be searched in. | |
209 * @param query The XPath query | |
210 * @param returnType The type of the result. | |
211 * @param namespaceContext The namespace context to be used or null | |
212 * if none should be used. | |
213 * @return The result of type 'returnTyp' or null if something | |
214 * went wrong during XPath evaluation. | |
215 */ | |
216 public static final Object xpath( | |
217 Object root, | |
218 String query, | |
219 QName returnType, | |
220 NamespaceContext namespaceContext | |
221 ) { | |
222 return xpath(root, query, returnType, namespaceContext, null); | |
223 } | |
224 | |
225 public static final Object xpath( | |
226 Object root, | |
227 String query, | |
228 QName returnType, | |
229 NamespaceContext namespaceContext, | |
230 Map<String, String> variables) | |
231 { | |
232 if (root == null) { | |
233 return null; | |
234 } | |
235 | |
236 XPathVariableResolver resolver = variables != null | |
237 ? new MapXPathVariableResolver(variables) | |
238 : null; | |
239 | |
240 try { | |
241 XPath xpath = newXPath(namespaceContext, resolver); | |
242 if (xpath != null) { | |
243 return xpath.evaluate(query, root, returnType); | |
244 } | |
245 } | |
246 catch (XPathExpressionException xpee) { | |
247 log.error(xpee.getLocalizedMessage(), xpee); | |
248 } | |
249 | |
250 return null; | |
251 } | |
252 | |
253 public static Document transform( | |
254 Document document, | |
255 File xformFile | |
256 ) { | |
257 try { | |
258 Transformer transformer = | |
259 TransformerFactory | |
260 .newInstance() | |
261 .newTransformer( | |
262 new StreamSource(xformFile)); | |
263 | |
264 DOMResult result = new DOMResult(); | |
265 | |
266 transformer.transform(new DOMSource(document), result); | |
267 | |
268 return (Document)result.getNode(); | |
269 } | |
270 catch (TransformerConfigurationException tce) { | |
271 log.error(tce, tce); | |
272 } | |
273 catch (TransformerException te) { | |
274 log.error(te, te); | |
275 } | |
276 | |
277 return null; | |
278 } | |
279 | |
280 /** | |
281 * Streams out an XML document to a given output stream. | |
282 * @param document The document to be streamed out. | |
283 * @param out The output stream to be used. | |
284 * @return true if operation succeeded else false. | |
285 */ | |
286 public static boolean toStream(Document document, OutputStream out) { | |
287 try { | |
288 Transformer transformer = | |
289 TransformerFactory.newInstance().newTransformer(); | |
290 DOMSource source = new DOMSource(document); | |
291 StreamResult result = new StreamResult(out); | |
292 transformer.transform(source, result); | |
293 return true; | |
294 } | |
295 catch (TransformerConfigurationException tce) { | |
296 log.error(tce.getLocalizedMessage(), tce); | |
297 } | |
298 catch (TransformerFactoryConfigurationError tfce) { | |
299 log.error(tfce.getLocalizedMessage(), tfce); | |
300 } | |
301 catch (TransformerException te) { | |
302 log.error(te.getLocalizedMessage(), te); | |
303 } | |
304 | |
305 return false; | |
306 } | |
307 | |
308 public static String toString(Document document) { | |
309 try { | |
310 Transformer transformer = | |
311 TransformerFactory.newInstance().newTransformer(); | |
312 DOMSource source = new DOMSource(document); | |
313 StringWriter out = new StringWriter(); | |
314 StreamResult result = new StreamResult(out); | |
315 transformer.transform(source, result); | |
316 out.flush(); | |
317 return out.toString(); | |
318 } | |
319 catch (TransformerConfigurationException tce) { | |
320 log.error(tce.getLocalizedMessage(), tce); | |
321 } | |
322 catch (TransformerFactoryConfigurationError tfce) { | |
323 log.error(tfce.getLocalizedMessage(), tfce); | |
324 } | |
325 catch (TransformerException te) { | |
326 log.error(te.getLocalizedMessage(), te); | |
327 } | |
328 | |
329 return null; | |
330 } | |
331 } | |
332 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |