01: package net.sourceforge.pmd.jaxen;
02:
03: import org.jaxen.Context;
04: import org.jaxen.Function;
05: import org.jaxen.FunctionCallException;
06: import org.jaxen.SimpleFunctionContext;
07: import org.jaxen.XPathFunctionContext;
08:
09: import java.util.List;
10: import java.util.regex.Pattern;
11: import java.util.regex.Matcher;
12:
13: public class MatchesFunction implements Function {
14:
15: public static void registerSelfInSimpleContext() {
16: // see http://jaxen.org/extensions.html
17: ((SimpleFunctionContext) XPathFunctionContext.getInstance())
18: .registerFunction(null, "matches",
19: new MatchesFunction());
20: }
21:
22: public Object call(Context context, List args)
23: throws FunctionCallException {
24: if (args.isEmpty()) {
25: return Boolean.FALSE;
26: }
27: List attributes = (List) args.get(0);
28: Attribute attr = (Attribute) attributes.get(0);
29:
30: Pattern check = Pattern.compile((String) args.get(1));
31: Matcher matcher = check.matcher(attr.getValue());
32: if (matcher.find()) {
33: return context.getNodeSet();
34: }
35: return Boolean.FALSE;
36: }
37: }
|