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