View Javadoc

    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      public static void registerSelfInSimpleContext() {
    15          // see http://jaxen.org/extensions.html
    16          ((SimpleFunctionContext) XPathFunctionContext.getInstance()).registerFunction(null, "matches", new MatchesFunction());
    17      }
    18
    19      public Object call(Context context, List args) throws FunctionCallException {
    20          if (args.isEmpty()) {
    21              return Boolean.FALSE;
    22          }
    23          List attributes = (List) args.get(0);
    24          Attribute attr = (Attribute) attributes.get(0);
    25          String testString = attr.getValue();
    26          String regularExpression = '/' + (String) args.get(1) + '/';
    27
    28          // see http://jakarta.apache.org/oro/api/org/apache/oro/text/regex/package-summary.html#package_description
    29          Perl5Util regexp = new Perl5Util();
    30          if (regexp.match(regularExpression, testString)) {
    31              return context.getNodeSet();
    32          }
    33          return Boolean.FALSE;
    34      }
    35  }