1 |
| package net.sourceforge.pmd.jaxen; |
2 |
| |
3 |
| import org.jaxen.Context; |
4 |
| import org.jaxen.Function; |
5 |
| import org.jaxen.FunctionCallException; |
6 |
| import org.jaxen.SimpleFunctionContext; |
7 |
| import org.jaxen.XPathFunctionContext; |
8 |
| |
9 |
| 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 |
117
| public static void registerSelfInSimpleContext() {
|
16 |
| |
17 |
117
| ((SimpleFunctionContext) XPathFunctionContext.getInstance()).registerFunction(null, "matches", new MatchesFunction());
|
18 |
| } |
19 |
| |
20 |
6
| public Object call(Context context, List args) throws FunctionCallException {
|
21 |
6
| if (args.isEmpty()) {
|
22 |
0
| return Boolean.FALSE;
|
23 |
| } |
24 |
6
| List attributes = (List) args.get(0);
|
25 |
6
| Attribute attr = (Attribute) attributes.get(0);
|
26 |
| |
27 |
6
| Pattern check = Pattern.compile((String) args.get(1));
|
28 |
6
| Matcher matcher = check.matcher(attr.getValue());
|
29 |
6
| if (matcher.find()) {
|
30 |
3
| return context.getNodeSet();
|
31 |
| } |
32 |
3
| return Boolean.FALSE;
|
33 |
| } |
34 |
| } |