comparison artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java @ 323:1d3607ceb9fa

Improved the XMLUtils to support xpath expressions that contain variables. artifacts/trunk@2540 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 24 Aug 2011 07:44:11 +0000
parents 715bdf990739
children e7d3f74854fb
comparison
equal deleted inserted replaced
322:93a774fe2bb4 323:1d3607ceb9fa
6 * or visit http://www.gnu.org/licenses/ if it does not exist. 6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */ 7 */
8 8
9 package de.intevation.artifacts.common.utils; 9 package de.intevation.artifacts.common.utils;
10 10
11 import java.util.Map;
11 import java.util.zip.GZIPInputStream; 12 import java.util.zip.GZIPInputStream;
12 import java.util.zip.GZIPOutputStream; 13 import java.util.zip.GZIPOutputStream;
13 14
14 import java.io.ByteArrayInputStream; 15 import java.io.ByteArrayInputStream;
15 import java.io.FileInputStream; 16 import java.io.FileInputStream;
39 40
40 import javax.xml.xpath.XPath; 41 import javax.xml.xpath.XPath;
41 import javax.xml.xpath.XPathConstants; 42 import javax.xml.xpath.XPathConstants;
42 import javax.xml.xpath.XPathExpressionException; 43 import javax.xml.xpath.XPathExpressionException;
43 import javax.xml.xpath.XPathFactory; 44 import javax.xml.xpath.XPathFactory;
45 import javax.xml.xpath.XPathVariableResolver;
44 46
45 import org.apache.log4j.Logger; 47 import org.apache.log4j.Logger;
46 48
47 import org.w3c.dom.Attr; 49 import org.w3c.dom.Attr;
48 import org.w3c.dom.Document; 50 import org.w3c.dom.Document;
220 /** 222 /**
221 * Creates a new XPath without a namespace context. 223 * Creates a new XPath without a namespace context.
222 * @return the new XPath. 224 * @return the new XPath.
223 */ 225 */
224 public static final XPath newXPath() { 226 public static final XPath newXPath() {
225 return newXPath(null); 227 return newXPath(null, null);
226 } 228 }
227 229
228 /** 230 /**
229 * Creates a new XPath with a given namespace context. 231 * Creates a new XPath with a given namespace context.
230 * @param namespaceContext The namespace context to be used or null 232 * @param namespaceContext The namespace context to be used or null
231 * if none should be used. 233 * if none should be used.
232 * @return The new XPath 234 * @return The new XPath
233 */ 235 */
234 public static final XPath newXPath(NamespaceContext namespaceContext) { 236 public static final XPath newXPath(
237 NamespaceContext namespaceContext,
238 XPathVariableResolver resolver)
239 {
235 XPathFactory factory = XPathFactory.newInstance(); 240 XPathFactory factory = XPathFactory.newInstance();
236 XPath xpath = factory.newXPath(); 241 XPath xpath = factory.newXPath();
237 if (namespaceContext != null) { 242 if (namespaceContext != null) {
238 xpath.setNamespaceContext(namespaceContext); 243 xpath.setNamespaceContext(namespaceContext);
244 }
245
246 if (resolver != null) {
247 xpath.setXPathVariableResolver(resolver);
239 } 248 }
240 return xpath; 249 return xpath;
241 } 250 }
242 251
243 /** 252 /**
292 Object root, 301 Object root,
293 String query, 302 String query,
294 QName returnType, 303 QName returnType,
295 NamespaceContext namespaceContext 304 NamespaceContext namespaceContext
296 ) { 305 ) {
306 return xpath(root, query, returnType, namespaceContext, null);
307 }
308
309 public static final Object xpath(
310 Object root,
311 String query,
312 QName returnType,
313 NamespaceContext namespaceContext,
314 Map<String, String> variables)
315 {
297 if (root == null) { 316 if (root == null) {
298 return null; 317 return null;
299 } 318 }
300 319
301 try { 320 XPathVariableResolver resolver = variables != null
302 XPath xpath = newXPath(namespaceContext); 321 ? new MapXPathVariableResolver(variables)
322 : null;
323
324 try {
325 XPath xpath = newXPath(namespaceContext, resolver);
303 if (xpath != null) { 326 if (xpath != null) {
304 return xpath.evaluate(query, root, returnType); 327 return xpath.evaluate(query, root, returnType);
305 } 328 }
306 } 329 }
307 catch (XPathExpressionException xpee) { 330 catch (XPathExpressionException xpee) {

http://dive4elements.wald.intevation.org