KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > jaxen > MatchesFunction


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 JavaDoc;
10 import java.util.regex.Pattern JavaDoc;
11 import java.util.regex.Matcher JavaDoc;
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 JavaDoc call(Context context, List JavaDoc args) throws FunctionCallException {
21         if (args.isEmpty()) {
22             return Boolean.FALSE;
23         }
24         List JavaDoc attributes = (List JavaDoc) args.get(0);
25         Attribute attr = (Attribute) attributes.get(0);
26
27         Pattern JavaDoc check = Pattern.compile((String JavaDoc) args.get(1));
28         Matcher JavaDoc matcher = check.matcher(attr.getValue());
29         if (matcher.find()) {
30             return context.getNodeSet();
31         }
32         return Boolean.FALSE;
33     }
34 }
35
Popular Tags