changeset 7968:09c9920e6f24

Added dc:coalesce(a, b). Returns first element which is not empty or not zero.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 26 Jun 2014 12:54:31 +0200
parents 6d5cbd69511a
children 912cf4ec09d1 d66ea32d98bc
files artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java
diffstat 1 files changed, 19 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java	Thu Jun 26 12:44:40 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java	Thu Jun 26 12:54:31 2014 +0200
@@ -86,6 +86,13 @@
 
         functions = new HashMap<String, Entry>();
 
+        addFunction("coalesce", 2, new XPathFunction() {
+            @Override
+            public Object evaluate(List args) throws XPathFunctionException {
+                return coalesce(args);
+            }
+        });
+
         addFunction("lowercase", 1, new XPathFunction() {
             @Override
             public Object evaluate(List args) throws XPathFunctionException {
@@ -546,5 +553,17 @@
             ? (Number)list
             : Double.valueOf(-Double.MAX_VALUE);
     }
+
+    public static Object coalesce(List list) {
+        for (Object x: list) {
+            if (x instanceof String && ((String)x).length() != 0) {
+                return x;
+            }
+            if (x instanceof Number && ((Number)x).doubleValue() != 0.0) {
+                return x;
+            }
+        }
+        return StackFrames.NULL;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org