001    package com.saelist.stx.xpath;
002    
003    import org.jaxen.Context;
004    import org.jaxen.Function;
005    import org.jaxen.FunctionCallException;
006    import org.jaxen.Navigator;
007    import org.jaxen.function.StringFunction;
008    import org.apache.oro.text.perl.Perl5Util;
009    
010    import java.util.List;
011    
012    /**
013     * <p><b>4.2</b> <code><i>boolean</i> matches(<i>string</i>,<i>string</i>)</code>
014     *
015     */
016    public class MatchesFunction implements Function {
017    
018      private static Perl5Util perl = new Perl5Util();
019    
020      public synchronized Object call(Context context, List args) throws FunctionCallException {
021        if (args.size() == 2)
022          return evaluate( args.get(0), args.get(1), context.getNavigator() );
023        throw new FunctionCallException( "matches() requires two arugments." );
024      }
025    
026      public static Boolean evaluate(Object textArg, Object patternArg, Navigator nav) {
027        String text   = StringFunction.evaluate(textArg, nav);
028        String pattern = StringFunction.evaluate(patternArg, nav);
029    
030        // String match = "m" + sep + "^" + str + "$" + sep + "i";
031    
032        return perl.match(pattern, text) ? Boolean.TRUE : Boolean.FALSE;
033      }
034    }