changeset 7382:420eb5a5fde4

Datacage: Add function dc:find-all(needle, haystack). Finds all matches for needle in haystack and results them als a list of strings. Example: dc:find-all("\d{4}", "1919 1291 1299 3029") returns ("1919", "1291, "1299", "3029")
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 18 Oct 2013 18:15:33 +0200
parents b7e82468299a
children 36a194156f15
files artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java
diffstat 1 files changed, 27 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java	Fri Oct 18 15:44:01 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java	Fri Oct 18 18:15:33 2013 +0200
@@ -9,6 +9,7 @@
 package org.dive4elements.river.artifacts.datacage.templating;
 
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
@@ -16,6 +17,8 @@
 import java.util.Map;
 import java.util.HashMap;
 import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
@@ -170,6 +173,18 @@
                     : Collections.<String>emptySet();
             }
         });
+
+        addFunction("find-all", 2, new XPathFunction() {
+            @Override
+            public Object evaluate(List args) throws XPathFunctionException {
+                Object needle    = args.get(0);
+                Object haystack  = args.get(1);
+                return haystack instanceof String
+                    && needle   instanceof String
+                    ? findAll((String)needle, (String)haystack)
+                    : Collections.<String>emptyList();
+            }
+        });
     }
 
     /**
@@ -410,5 +425,17 @@
         }
         return Collections.<String>emptySet();
     }
+
+    public Collection<String> findAll(String needle, String haystack) {
+
+        ArrayList<String> result = new ArrayList<String>();
+
+        Pattern pattern = Pattern.compile(needle);
+        Matcher matcher = pattern.matcher(haystack);
+        while (matcher.find()) {
+            result.add(matcher.group());
+        }
+        return result;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org