KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > validator > PatternValidator


1 //$Id: PatternValidator.java,v 1.1 2005/05/27 08:58:54 epbernard Exp $
2
package org.hibernate.validator;
3
4 import java.util.regex.Matcher JavaDoc;
5
6 /**
7  * check if a given element match the regular expression
8  * @author Gavin King
9  */

10 public class PatternValidator implements Validator<Pattern> {
11     
12     private java.util.regex.Pattern JavaDoc pattern;
13
14     public void initialize(Pattern parameters) {
15         pattern = java.util.regex.Pattern.compile(
16                 parameters.regex(),
17                 parameters.flags()
18             );
19     }
20
21     public boolean isValid(Object JavaDoc value) {
22         if (value==null) return true;
23         if ( !(value instanceof String JavaDoc) ) return false;
24         String JavaDoc string = (String JavaDoc) value;
25         Matcher JavaDoc m = pattern.matcher(string);
26         return m.matches();
27     }
28
29 }
30
Popular Tags