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