KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > equalvalidator > EqualValidator


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

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

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

58     public static final String JavaDoc EQUAL_MESSAGE_ID = "org.apache.myfaces.Equal.INVALID";
59
60     public EqualValidator(){
61     }
62
63     //the foreign component_id on which the validation is based.
64
private String JavaDoc _for= null;
65
66     
67     //JSF-Field for StateHolder-IF
68
private boolean _transient = false;
69
70
71     // -------------------------------------------------------- ValidatorIF
72
public void validate(
73         FacesContext facesContext,
74         UIComponent uiComponent,
75         Object JavaDoc value)
76         throws ValidatorException {
77
78             if (facesContext == null) throw new NullPointerException JavaDoc("facesContext");
79             if (uiComponent == null) throw new NullPointerException JavaDoc("uiComponent");
80
81             if (value == null)
82             {
83                 return;
84         }
85
86         UIInput foreignComp = (UIInput) uiComponent.getParent().findComponent(_for);
87         if(foreignComp==null)
88             throw new FacesException("Unable to find component '" + _for + "' (calling findComponent on component '" + uiComponent.getId() + "')");
89
90         Object JavaDoc[] args = {value.toString(),(foreignComp.getValue()==null) ? foreignComp.getId():foreignComp.getValue().toString()};
91
92         if(foreignComp.getValue()==null || !foreignComp.getValue().toString().equals(value.toString()) )
93             throw new ValidatorException(MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,EQUAL_MESSAGE_ID, args));
94         
95     }
96     // -------------------------------------------------------- StateholderIF
97

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

116     /**
117      * @return the foreign component_id, on which a value should be validated
118      */

119     public String JavaDoc getFor() {
120         return _for;
121     }
122
123     /**
124      * @param string the foreign component_id, on which a value should be validated
125      */

126     public void setFor(String JavaDoc string) {
127         _for = string;
128     }
129
130 }
Popular Tags