# HG changeset patch # User Sascha L. Teichmann # Date 1382112933 -7200 # Node ID 420eb5a5fde41e1a42fe7957a633f4ae59770581 # Parent b7e82468299a2a5fdba001dbcf8bec7122eec566 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") diff -r b7e82468299a -r 420eb5a5fde4 artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java --- 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.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.emptyList(); + } + }); } /** @@ -410,5 +425,17 @@ } return Collections.emptySet(); } + + public Collection findAll(String needle, String haystack) { + + ArrayList result = new ArrayList(); + + 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 :