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.el.ValueExpression;
22
23 import javax.faces.application.FacesMessage;
24 import javax.faces.component.UIComponent;
25 import javax.faces.context.FacesContext;
26 import javax.faces.el.ValueBinding;
27
28 import javax.faces.validator.Validator;
29 import javax.faces.validator.ValidatorException;
30
31 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
32 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFValidator;
33 import org.apache.myfaces.trinidad.bean.FacesBean;
34 import org.apache.myfaces.trinidad.bean.PropertyKey;
35 import org.apache.myfaces.trinidad.util.ComponentUtils;
36 import org.apache.myfaces.trinidad.util.MessageFactory;
37
38
39
40
41
42 @JSFValidator(configExcluded=true)
43 public class LengthValidator extends javax.faces.validator.LengthValidator
44 {
45
46 public static final String VALIDATOR_ID = "org.apache.myfaces.trinidad.Length";
47
48
49
50
51
52
53
54
55
56 public static final String MAXIMUM_MESSAGE_ID =
57 "org.apache.myfaces.trinidad.validator.LengthValidator.MAXIMUM";
58
59
60
61
62
63
64
65
66 public static final String MINIMUM_MESSAGE_ID =
67 "org.apache.myfaces.trinidad.validator.LengthValidator.MINIMUM";
68
69
70
71
72
73
74
75
76
77
78
79 public static final String NOT_IN_RANGE_MESSAGE_ID =
80 "org.apache.myfaces.trinidad.validator.LengthValidator.NOT_IN_RANGE";
81
82
83
84
85
86
87
88
89
90
91
92 public static final String EXACT_MESSAGE_ID =
93 "org.apache.myfaces.trinidad.validator.LengthValidator.EXACT";
94
95
96
97
98
99 public LengthValidator()
100 {
101 super();
102 }
103
104
105
106
107
108
109
110 public LengthValidator(int maximum)
111 {
112 super(maximum);
113 }
114
115
116
117
118
119
120
121
122
123 public LengthValidator(int maximum, int minimum)
124 {
125 super(maximum, minimum);
126 }
127
128
129
130
131
132
133 @JSFProperty
134 @Override
135 public int getMaximum()
136 {
137 Object maxInt = _facesBean.getProperty(_MAXIMUM_KEY);
138 return ComponentUtils.resolveInteger(maxInt);
139 }
140
141
142
143
144
145
146
147 @Override
148 public void setMaximum(int maximum)
149 {
150 _facesBean.setProperty(_MAXIMUM_KEY, Integer.valueOf(maximum));
151 }
152
153
154
155
156
157
158
159 @JSFProperty
160 @Override
161 public int getMinimum()
162 {
163 Object minInt = _facesBean.getProperty(_MINIMUM_KEY);
164 return ComponentUtils.resolveInteger(minInt);
165 }
166
167
168
169
170
171
172
173 @Override
174 public void setMinimum(int minimum)
175 {
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 @JSFProperty
197 public String getMessageDetailMaximum()
198 {
199 Object maxMsgDet = _facesBean.getProperty(_MAXIMUM_MESSAGE_DETAIL_KEY);
200 return ComponentUtils.resolveString(maxMsgDet);
201 }
202
203
204
205
206
207
208
209
210 public void setMessageDetailMinimum(String minimumMessageDetail)
211 {
212 _facesBean.setProperty(_MINIMUM_MESSAGE_DETAIL_KEY, minimumMessageDetail);
213 }
214
215
216
217
218
219
220
221 @JSFProperty
222 public String getMessageDetailMinimum()
223 {
224 Object minMsgDet = _facesBean.getProperty(_MINIMUM_MESSAGE_DETAIL_KEY);
225 return ComponentUtils.resolveString(minMsgDet);
226 }
227
228
229
230
231
232
233
234
235 public void setMessageDetailNotInRange(String notInRangeMessageDetail)
236 {
237 _facesBean.setProperty(_NOT_IN_RANGE_MESSAGE_DETAIL_KEY, notInRangeMessageDetail);
238 }
239
240
241
242
243
244
245
246
247 @JSFProperty
248 public String getMessageDetailNotInRange()
249 {
250 Object notInRngMsg = _facesBean.getProperty(_NOT_IN_RANGE_MESSAGE_DETAIL_KEY);
251 return ComponentUtils.resolveString(notInRngMsg);
252 }
253
254
255
256
257
258
259
260
261
262
263 public void setMessageDetailExact(String exactMessageDetail)
264 {
265 _facesBean.setProperty(_EXACT_MESSAGE_DETAIL_KEY, exactMessageDetail);
266 }
267
268
269
270
271
272
273
274
275 @JSFProperty
276 public String getMessageDetailExact()
277 {
278 Object msg = _facesBean.getProperty(_EXACT_MESSAGE_DETAIL_KEY);
279 return ComponentUtils.resolveString(msg);
280 }
281
282
283
284
285
286
287
288 public void setHintMaximum(String hintMaximum)
289 {
290 _facesBean.setProperty(_HINT_MAXIMUM_KEY, hintMaximum);
291 }
292
293
294
295
296
297
298 @JSFProperty(tagExcluded=true)
299 public String getHintMaximum()
300 {
301 Object obj = _facesBean.getProperty(_HINT_MAXIMUM_KEY);
302 return ComponentUtils.resolveString(obj);
303 }
304
305
306
307
308
309
310 public void setHintMinimum(String hintMinimum)
311 {
312 _facesBean.setProperty(_HINT_MINIMUM_KEY, hintMinimum);
313 }
314
315
316
317
318
319
320 @JSFProperty(tagExcluded=true)
321 public String getHintMinimum()
322 {
323 Object obj = _facesBean.getProperty(_HINT_MINIMUM_KEY);
324 return ComponentUtils.resolveString(obj);
325 }
326
327
328
329
330
331
332 public void setHintNotInRange(String hintNotInRange)
333 {
334 _facesBean.setProperty(_HINT_NOT_IN_RANGE, hintNotInRange);
335 }
336
337
338
339
340
341
342 @JSFProperty(tagExcluded=true)
343 public String getHintNotInRange()
344 {
345 Object obj = _facesBean.getProperty(_HINT_NOT_IN_RANGE);
346 return ComponentUtils.resolveString(obj);
347 }
348
349
350
351
352
353
354
355 public void setHintExact(String hintExact)
356 {
357 _facesBean.setProperty(_HINT_EXACT, hintExact);
358 }
359
360
361
362
363
364
365 @JSFProperty(tagExcluded=true)
366 public String getHintExact()
367 {
368 Object obj = _facesBean.getProperty(_HINT_EXACT);
369 return ComponentUtils.resolveString(obj);
370 }
371
372 @Override
373 public void validate(
374 FacesContext context,
375 UIComponent component,
376 Object value
377 ) throws ValidatorException
378 {
379 if (isDisabled())
380 return;
381
382 if ((context == null) || (component == null))
383 {
384 throw new NullPointerException();
385 }
386
387 if(value != null)
388 {
389 String theValue = (value instanceof String)? (String)value : value.toString();
390 int max = getMaximum();
391 int min = getMinimum();
392
393
394
395
396
397 if (theValue.isEmpty())
398 return;
399
400 int length = theValue.length();
401
402
403 if(isMaximumSet() && isMinimumSet())
404 {
405 if(length<min || length>max)
406 {
407 throw new ValidatorException(
408 _getNotInRangeMessage(context, component, value, min, max));
409 }
410 }
411
412 if(isMinimumSet())
413 {
414 if (length < min)
415 {
416 throw new ValidatorException(
417 _getMinimumMessage(context, component, value, min));
418 }
419 }
420
421 if(isMaximumSet())
422 {
423 if (length > max)
424 {
425 throw new ValidatorException(
426 _getMaximumMessage(context, component, value, max));
427 }
428 }
429 }
430 }
431
432
433 @Override
434 public Object saveState(FacesContext context)
435 {
436 return _facesBean.saveState(context);
437 }
438
439
440 @Override
441 public void restoreState(FacesContext context, Object state)
442 {
443 _facesBean.restoreState(context, state);
444 }
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459 public void setValueExpression(String name, ValueExpression expression)
460 {
461 ValidatorUtils.setValueExpression(_facesBean, name, expression) ;
462 }
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477 public ValueExpression getValueExpression(String name)
478 {
479 return ValidatorUtils.getValueExpression(_facesBean, name);
480 }
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497 public void setValueBinding(String name, ValueBinding binding)
498 {
499 ValidatorUtils.setValueBinding(_facesBean, name, binding) ;
500 }
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515 public ValueBinding getValueBinding(String name)
516 {
517 return ValidatorUtils.getValueBinding(_facesBean, name);
518 }
519
520 @JSFProperty(istransient=true, tagExcluded=true)
521 @Override
522 public boolean isTransient()
523 {
524 return (_transientValue);
525 }
526
527
528 @Override
529 public void setTransient(boolean transientValue)
530 {
531 _transientValue = transientValue;
532 }
533
534 @Override
535 public boolean equals(Object otherObj)
536 {
537 if (!(otherObj instanceof LengthValidator))
538 {
539 return false;
540 }
541
542 LengthValidator other = (LengthValidator) otherObj;
543
544 return ((this.getMaximum() == other.getMaximum())
545 && (this.getMinimum() == other.getMinimum())
546 && (this.isMinimumSet() == other.isMinimumSet())
547 && (this.isMaximumSet() == other.isMaximumSet())
548 && (this.isDisabled() == other.isDisabled())
549 && (this.isTransient() == other.isTransient()));
550 }
551
552 @Override
553 public int hashCode()
554 {
555 int result = 17;
556 Object msgDetExact = getMessageDetailExact();
557 Object maxMsgDet = getMessageDetailMaximum();
558 Object minMsgDet = getMessageDetailMinimum();
559 Object notInRangeMsgDet = getMessageDetailNotInRange();
560
561 result = 37 * result + (isDisabled() ? 1 : 0);
562 result = 37 * result + (isTransient() ? 0 : 1);
563 result = 37 * result + (isMaximumSet() ? 0 : 1);
564 result = 37 * result + (isMinimumSet() ? 0 : 1);
565 result = 37 * result + Integer.valueOf(getMinimum()).hashCode();
566 result = 37 * result + Integer.valueOf(getMaximum()).hashCode();
567 result = 37 * result + ( msgDetExact == null ? 0 : msgDetExact.hashCode());
568 result = 37 * result + ( maxMsgDet == null ? 0 : maxMsgDet.hashCode());
569 result = 37 * result + ( minMsgDet == null ? 0 : minMsgDet.hashCode());
570 result = 37 * result + ( notInRangeMsgDet == null ? 0 : notInRangeMsgDet.hashCode());
571
572 return result;
573 }
574
575
576
577
578
579 public void setDisabled(boolean isDisabled)
580 {
581 _facesBean.setProperty(_DISABLED_KEY, Boolean.valueOf(isDisabled));
582 }
583
584
585
586
587
588 public boolean isDisabled()
589 {
590 Boolean disabled = (Boolean) _facesBean.getProperty(_DISABLED_KEY);
591
592 return (disabled != null) ? disabled.booleanValue() : false;
593 }
594
595 protected boolean isMaximumSet()
596 {
597 return _facesBean.getProperty(_MAXIMUM_KEY) != null;
598 }
599
600 protected boolean isMinimumSet()
601 {
602 return _facesBean.getProperty(_MINIMUM_KEY) != null;
603 }
604
605 private FacesMessage _getNotInRangeMessage(
606 FacesContext context,
607 UIComponent component,
608 Object value,
609 Object min,
610 Object max)
611 {
612 if (min.equals(max))
613 return _getExactMessage(context, component, value, min);
614
615 Object msg = _getRawNotInRangeMessageDetail();
616 Object label = ValidatorUtils.getComponentLabel(component);
617
618 Object[] params = {label, value, min, max};
619
620 return MessageFactory.getMessage(context, NOT_IN_RANGE_MESSAGE_ID,
621 msg, params, component);
622 }
623
624
625 private Object _getRawNotInRangeMessageDetail()
626 {
627 return _facesBean.getRawProperty(_NOT_IN_RANGE_MESSAGE_DETAIL_KEY);
628 }
629
630
631 private FacesMessage _getExactMessage(
632 FacesContext context,
633 UIComponent component,
634 Object value,
635 Object minMax)
636 {
637 Object msg = _getRawExactMessageDetail();
638 Object label = ValidatorUtils.getComponentLabel(component);
639
640 Object[] params = {label, value, minMax};
641
642 return MessageFactory.getMessage(context, EXACT_MESSAGE_ID,
643 msg, params, component);
644 }
645
646
647 private Object _getRawExactMessageDetail()
648 {
649 return _facesBean.getRawProperty(_EXACT_MESSAGE_DETAIL_KEY);
650 }
651
652
653 private FacesMessage _getMaximumMessage(
654 FacesContext context,
655 UIComponent component,
656 Object value,
657 Object max)
658 {
659
660 Object msg = _getRawMaximumMessageDetail();
661 Object label = ValidatorUtils.getComponentLabel(component);
662
663 Object[] params = {label, value, max};
664
665 return MessageFactory.getMessage(context,
666 MAXIMUM_MESSAGE_ID,
667 msg,
668 params,
669 component);
670 }
671
672 private Object _getRawMaximumMessageDetail()
673 {
674 return _facesBean.getRawProperty(_MAXIMUM_MESSAGE_DETAIL_KEY);
675 }
676
677 private FacesMessage _getMinimumMessage(
678 FacesContext context,
679 UIComponent component,
680 Object value,
681 Object min)
682 {
683 Object msg = _getRawMinimumMessageDetail();
684 Object label = ValidatorUtils.getComponentLabel(component);
685
686 Object[] params = {label, value, min};
687
688 return MessageFactory.getMessage(context, MINIMUM_MESSAGE_ID,
689 msg, params, component);
690 }
691
692 private Object _getRawMinimumMessageDetail()
693 {
694 return _facesBean.getRawProperty(_MINIMUM_MESSAGE_DETAIL_KEY);
695 }
696
697 private static final FacesBean.Type _TYPE = new FacesBean.Type();
698
699
700 private static final PropertyKey _DISABLED_KEY =
701 _TYPE.registerKey("disabled",
702 Boolean.class,
703 Boolean.FALSE);
704
705
706 private static final PropertyKey _MINIMUM_KEY =
707 _TYPE.registerKey("minimum",
708 Integer.class,
709
710 Integer.valueOf(0));
711
712
713 private static final PropertyKey _MAXIMUM_KEY =
714 _TYPE.registerKey("maximum", Integer.class,
715
716 Integer.valueOf(0));
717
718 private static final PropertyKey _MAXIMUM_MESSAGE_DETAIL_KEY =
719 _TYPE.registerKey("messageDetailMaximum", String.class);
720
721 private static final PropertyKey _MINIMUM_MESSAGE_DETAIL_KEY =
722 _TYPE.registerKey("messageDetailMinimum", String.class);
723
724 private static final PropertyKey _NOT_IN_RANGE_MESSAGE_DETAIL_KEY =
725 _TYPE.registerKey("messageDetailNotInRange", String.class);
726
727 private static final PropertyKey _EXACT_MESSAGE_DETAIL_KEY =
728 _TYPE.registerKey("messageDetailExact", String.class);
729
730 private static final PropertyKey _HINT_MAXIMUM_KEY =
731 _TYPE.registerKey("hintMaximum", String.class);
732
733 private static final PropertyKey _HINT_MINIMUM_KEY =
734 _TYPE.registerKey("hintMinimum", String.class);
735
736 private static final PropertyKey _HINT_NOT_IN_RANGE =
737 _TYPE.registerKey("hintNotInRange", String.class);
738
739 private static final PropertyKey _HINT_EXACT =
740 _TYPE.registerKey("hintExact", String.class);
741
742 private FacesBean _facesBean = ValidatorUtils.getFacesBean(_TYPE);
743
744 private boolean _transientValue = false;
745 }