1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package javax.faces.validator;
20
21 import javax.faces.component.PartialStateHolder;
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.FacesContext;
24
25 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspProperty;
26 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
27 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFValidator;
28
29
30
31
32
33
34
35
36
37
38
39
40
41 @JSFValidator(
42 name="f:validateLength",
43 bodyContent="empty",
44 tagClass="org.apache.myfaces.taglib.core.ValidateLengthTag")
45 @JSFJspProperty(
46 name="binding",
47 returnType = "javax.faces.validator.LengthValidator",
48 longDesc = "A ValueExpression that evaluates to a LengthValidator.")
49 public class LengthValidator
50 implements Validator, PartialStateHolder
51 {
52
53 public static final String MAXIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MAXIMUM";
54 public static final String MINIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MINIMUM";
55 public static final String VALIDATOR_ID = "javax.faces.Length";
56
57 private Integer _minimum = null;
58 private Integer _maximum = null;
59 private boolean _transient = false;
60
61
62 public LengthValidator()
63 {
64 }
65
66 public LengthValidator(int maximum)
67 {
68 _maximum = Integer.valueOf(maximum);
69 }
70
71 public LengthValidator(int maximum,
72 int minimum)
73 {
74 _maximum = Integer.valueOf(maximum);
75 _minimum = Integer.valueOf(minimum);
76 }
77
78
79 public void validate(FacesContext facesContext,
80 UIComponent uiComponent,
81 Object value)
82 throws ValidatorException
83 {
84 if (facesContext == null)
85 {
86 throw new NullPointerException("facesContext");
87 }
88 if (uiComponent == null)
89 {
90 throw new NullPointerException("uiComponent");
91 }
92
93 if (value == null)
94 {
95 return;
96 }
97
98 int length = value instanceof String ?
99 ((String)value).length() : value.toString().length();
100
101 if (_minimum != null)
102 {
103 if (length < _minimum.intValue())
104 {
105 Object[] args = {_minimum,_MessageUtils.getLabel(facesContext, uiComponent)};
106 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MINIMUM_MESSAGE_ID, args));
107 }
108 }
109
110 if (_maximum != null)
111 {
112 if (length > _maximum.intValue())
113 {
114 Object[] args = {_maximum,_MessageUtils.getLabel(facesContext, uiComponent)};
115 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MAXIMUM_MESSAGE_ID, args));
116 }
117 }
118 }
119
120
121
122
123
124
125
126 @JSFProperty(deferredValueType="java.lang.Integer")
127 public int getMaximum()
128 {
129 return _maximum != null ? _maximum.intValue() : 0;
130 }
131
132 public void setMaximum(int maximum)
133 {
134 _maximum = Integer.valueOf(maximum);
135 clearInitialState();
136 }
137
138
139
140
141
142 @JSFProperty(deferredValueType="java.lang.Integer")
143 public int getMinimum()
144 {
145 return _minimum != null ? _minimum.intValue() : 0;
146 }
147
148 public void setMinimum(int minimum)
149 {
150 _minimum = Integer.valueOf(minimum);
151 clearInitialState();
152 }
153
154 public boolean isTransient()
155 {
156 return _transient;
157 }
158
159 public void setTransient(boolean transientValue)
160 {
161 _transient = transientValue;
162 }
163
164
165 public Object saveState(FacesContext context)
166 {
167 if (context == null)
168 {
169 throw new NullPointerException("context");
170 }
171
172 if (!initialStateMarked())
173 {
174 Object values[] = new Object[2];
175 values[0] = _maximum;
176 values[1] = _minimum;
177 return values;
178 }
179 return null;
180 }
181
182 public void restoreState(FacesContext context,
183 Object state)
184 {
185 if (context == null)
186 {
187 throw new NullPointerException("context");
188 }
189
190 if (state != null)
191 {
192 Object values[] = (Object[])state;
193 _maximum = (Integer)values[0];
194 _minimum = (Integer)values[1];
195 }
196 }
197
198
199 @Override
200 public boolean equals(Object o)
201 {
202 if (this == o)
203 {
204 return true;
205 }
206 if (!(o instanceof LengthValidator))
207 {
208 return false;
209 }
210
211 LengthValidator lengthValidator = (LengthValidator)o;
212
213 if (_maximum != null ? !_maximum.equals(lengthValidator._maximum) : lengthValidator._maximum != null)
214 {
215 return false;
216 }
217 if (_minimum != null ? !_minimum.equals(lengthValidator._minimum) : lengthValidator._minimum != null)
218 {
219 return false;
220 }
221
222 return true;
223 }
224
225 @Override
226 public int hashCode()
227 {
228 int result = _minimum != null ? _minimum.hashCode() : 0;
229 result = 31 * result + (_maximum != null ? _maximum.hashCode() : 0);
230 return result;
231 }
232
233 private boolean _initialStateMarked = false;
234
235 public void clearInitialState()
236 {
237 _initialStateMarked = false;
238 }
239
240 public boolean initialStateMarked()
241 {
242 return _initialStateMarked;
243 }
244
245 public void markInitialState()
246 {
247 _initialStateMarked = true;
248 }
249
250 @JSFProperty(faceletsOnly=true)
251 @SuppressWarnings("unused")
252 private Boolean isDisabled()
253 {
254 return null;
255 }
256
257 @JSFProperty(faceletsOnly=true)
258 @SuppressWarnings("unused")
259 private String getFor()
260 {
261 return null;
262 }
263 }