KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > regexprvalidator > RegExprValidator


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.custom.regexprvalidator;
17
18 import javax.faces.application.FacesMessage;
19 import javax.faces.component.StateHolder;
20 import javax.faces.component.UIComponent;
21 import javax.faces.context.FacesContext;
22 import javax.faces.validator.Validator;
23 import javax.faces.validator.ValidatorException;
24
25 import org.apache.myfaces.util.MessageUtils;
26
27 import org.apache.commons.validator.GenericValidator;
28
29 /**
30  * @author mwessendorf (latest modification by $Author: matzew $)
31  * @version $Revision: 1.5 $ $Date: 2004/11/30 09:37:43 $
32  * $Log: RegExprValidator.java,v $
33  * Revision 1.5 2004/11/30 09:37:43 matzew
34  * changes i18n-messages for validation
35  *
36  * Revision 1.4 2004/10/13 11:50:57 matze
37  * renamed packages to org.apache
38  *
39  * Revision 1.3 2004/07/01 21:53:11 mwessendorf
40  * ASF switch
41  *
42  * Revision 1.2 2004/06/27 22:06:27 mwessendorf
43  * Log
44  *
45  *
46  */

47 public class RegExprValidator implements Validator, StateHolder {
48     /**
49      * <p>The standard converter id for this converter.</p>
50      */

51     public static final String JavaDoc VALIDATOR_ID = "org.apache.myfaces.validator.RegExpr";
52
53     /**
54      * <p>The message identifier of the {@link FacesMessage} to be created if
55      * the creditcard check fails.</p>
56      */

57     public static final String JavaDoc REGEXPR_MESSAGE_ID = "org.apache.myfaces.Regexpr.INVALID";
58
59     public RegExprValidator(){
60     }
61
62     //the pattern on which the validation is based.
63
private String JavaDoc _pattern= null;
64
65     
66     //JSF-Field for StateHolder-IF
67
private boolean _transient = false;
68
69
70
71     public void validate(
72         FacesContext facesContext,
73         UIComponent uiComponent,
74         Object JavaDoc value)
75         throws ValidatorException {
76
77             if (facesContext == null) throw new NullPointerException JavaDoc("facesContext");
78             if (uiComponent == null) throw new NullPointerException JavaDoc("uiComponent");
79
80             if (value == null)
81             {
82                 return;
83         }
84         Object JavaDoc[] args = {value.toString()};
85         if(!GenericValidator.matchRegexp(value.toString(),"^"+_pattern+"$")){
86             throw new ValidatorException(MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,REGEXPR_MESSAGE_ID, args));
87         
88         }
89
90     }
91
92
93
94     // -------------------------------------------------------- StateholderIF
95

96     public Object JavaDoc saveState(FacesContext context) {
97         Object JavaDoc state = _pattern;
98         return state;
99     }
100
101     public void restoreState(FacesContext context, Object JavaDoc state) {
102         _pattern = (String JavaDoc) state;
103     }
104
105     public boolean isTransient() {
106         return _transient;
107     }
108
109     public void setTransient(boolean newTransientValue) {
110         _transient = newTransientValue;
111     }
112     // -------------------------------------------------------- GETTER & SETTER
113

114     /**
115      * @return the pattern, on which a value should be validated
116      */

117     public String JavaDoc getPattern() {
118         return _pattern;
119     }
120
121     /**
122      * @param string the pattern, on which a value should be validated
123      */

124     public void setPattern(String JavaDoc string) {
125         _pattern = string;
126     }
127
128 }
129
Popular Tags