1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.trinidad.validator;
20
21 import javax.faces.application.FacesMessage;
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.FacesContext;
24 import javax.faces.el.ValueBinding;
25
26 import javax.faces.validator.Validator;
27 import javax.faces.validator.ValidatorException;
28
29 import org.apache.myfaces.trinidad.bean.FacesBean;
30 import org.apache.myfaces.trinidad.bean.PropertyKey;
31 import org.apache.myfaces.trinidad.util.ComponentUtils;
32 import org.apache.myfaces.trinidad.util.IntegerUtils;
33 import org.apache.myfaces.trinidad.util.MessageFactory;
34
35
36
37
38
39 public class LengthValidator extends javax.faces.validator.LengthValidator
40 {
41
42 public static final String VALIDATOR_ID = "org.apache.myfaces.trinidad.Length";
43
44
45
46
47
48
49
50
51
52 public static final String MAXIMUM_MESSAGE_ID =
53 "org.apache.myfaces.trinidad.validator.LengthValidator.MAXIMUM";
54
55
56
57
58
59
60
61
62 public static final String MINIMUM_MESSAGE_ID =
63 "org.apache.myfaces.trinidad.validator.LengthValidator.MINIMUM";
64
65
66
67
68
69
70
71
72
73
74
75 public static final String NOT_IN_RANGE_MESSAGE_ID =
76 "org.apache.myfaces.trinidad.validator.LengthValidator.NOT_IN_RANGE";
77
78
79
80
81
82
83
84
85
86
87
88 public static final String EXACT_MESSAGE_ID =
89 "org.apache.myfaces.trinidad.validator.LengthValidator.EXACT";
90
91
92
93
94
95 public LengthValidator()
96 {
97 super();
98 }
99
100
101
102
103
104
105
106 public LengthValidator(int maximum)
107 {
108 super(maximum);
109 }
110
111
112
113
114
115
116
117
118
119 public LengthValidator(int maximum, int minimum)
120 {
121 super(maximum, minimum);
122 }
123
124
125
126
127
128
129 @Override
130 public int getMaximum()
131 {
132 Object maxInt = _facesBean.getProperty(_MAXIMUM_KEY);
133 if (maxInt == null)
134 maxInt = _MAXIMUM_KEY.getDefault();
135 return ComponentUtils.resolveInteger(maxInt);
136 }
137
138
139
140
141
142
143
144 @Override
145 public void setMaximum(int maximum)
146 {
147 super.setMaximum(maximum);
148 _facesBean.setProperty(_MAXIMUM_KEY, Integer.valueOf(maximum));
149 }
150
151
152
153
154
155
156
157 @Override
158 public int getMinimum()
159 {
160 Object minInt = _facesBean.getProperty(_MINIMUM_KEY);
161 if (minInt == null)
162 minInt = _MINIMUM_KEY.getDefault();
163 return ComponentUtils.resolveInteger(minInt);
164 }
165
166
167
168
169
170
171
172 @Override
173 public void setMinimum(int minimum)
174 {
175 super.setMinimum(minimum);
176 _facesBean.setProperty(_MINIMUM_KEY, Integer.valueOf(minimum));
177 }
178
179
180
181
182
183
184
185 public void setMessageDetailMaximum(String maximumMessageDetail)
186 {
187 _facesBean.setProperty(_MAXIMUM_MESSAGE_DETAIL_KEY, maximumMessageDetail);
188 }
189
190
191
192
193
194
195
196 public String getMessageDetailMaximum()
197 {
198 Object maxMsgDet = _facesBean.getProperty(_MAXIMUM_MESSAGE_DETAIL_KEY);
199 return ComponentUtils.resolveString(maxMsgDet);
200 }
201
202
203
204
205
206
207
208
209 public void setMessageDetailMinimum(String minimumMessageDetail)
210 {
211 _facesBean.setProperty(_MINIMUM_MESSAGE_DETAIL_KEY, minimumMessageDetail);
212 }
213
214
215
216
217
218
219
220 public String getMessageDetailMinimum()
221 {
222 Object minMsgDet = _facesBean.getProperty(_MINIMUM_MESSAGE_DETAIL_KEY);
223 return ComponentUtils.resolveString(minMsgDet);
224 }
225
226
227
228
229
230
231
232
233 public void setMessageDetailNotInRange(String notInRangeMessageDetail)
234 {
235 _facesBean.setProperty(_NOT_IN_RANGE_MESSAGE_DETAIL_KEY, notInRangeMessageDetail);
236 }
237
238
239
240
241
242
243
244
245 public String getMessageDetailNotInRange()
246 {
247 Object notInRngMsg = _facesBean.getProperty(_NOT_IN_RANGE_MESSAGE_DETAIL_KEY);
248 return ComponentUtils.resolveString(notInRngMsg);
249 }
250
251
252
253
254
255
256
257
258
259
260 public void setMessageDetailExact(String exactMessageDetail)
261 {
262 _facesBean.setProperty(_EXACT_MESSAGE_DETAIL_KEY, exactMessageDetail);
263 }
264
265
266
267
268
269
270
271
272 public String getMessageDetailExact()
273 {
274 Object msg = _facesBean.getProperty(_EXACT_MESSAGE_DETAIL_KEY);
275 return ComponentUtils.resolveString(msg);
276 }
277
278
279
280
281
282
283
284 public void setHintMaximum(String hintMaximum)
285 {
286 _facesBean.setProperty(_HINT_MAXIMUM_KEY, hintMaximum);
287 }
288
289
290
291
292
293
294 public String getHintMaximum()
295 {
296 Object obj = _facesBean.getProperty(_HINT_MAXIMUM_KEY);
297 return ComponentUtils.resolveString(obj);
298 }
299
300
301
302
303
304
305 public void setHintMinimum(String hintMinimum)
306 {
307 _facesBean.setProperty(_HINT_MINIMUM_KEY, hintMinimum);
308 }
309
310
311
312
313
314
315 public String getHintMinimum()
316 {
317 Object obj = _facesBean.getProperty(_HINT_MINIMUM_KEY);
318 return ComponentUtils.resolveString(obj);
319 }
320
321
322
323
324
325
326 public void setHintNotInRange(String hintNotInRange)
327 {
328 _facesBean.setProperty(_HINT_NOT_IN_RANGE, hintNotInRange);
329 }
330
331
332
333
334
335
336 public String getHintNotInRange()
337 {
338 Object obj = _facesBean.getProperty(_HINT_NOT_IN_RANGE);
339 return ComponentUtils.resolveString(obj);
340 }
341
342
343
344
345
346
347
348 public void setHintExact(String hintExact)
349 {
350 _facesBean.setProperty(_HINT_EXACT, hintExact);
351 }
352
353
354
355
356
357
358 public String getHintExact()
359 {
360 Object obj = _facesBean.getProperty(_HINT_EXACT);
361 return ComponentUtils.resolveString(obj);
362 }
363
364 @Override
365 public void validate(
366 FacesContext context,
367 UIComponent component,
368 Object value
369 ) throws ValidatorException
370 {
371 try
372 {
373 super.validate(context, component, value);
374 }
375 catch (ValidatorException ve)
376 {
377
378
379
380 int min = getMinimum();
381 int max = getMaximum();
382
383
384
385 if (max != Integer.MAX_VALUE)
386 {
387 if (min > 0)
388 {
389 throw new ValidatorException(
390 _getNotInRangeMessage(context, component, value, min, max));
391 }
392 else
393 {
394 throw new ValidatorException(
395 _getMaximumMessage(context, component, value, max));
396 }
397 }
398 else
399 {
400 throw new ValidatorException(
401 _getMinimumMessage(context, component, value, min));
402 }
403 }
404 }
405
406
407 @Override
408 public Object saveState(FacesContext context)
409 {
410 return _facesBean.saveState(context);
411 }
412
413
414 @Override
415 public void restoreState(FacesContext context, Object state)
416 {
417 _facesBean.restoreState(context, state);
418 }
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433 public void setValueBinding(String name, ValueBinding binding)
434 {
435 ValidatorUtils.setValueBinding(_facesBean, name, binding) ;
436 }
437
438
439
440
441
442
443
444
445
446
447
448
449
450 public ValueBinding getValueBinding(String name)
451 {
452 return ValidatorUtils.getValueBinding(_facesBean, name);
453 }
454
455 @Override
456 public boolean isTransient()
457 {
458 return (_transientValue);
459 }
460
461
462 @Override
463 public void setTransient(boolean transientValue)
464 {
465 _transientValue = transientValue;
466 }
467
468
469 private FacesMessage _getNotInRangeMessage(
470 FacesContext context,
471 UIComponent component,
472 Object value,
473 Object min,
474 Object max)
475 {
476 if (min.equals(max))
477 return _getExactMessage(context, component, value, min);
478
479 Object msg = _getRawNotInRangeMessageDetail();
480 Object label = ValidatorUtils.getComponentLabel(component);
481
482 Object[] params = {label, value, min, max};
483
484 return MessageFactory.getMessage(context, NOT_IN_RANGE_MESSAGE_ID,
485 msg, params, component);
486 }
487
488
489 private Object _getRawNotInRangeMessageDetail()
490 {
491 return _facesBean.getRawProperty(_NOT_IN_RANGE_MESSAGE_DETAIL_KEY);
492 }
493
494
495 private FacesMessage _getExactMessage(
496 FacesContext context,
497 UIComponent component,
498 Object value,
499 Object minMax)
500 {
501 Object msg = _getRawExactMessageDetail();
502 Object label = ValidatorUtils.getComponentLabel(component);
503
504 Object[] params = {label, value, minMax};
505
506 return MessageFactory.getMessage(context, EXACT_MESSAGE_ID,
507 msg, params, component);
508 }
509
510
511 private Object _getRawExactMessageDetail()
512 {
513 return _facesBean.getRawProperty(_EXACT_MESSAGE_DETAIL_KEY);
514 }
515
516
517 private FacesMessage _getMaximumMessage(
518 FacesContext context,
519 UIComponent component,
520 Object value,
521 Object max)
522 {
523
524 Object msg = _getRawMaximumMessageDetail();
525 Object label = ValidatorUtils.getComponentLabel(component);
526
527 Object[] params = {label, value, max};
528
529 return MessageFactory.getMessage(context,
530 MAXIMUM_MESSAGE_ID,
531 msg,
532 params,
533 component);
534 }
535
536 private Object _getRawMaximumMessageDetail()
537 {
538 return _facesBean.getRawProperty(_MAXIMUM_MESSAGE_DETAIL_KEY);
539 }
540
541 private FacesMessage _getMinimumMessage(
542 FacesContext context,
543 UIComponent component,
544 Object value,
545 Object min)
546 {
547 Object msg = _getRawMinimumMessageDetail();
548 Object label = ValidatorUtils.getComponentLabel(component);
549
550 Object[] params = {label, value, min};
551
552 return MessageFactory.getMessage(context, MINIMUM_MESSAGE_ID,
553 msg, params, component);
554 }
555
556 private Object _getRawMinimumMessageDetail()
557 {
558 return _facesBean.getRawProperty(_MINIMUM_MESSAGE_DETAIL_KEY);
559 }
560
561 private static final FacesBean.Type _TYPE = new FacesBean.Type();
562
563
564 private static final PropertyKey _MINIMUM_KEY =
565 _TYPE.registerKey("minimum",
566 Integer.class,
567
568 Integer.valueOf(0));
569
570
571 private static final PropertyKey _MAXIMUM_KEY =
572 _TYPE.registerKey("maximum", Integer.class,
573
574 Integer.valueOf(Integer.MAX_VALUE));
575
576 private static final PropertyKey _MAXIMUM_MESSAGE_DETAIL_KEY =
577 _TYPE.registerKey("messageDetailMaximum", String.class);
578
579 private static final PropertyKey _MINIMUM_MESSAGE_DETAIL_KEY =
580 _TYPE.registerKey("messageDetailMinimum", String.class);
581
582 private static final PropertyKey _NOT_IN_RANGE_MESSAGE_DETAIL_KEY =
583 _TYPE.registerKey("messageDetailNotInRange", String.class);
584
585 private static final PropertyKey _EXACT_MESSAGE_DETAIL_KEY =
586 _TYPE.registerKey("messageDetailExact", String.class);
587
588 private static final PropertyKey _HINT_MAXIMUM_KEY =
589 _TYPE.registerKey("hintMaximum", String.class);
590
591 private static final PropertyKey _HINT_MINIMUM_KEY =
592 _TYPE.registerKey("hintMinimum", String.class);
593
594 private static final PropertyKey _HINT_NOT_IN_RANGE =
595 _TYPE.registerKey("hintNotInRange", String.class);
596
597 private static final PropertyKey _HINT_EXACT =
598 _TYPE.registerKey("hintExact", String.class);
599
600 private FacesBean _facesBean = ValidatorUtils.getFacesBean(_TYPE);
601
602 private boolean _transientValue = false;
603 }