|
1 |
| package net.sourceforge.pmd.jaxen; |
|
2 |
| |
|
3 |
| import org.apache.oro.text.perl.Perl5Util; |
|
4 |
| import org.jaxen.Context; |
|
5 |
| import org.jaxen.Function; |
|
6 |
| import org.jaxen.FunctionCallException; |
|
7 |
| import org.jaxen.SimpleFunctionContext; |
|
8 |
| import org.jaxen.XPathFunctionContext; |
|
9 |
| |
|
10 |
| import java.util.List; |
|
11 |
| |
|
12 |
| public class MatchesFunction implements Function { |
|
13 |
| |
|
14 |
126
| public static void registerSelfInSimpleContext() {
|
|
15 |
| |
|
16 |
126
| ((SimpleFunctionContext) XPathFunctionContext.getInstance()).registerFunction(null, "matches", new MatchesFunction());
|
|
17 |
| } |
|
18 |
| |
|
19 |
6
| public Object call(Context context, List args) throws FunctionCallException {
|
|
20 |
6
| if (args.isEmpty()) {
|
|
21 |
0
| return Boolean.FALSE;
|
|
22 |
| } |
|
23 |
6
| List attributes = (List) args.get(0);
|
|
24 |
6
| Attribute attr = (Attribute) attributes.get(0);
|
|
25 |
6
| String testString = attr.getValue();
|
|
26 |
6
| String regularExpression = '/' + (String) args.get(1) + '/';
|
|
27 |
| |
|
28 |
| |
|
29 |
6
| Perl5Util regexp = new Perl5Util();
|
|
30 |
6
| if (regexp.match(regularExpression, testString)) {
|
|
31 |
3
| return context.getNodeSet();
|
|
32 |
| } |
|
33 |
3
| return Boolean.FALSE;
|
|
34 |
| } |
|
35 |
| } |