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