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