changeset 959:a2b20ed3d3b4

Datacage: Added custom provider for XPath functions. flys-artifacts/trunk@2378 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 20 Jul 2011 16:28:49 +0000
parents ae198bef4ba0
children 92027887775a
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/App.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/FunctionResolver.java
diffstat 4 files changed, 124 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Wed Jul 20 15:06:16 2011 +0000
+++ b/flys-artifacts/ChangeLog	Wed Jul 20 16:28:49 2011 +0000
@@ -1,3 +1,18 @@
+2011-07-20  Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/services/meta/FunctionResolver.java:
+	  New. Custom XPath function provider. Provides
+	  'dc:contains(Object [] haystack, Object needle)' by now. Should be
+	  useful to check containments in 'out' lists later.
+
+	* src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java:
+	  Register the FunctionResolver to the evaluated XPaths.
+
+	* src/main/java/de/intevation/flys/artifacts/services/meta/App.java:
+	  Added code to parse
+	  "param:a,b,c" to "param" -> new String [] { "a", "b", "c" } to
+	  help testing the 'dc:contains' XPath function.
+
 2011-07-20  Ingo Weinzierl <ingo@intevation.de>
 
 	* doc/conf/conf.xml: Added wms configurations for Saar, Mosel and Elbe.
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/App.java	Wed Jul 20 15:06:16 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/App.java	Wed Jul 20 16:28:49 2011 +0000
@@ -41,12 +41,11 @@
         String [] parts = PARAMETERS.split("\\s*;\\s*");
         for (String part: parts) {
             String [] kv = part.split("\\s*:\\s*");
-            if (kv.length < 2) {
+            if (kv.length < 2 || (kv[0] = kv[0].trim()).length() == 0) {
                 continue;
             }
-            if ((kv[0] = kv[0].trim()).length() > 0) {
-                map.put(kv[0], kv[1]);
-            }
+            String [] values = kv[1].split("\\s*,\\s*");
+            map.put(kv[0], values.length == 1 ? values[0] : values);
         }
         return map;
     }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java	Wed Jul 20 15:06:16 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java	Wed Jul 20 16:28:49 2011 +0000
@@ -206,21 +206,12 @@
                         continue;
                     }
 
-                    try {
-                        XPath xpath = XPATH_FACTORY.newXPath();
-                        xpath.setXPathVariableResolver(frames);
-                        Object result = xpath.evaluate(
-                            test, EVAL_DOCUMENT, XPathConstants.BOOLEAN);
+                    Boolean result = evaluateXPath(test);
+                    if (result != null && result.booleanValue()) {
+                        branch = child;
+                        break;
+                    }
 
-                        if (result instanceof Boolean
-                        && ((Boolean)result).booleanValue()) {
-                            branch = child;
-                            break;
-                        }
-                    }
-                    catch (XPathExpressionException xfce) {
-                        log.error(xfce);
-                    }
                     continue;
                 }
                 else if ("otherwise".equals(name)) {
@@ -237,6 +228,24 @@
             }
         }
 
+        protected Boolean evaluateXPath(String expr) {
+            try {
+                XPath xpath = XPATH_FACTORY.newXPath();
+                xpath.setXPathVariableResolver(frames);
+                xpath.setXPathFunctionResolver(FunctionResolver.FUNCTIONS);
+                Object result = xpath.evaluate(
+                    expr, EVAL_DOCUMENT, XPathConstants.BOOLEAN);
+
+                return result instanceof Boolean
+                    ? (Boolean)result
+                    : null;
+            }
+            catch (XPathExpressionException xfce) {
+                log.error(xfce);
+            }
+            return null;
+        }
+
         protected void convert(Node parent, Element current) {
 
             String variable = expand(current.getAttribute("var"));
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/FunctionResolver.java	Wed Jul 20 16:28:49 2011 +0000
@@ -0,0 +1,83 @@
+package de.intevation.flys.artifacts.services.meta;
+
+import java.util.List;
+import java.util.ArrayList;
+
+import javax.xml.xpath.XPathFunctionResolver;
+import javax.xml.xpath.XPathFunction;
+import javax.xml.xpath.XPathFunctionException;
+
+import javax.xml.namespace.QName;
+
+public class FunctionResolver
+implements   XPathFunctionResolver
+{
+    public static final String FUNCTION_NAMESPACE_URI = "dc";
+
+    public static final class Entry {
+
+        String        name;
+        XPathFunction function;
+        int           arity;
+
+        public Entry() {
+        }
+
+        public Entry(String name, XPathFunction function, int arity) {
+            this.name     = name;
+            this.function = function;
+            this.arity    = arity;
+        }
+    } // class Entry
+
+    public static final FunctionResolver FUNCTIONS = new FunctionResolver();
+
+    static {
+        FUNCTIONS.addFunction("contains", 2, new XPathFunction() {
+            @Override
+            public Object evaluate(List args) throws XPathFunctionException {
+                try {
+                    Object [] haystack = (Object [])args.get(0);
+                    Object    needle   = args.get(1);
+                    for (Object straw: haystack) {
+                        if (straw.equals(needle)) {
+                            return Boolean.TRUE;
+                        }
+                    }
+                    return Boolean.FALSE;
+                }
+                catch (Exception e) {
+                    throw new XPathFunctionException(e);
+                }
+            }
+        });
+    }
+
+    protected List<Entry> functions;
+
+    public FunctionResolver() {
+        functions = new ArrayList<Entry>();
+    }
+
+    public void addFunction(String name, int arity, XPathFunction function) {
+        functions.add(new Entry(name, function, arity));
+    }
+
+    @Override
+    public XPathFunction resolveFunction(QName functionName, int arity) {
+
+        if (!functionName.getNamespaceURI().equals(FUNCTION_NAMESPACE_URI)) {
+            return null;
+        }
+
+        String name = functionName.getLocalPart();
+        for (Entry entry: functions) {
+            if (entry.arity == arity && entry.name.equals(name)) {
+                return entry.function;
+            }
+        }
+
+        return null;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org