comparison flys-aft/src/main/java/de/intevation/utils/XML.java @ 4070:d09adfa90942

Added XML/XPath support. flys-aft/trunk@3397 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 13 Dec 2011 10:01:44 +0000
parents
children 88f801888d85
comparison
equal deleted inserted replaced
4069:a4e79e8e0aa0 4070:d09adfa90942
1 package de.intevation.utils;
2
3 import java.io.FileInputStream;
4 import java.io.BufferedInputStream;
5 import java.io.File;
6 import java.io.IOException;
7 import java.io.InputStream;
8
9 import org.w3c.dom.Document;
10
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.parsers.ParserConfigurationException;
13
14 import org.xml.sax.SAXException;
15
16 import org.apache.log4j.Logger;
17
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import javax.xml.namespace.NamespaceContext;
22 import javax.xml.namespace.QName;
23
24 import javax.xml.xpath.XPath;
25 import javax.xml.xpath.XPathExpressionException;
26 import javax.xml.xpath.XPathFactory;
27 import javax.xml.xpath.XPathVariableResolver;
28
29 public final class XML
30 {
31 /** Logger for this class. */
32 private static Logger log = Logger.getLogger(XML.class);
33
34 public static class MapXPathVariableResolver
35 implements XPathVariableResolver
36 {
37 protected Map<String, String> variables;
38
39
40 public MapXPathVariableResolver() {
41 this.variables = new HashMap<String, String>();
42 }
43
44
45 public MapXPathVariableResolver(Map<String, String> variables) {
46 this.variables = variables;
47 }
48
49
50 public void addVariable(String name, String value) {
51 variables.put(name, value);
52 }
53
54
55 @Override
56 public Object resolveVariable(QName variableName) {
57 String key = variableName.getLocalPart();
58 return variables.get(key);
59 }
60 } // class MapXPathVariableResolver
61
62 private XML() {
63 }
64
65 /**
66 * Loads a XML document namespace aware from a file
67 * @param file The file to load.
68 * @return the XML document or null if something went wrong
69 * during loading.
70 */
71 public static final Document parseDocument(File file) {
72 InputStream inputStream = null;
73 try {
74 inputStream = new BufferedInputStream(new FileInputStream(file));
75 return parseDocument(inputStream);
76 }
77 catch (IOException ioe) {
78 log.error(ioe.getLocalizedMessage(), ioe);
79 }
80 finally {
81 if (inputStream != null) {
82 try { inputStream.close(); }
83 catch (IOException ioe) {}
84 }
85 }
86 return null;
87 }
88
89
90 public static final Document parseDocument(InputStream inputStream) {
91 return parseDocument(inputStream, Boolean.TRUE);
92 }
93
94 public static final Document parseDocument(
95 InputStream inputStream,
96 Boolean namespaceAware
97 ) {
98 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
99
100 if (namespaceAware != null) {
101 factory.setNamespaceAware(namespaceAware.booleanValue());
102 }
103
104 try {
105 return factory.newDocumentBuilder().parse(inputStream);
106 }
107 catch (ParserConfigurationException pce) {
108 log.error(pce.getLocalizedMessage(), pce);
109 }
110 catch (SAXException se) {
111 log.error(se.getLocalizedMessage(), se);
112 }
113 catch (IOException ioe) {
114 log.error(ioe.getLocalizedMessage(), ioe);
115 }
116 return null;
117 }
118
119
120 /**
121 * Creates a new XPath without a namespace context.
122 * @return the new XPath.
123 */
124 public static final XPath newXPath() {
125 return newXPath(null, null);
126 }
127
128 /**
129 * Creates a new XPath with a given namespace context.
130 * @param namespaceContext The namespace context to be used or null
131 * if none should be used.
132 * @return The new XPath
133 */
134 public static final XPath newXPath(
135 NamespaceContext namespaceContext,
136 XPathVariableResolver resolver)
137 {
138 XPathFactory factory = XPathFactory.newInstance();
139 XPath xpath = factory.newXPath();
140 if (namespaceContext != null) {
141 xpath.setNamespaceContext(namespaceContext);
142 }
143
144 if (resolver != null) {
145 xpath.setXPathVariableResolver(resolver);
146 }
147 return xpath;
148 }
149
150 /**
151 * Evaluates an XPath query on a given object and returns the result
152 * as a given type. No namespace context is used.
153 * @param root The object which is used as the root of the tree to
154 * be searched in.
155 * @param query The XPath query
156 * @param returnTyp The type of the result.
157 * @return The result of type 'returnTyp' or null if something
158 * went wrong during XPath evaluation.
159 */
160 public static final Object xpath(
161 Object root,
162 String query,
163 QName returnTyp
164 ) {
165 return xpath(root, query, returnTyp, null);
166 }
167
168 /**
169 * Evaluates an XPath query on a given object and returns the result
170 * as a given type. Optionally a namespace context is used.
171 * @param root The object which is used as the root of the tree to
172 * be searched in.
173 * @param query The XPath query
174 * @param returnType The type of the result.
175 * @param namespaceContext The namespace context to be used or null
176 * if none should be used.
177 * @return The result of type 'returnTyp' or null if something
178 * went wrong during XPath evaluation.
179 */
180 public static final Object xpath(
181 Object root,
182 String query,
183 QName returnType,
184 NamespaceContext namespaceContext
185 ) {
186 return xpath(root, query, returnType, namespaceContext, null);
187 }
188
189 public static final Object xpath(
190 Object root,
191 String query,
192 QName returnType,
193 NamespaceContext namespaceContext,
194 Map<String, String> variables)
195 {
196 if (root == null) {
197 return null;
198 }
199
200 XPathVariableResolver resolver = variables != null
201 ? new MapXPathVariableResolver(variables)
202 : null;
203
204 try {
205 XPath xpath = newXPath(namespaceContext, resolver);
206 if (xpath != null) {
207 return xpath.evaluate(query, root, returnType);
208 }
209 }
210 catch (XPathExpressionException xpee) {
211 log.error(xpee.getLocalizedMessage(), xpee);
212 }
213
214 return null;
215 }
216 }
217 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org