View Javadoc

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      public static void registerSelfInSimpleContext() {
16          // see http://jaxen.org/extensions.html
17          ((SimpleFunctionContext) XPathFunctionContext.getInstance()).registerFunction(null, "matches", new MatchesFunction());
18      }
19  
20      public Object call(Context context, List args) throws FunctionCallException {
21          if (args.isEmpty()) {
22              return Boolean.FALSE;
23          }
24          List attributes = (List) args.get(0);
25          Attribute attr = (Attribute) attributes.get(0);
26  
27          Pattern check = Pattern.compile((String) args.get(1));
28          Matcher matcher = check.matcher(attr.getValue());
29          if (matcher.find()) {
30              return context.getNodeSet();
31          }
32          return Boolean.FALSE;
33      }
34  }