KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > faces > validator > LengthValidator


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 javax.faces.validator;
17
18 import javax.faces.component.StateHolder;
19 import javax.faces.component.UIComponent;
20 import javax.faces.context.FacesContext;
21
22 /**
23  * @author Manfred Geiler (latest modification by $Author: mwessendorf $)
24  * @author Thomas Spiegl
25  * @version $Revision: 1.10 $ $Date: 2004/07/01 22:01:11 $
26  * $Log: LengthValidator.java,v $
27  * Revision 1.10 2004/07/01 22:01:11 mwessendorf
28  * ASF switch
29  *
30  * Revision 1.9 2004/06/14 12:55:22 manolito
31  * Added missing CVS Log comment
32  *
33  */

34 public class LengthValidator
35         implements Validator, StateHolder
36 {
37     // FIELDS
38
public static final String JavaDoc MAXIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MAXIMUM";
39     public static final String JavaDoc MINIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MINIMUM";
40     public static final String JavaDoc VALIDATOR_ID = "javax.faces.Length";
41
42     private Integer JavaDoc _minimum = null;
43     private Integer JavaDoc _maximum = null;
44     private boolean _transient = false;
45
46     // CONSTRUCTORS
47
public LengthValidator()
48     {
49     }
50
51     public LengthValidator(int maximum)
52     {
53         _maximum = new Integer JavaDoc(maximum);
54     }
55
56     public LengthValidator(int maximum,
57                            int minimum)
58     {
59         _maximum = new Integer JavaDoc(maximum);
60         _minimum = new Integer JavaDoc(minimum);
61     }
62
63     // VALIDATE
64
public void validate(FacesContext facesContext,
65                          UIComponent uiComponent,
66                          Object JavaDoc value)
67             throws ValidatorException
68     {
69         if (facesContext == null) throw new NullPointerException JavaDoc("facesContext");
70         if (uiComponent == null) throw new NullPointerException JavaDoc("uiComponent");
71
72         if (value == null)
73         {
74             return;
75         }
76
77         int length = value instanceof String JavaDoc ?
78             ((String JavaDoc)value).length() : value.toString().length();
79
80         if (_minimum != null)
81         {
82             if (length < _minimum.intValue())
83             {
84                 Object JavaDoc[] args = {_minimum,uiComponent.getId()};
85                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MINIMUM_MESSAGE_ID, args));
86             }
87         }
88         
89         if (_maximum != null)
90         {
91             if (length > _maximum.intValue())
92             {
93                 Object JavaDoc[] args = {_maximum,uiComponent.getId()};
94                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MAXIMUM_MESSAGE_ID, args));
95             }
96         }
97     }
98
99     // SETTER & GETTER
100
public int getMaximum()
101     {
102         return _maximum != null ? _maximum.intValue() : Integer.MAX_VALUE;
103     }
104
105     public void setMaximum(int maximum)
106     {
107         _maximum = new Integer JavaDoc(maximum);
108     }
109
110     public int getMinimum()
111     {
112         return _minimum != null ? _minimum.intValue() : 0;
113     }
114
115     public void setMinimum(int minimum)
116     {
117         _minimum = new Integer JavaDoc(minimum);
118     }
119
120     public boolean isTransient()
121     {
122         return _transient;
123     }
124
125     public void setTransient(boolean transientValue)
126     {
127         _transient = transientValue;
128     }
129
130     // RESTORE & SAVE STATE
131
public Object JavaDoc saveState(FacesContext context)
132     {
133         Object JavaDoc values[] = new Object JavaDoc[2];
134         values[0] = _maximum;
135         values[1] = _minimum;
136         return values;
137     }
138
139     public void restoreState(FacesContext context,
140                              Object JavaDoc state)
141     {
142         Object JavaDoc values[] = (Object JavaDoc[])state;
143         _maximum = (Integer JavaDoc)values[0];
144         _minimum = (Integer JavaDoc)values[1];
145     }
146
147     // MISC
148
public boolean equals(Object JavaDoc o)
149     {
150         if (this == o) return true;
151         if (!(o instanceof LengthValidator)) return false;
152
153         final LengthValidator lengthValidator = (LengthValidator)o;
154
155         if (_maximum != null ? !_maximum.equals(lengthValidator._maximum) : lengthValidator._maximum != null) return false;
156         if (_minimum != null ? !_minimum.equals(lengthValidator._minimum) : lengthValidator._minimum != null) return false;
157
158         return true;
159     }
160 }
161
Popular Tags