2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shale.validator.validator;
19
20 import javax.faces.application.FacesMessage;
21 import javax.faces.component.StateHolder;
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.FacesContext;
24 import javax.faces.validator.ValidatorException;
25
26 /***
27 * <p><code>Validator</code> implementation that will perform both format
28 * and (optional) range checks on an integer value.</p>
29 */
30 public final class IntegerValidator extends AbstractValidator
31 implements StateHolder {
32
33
34
35
36
37 /***
38 * <p>The Apache Commons Validator instance we will be using.</p>
39 */
40 private static final
41 org.apache.commons.validator.routines.IntegerValidator INSTANCE =
42 org.apache.commons.validator.routines.IntegerValidator.getInstance();
43
44
45
46
47
48 private int maximum = Integer.MAX_VALUE;
49 private boolean maximumSet = false;
50
51
52 /***
53 * <p>Return the configured maximum value for this validator.</p>
54 */
55 public int getMaximum() {
56 return this.maximum;
57 }
58
59
60 /***
61 * <p>Set the configured maximum value for this validator.</p>
62 *
63 * @param maximum The new maximum value
64 */
65 public void setMaximum(int maximum) {
66 this.maximum = maximum;
67 this.maximumSet = true;
68 }
69
70
71 private int minimum = Integer.MIN_VALUE;
72 private boolean minimumSet = false;
73
74
75 /***
76 * <p>Return the configured minimum value for this validator.</p>
77 */
78 public int getMinimum() {
79 return this.minimum;
80 }
81
82
83 /***
84 * <p>Set the configured minimum value for this validator.</p>
85 *
86 * @param minimum The new minimum value
87 */
88 public void setMinimum(int minimum) {
89 this.minimum = minimum;
90 this.minimumSet = true;
91 }
92
93
94
95
96
97 /*** {@inheritDoc} */
98 public void validate(FacesContext context,
99 UIComponent component,
100 Object value) throws ValidatorException {
101
102
103 if ((context == null) || (component == null)) {
104 throw new NullPointerException(message(context, "common.null"));
105 }
106 if (value == null) {
107 return;
108 }
109
110
111
112
113 if (!(value instanceof Integer)) {
114 throw new ValidatorException
115 (new FacesMessage(FacesMessage.SEVERITY_ERROR,
116 message(context, "Integer.unconverted"),
117 null));
118 }
119
120
121 Integer ivalue = (Integer) value;
122 if (minimumSet) {
123 if (maximumSet) {
124 if (!INSTANCE.isInRange(ivalue, minimum, maximum)) {
125 throw new ValidatorException
126 (new FacesMessage(FacesMessage.SEVERITY_ERROR,
127 message(context, "Common.range",
128 new Object[] { ivalue,
129 new Integer(minimum),
130 new Integer(maximum) }),
131 null));
132 }
133 } else {
134 if (!INSTANCE.minValue(ivalue, minimum)) {
135 throw new ValidatorException
136 (new FacesMessage(FacesMessage.SEVERITY_ERROR,
137 message(context, "Common.minimum",
138 new Object[] { ivalue,
139 new Integer(minimum) }),
140 null));
141 }
142 }
143 } else if (maximumSet) {
144 if (!INSTANCE.maxValue(ivalue, maximum)) {
145 throw new ValidatorException
146 (new FacesMessage(FacesMessage.SEVERITY_ERROR,
147 message(context, "Common.maximum",
148 new Object[] { ivalue,
149 new Integer(maximum) }),
150 null));
151 }
152 }
153
154 }
155
156
157
158
159
160 /*** {@inheritDoc} */
161 public void restoreState(FacesContext context, Object state) {
162
163 Object[] values = (Object[]) state;
164 super.restoreState(context, values[0]);
165 maximum = ((Integer) values[1]).intValue();
166 maximumSet = ((Boolean) values[2]).booleanValue();
167 minimum = ((Integer) values[3]).intValue();
168 minimumSet = ((Boolean) values[4]).booleanValue();
169
170 }
171
172
173 /*** {@inheritDoc} */
174 public Object saveState(FacesContext context) {
175
176 Object[] values = new Object[5];
177 values[0] = super.saveState(context);
178 values[1] = new Integer(maximum);
179 values[2] = this.maximumSet ? Boolean.TRUE : Boolean.FALSE;
180 values[3] = new Integer(minimum);
181 values[4] = this.minimumSet ? Boolean.TRUE : Boolean.FALSE;
182 return values;
183
184 }
185
186
187
188
189
190 }