changeset 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 93a774fe2bb4
children ee6e1b2f137a
files ChangeLog artifacts-common/src/main/java/de/intevation/artifacts/common/utils/MapXPathVariableResolver.java artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java
diffstat 3 files changed, 85 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Tue Aug 23 10:54:38 2011 +0000
+++ b/ChangeLog	Wed Aug 24 07:44:11 2011 +0000
@@ -1,3 +1,14 @@
+2011-08-24  Ingo Weinzierl <ingo@intevation.de>
+
+	* artifacts-common/src/main/java/de/intevation/artifacts/common/utils/MapXPathVariableResolver.java:
+	  New. An XPathVariableResolver instance that stores its variables in a
+	  Map.
+
+	* artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java:
+	  Added support for variables in xpath expressions. There is a new xpath()
+	  method that takes a Map<String,String> that stores the required
+	  variables.
+
 2011-08-22	Sascha L. Teichmann	<teichmann@intevation.de>
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/state/Facet.java,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/MapXPathVariableResolver.java	Wed Aug 24 07:44:11 2011 +0000
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2011 by Intevation GmbH
+ *
+ * This program is free software under the LGPL (>=v2.1)
+ * Read the file LGPL.txt coming with the software for details
+ * or visit http://www.gnu.org/licenses/ if it does not exist.
+ */
+package de.intevation.artifacts.common.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPathVariableResolver;
+
+
+/**
+ *  @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public class MapXPathVariableResolver implements XPathVariableResolver {
+
+    protected Map<String, String> variables;
+
+
+    public MapXPathVariableResolver() {
+        this.variables = new HashMap<String, String>();
+    }
+
+
+    public MapXPathVariableResolver(Map<String, String> variables) {
+        this.variables = variables;
+    }
+
+
+    public void addVariable(String name, String value) {
+        if (name != null && value != null) {
+            variables.put(name, value);
+        }
+    }
+
+
+    @Override
+    public Object resolveVariable(QName variableName) {
+        String key = variableName.getLocalPart();
+        return variables.get(key);
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java	Tue Aug 23 10:54:38 2011 +0000
+++ b/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java	Wed Aug 24 07:44:11 2011 +0000
@@ -8,6 +8,7 @@
 
 package de.intevation.artifacts.common.utils;
 
+import java.util.Map;
 import java.util.zip.GZIPInputStream;
 import java.util.zip.GZIPOutputStream;
 
@@ -41,6 +42,7 @@
 import javax.xml.xpath.XPathConstants;
 import javax.xml.xpath.XPathExpressionException;
 import javax.xml.xpath.XPathFactory;
+import javax.xml.xpath.XPathVariableResolver;
 
 import org.apache.log4j.Logger;
 
@@ -222,7 +224,7 @@
      * @return the new XPath.
      */
     public static final XPath newXPath() {
-        return newXPath(null);
+        return newXPath(null, null);
     }
 
     /**
@@ -231,12 +233,19 @@
      * if none should be used.
      * @return The new XPath
      */
-    public static final XPath newXPath(NamespaceContext namespaceContext) {
+    public static final XPath newXPath(
+        NamespaceContext      namespaceContext,
+        XPathVariableResolver resolver)
+    {
         XPathFactory factory = XPathFactory.newInstance();
         XPath        xpath   = factory.newXPath();
         if (namespaceContext != null) {
             xpath.setNamespaceContext(namespaceContext);
         }
+
+        if (resolver != null) {
+            xpath.setXPathVariableResolver(resolver);
+        }
         return xpath;
     }
 
@@ -294,12 +303,26 @@
         QName            returnType,
         NamespaceContext namespaceContext
     ) {
+        return xpath(root, query, returnType, namespaceContext, null);
+    }
+
+    public static final Object xpath(
+        Object           root,
+        String           query,
+        QName            returnType,
+        NamespaceContext namespaceContext,
+        Map<String, String> variables)
+    {
         if (root == null) {
             return null;
         }
 
+        XPathVariableResolver resolver = variables != null
+            ? new MapXPathVariableResolver(variables)
+            : null;
+
         try {
-            XPath xpath = newXPath(namespaceContext);
+            XPath xpath = newXPath(namespaceContext, resolver);
             if (xpath != null) {
                 return xpath.evaluate(query, root, returnType);
             }

http://dive4elements.wald.intevation.org