KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > validator > rules > RegEx


1 package org.sapia.validator.rules;
2
3 import java.util.regex.Matcher JavaDoc;
4 import java.util.regex.Pattern JavaDoc;
5 import org.sapia.validator.BeanRule;
6
7 /**
8  * A rule that validates a given Perl5 pattern.
9  *
10  * @author Yanick Duchesne
11  * <dl>
12  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
13  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
14  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
15  * </dl>
16  */

17 public class RegEx extends BeanRule{
18   
19   private Pattern JavaDoc _patternObj;
20
21   /**
22    * Constructor for RegExp.
23    */

24   public RegEx() {
25     super.throwExceptionOnNull(true);
26   }
27   
28   /**
29    * Sets the Perl5 pattern to use for validation.
30    *
31    * @param pattern a Perl5 pattern.
32    */

33   public void setPattern(String JavaDoc pattern){
34       _patternObj = Pattern.compile(pattern);
35   }
36   
37   /**
38    * @see org.sapia.validator.BeanRule#doValidate(Object)
39    */

40   protected boolean doValidate(Object JavaDoc toValidate) {
41     if(_patternObj == null){
42       throw new IllegalStateException JavaDoc("'pattern' attribute not defined on regExp rule at " + qualifiedName());
43     }
44     Matcher JavaDoc matcher = _patternObj.matcher(toValidate.toString());
45     return matcher.matches();
46   }
47
48
49 }
50
Popular Tags