Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java @ 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 | 1dff8e71c4d6 |
children | 36a194156f15 |
comparison
equal
deleted
inserted
replaced
7381:b7e82468299a | 7382:420eb5a5fde4 |
---|---|
7 */ | 7 */ |
8 | 8 |
9 package org.dive4elements.river.artifacts.datacage.templating; | 9 package org.dive4elements.river.artifacts.datacage.templating; |
10 | 10 |
11 import java.text.SimpleDateFormat; | 11 import java.text.SimpleDateFormat; |
12 import java.util.ArrayList; | |
12 import java.util.Collection; | 13 import java.util.Collection; |
13 import java.util.Collections; | 14 import java.util.Collections; |
14 import java.util.Date; | 15 import java.util.Date; |
15 import java.util.List; | 16 import java.util.List; |
16 import java.util.Map; | 17 import java.util.Map; |
17 import java.util.HashMap; | 18 import java.util.HashMap; |
18 import java.util.Set; | 19 import java.util.Set; |
20 import java.util.regex.Matcher; | |
21 import java.util.regex.Pattern; | |
19 | 22 |
20 import java.lang.reflect.InvocationTargetException; | 23 import java.lang.reflect.InvocationTargetException; |
21 import java.lang.reflect.Method; | 24 import java.lang.reflect.Method; |
22 | 25 |
23 import javax.xml.namespace.QName; | 26 import javax.xml.namespace.QName; |
166 | 169 |
167 return artifactName instanceof String | 170 return artifactName instanceof String |
168 && stateId instanceof String | 171 && stateId instanceof String |
169 ? allStateSuccessors((String)artifactName, (String)stateId) | 172 ? allStateSuccessors((String)artifactName, (String)stateId) |
170 : Collections.<String>emptySet(); | 173 : Collections.<String>emptySet(); |
174 } | |
175 }); | |
176 | |
177 addFunction("find-all", 2, new XPathFunction() { | |
178 @Override | |
179 public Object evaluate(List args) throws XPathFunctionException { | |
180 Object needle = args.get(0); | |
181 Object haystack = args.get(1); | |
182 return haystack instanceof String | |
183 && needle instanceof String | |
184 ? findAll((String)needle, (String)haystack) | |
185 : Collections.<String>emptyList(); | |
171 } | 186 } |
172 }); | 187 }); |
173 } | 188 } |
174 | 189 |
175 /** | 190 /** |
408 TransitionEngine te = (TransitionEngine)o; | 423 TransitionEngine te = (TransitionEngine)o; |
409 return te.allRecursiveSuccessorStateIds(artifactName, stateId); | 424 return te.allRecursiveSuccessorStateIds(artifactName, stateId); |
410 } | 425 } |
411 return Collections.<String>emptySet(); | 426 return Collections.<String>emptySet(); |
412 } | 427 } |
428 | |
429 public Collection<String> findAll(String needle, String haystack) { | |
430 | |
431 ArrayList<String> result = new ArrayList<String>(); | |
432 | |
433 Pattern pattern = Pattern.compile(needle); | |
434 Matcher matcher = pattern.matcher(haystack); | |
435 while (matcher.find()) { | |
436 result.add(matcher.group()); | |
437 } | |
438 return result; | |
439 } | |
413 } | 440 } |
414 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 441 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |