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.StateHolder; |
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:validateDoubleRange", |
46 | |
bodyContent="empty", |
47 | |
tagClass="org.apache.myfaces.taglib.core.ValidateDoubleRangeTag") |
48 | |
@JSFJspProperty( |
49 | |
name="binding", |
50 | |
returnType = "javax.faces.validator.DoubleRangeValidator", |
51 | |
longDesc = "A ValueExpression that evaluates to a DoubleRangeValidator.") |
52 | |
public class DoubleRangeValidator |
53 | |
implements Validator, StateHolder |
54 | |
{ |
55 | |
|
56 | |
public static final String VALIDATOR_ID = "javax.faces.DoubleRange"; |
57 | |
public static final String MAXIMUM_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.MAXIMUM"; |
58 | |
public static final String MINIMUM_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.MINIMUM"; |
59 | |
public static final String TYPE_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.TYPE"; |
60 | |
public static final String NOT_IN_RANGE_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.NOT_IN_RANGE"; |
61 | |
|
62 | 0 | private Double _minimum = null; |
63 | 0 | private Double _maximum = null; |
64 | 0 | private boolean _transient = false; |
65 | |
|
66 | |
|
67 | |
public DoubleRangeValidator() |
68 | 0 | { |
69 | 0 | } |
70 | |
|
71 | |
public DoubleRangeValidator(double maximum) |
72 | 0 | { |
73 | 0 | _maximum = new Double(maximum); |
74 | 0 | } |
75 | |
|
76 | |
public DoubleRangeValidator(double maximum, |
77 | |
double minimum) |
78 | 0 | { |
79 | 0 | _maximum = new Double(maximum); |
80 | 0 | _minimum = new Double(minimum); |
81 | 0 | } |
82 | |
|
83 | |
|
84 | |
public void validate(FacesContext facesContext, |
85 | |
UIComponent uiComponent, |
86 | |
Object value) |
87 | |
throws ValidatorException |
88 | |
{ |
89 | 0 | if (facesContext == null) throw new NullPointerException("facesContext"); |
90 | 0 | if (uiComponent == null) throw new NullPointerException("uiComponent"); |
91 | |
|
92 | 0 | if (value == null) |
93 | |
{ |
94 | 0 | return; |
95 | |
} |
96 | |
|
97 | 0 | double dvalue = parseDoubleValue(facesContext, uiComponent,value); |
98 | 0 | if (_minimum != null && _maximum != null) |
99 | |
{ |
100 | 0 | if (dvalue < _minimum.doubleValue() || |
101 | |
dvalue > _maximum.doubleValue()) |
102 | |
{ |
103 | 0 | Object[] args = {_minimum, _maximum,_MessageUtils.getLabel(facesContext, uiComponent)}; |
104 | 0 | throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, NOT_IN_RANGE_MESSAGE_ID, args)); |
105 | |
} |
106 | |
} |
107 | 0 | else if (_minimum != null) |
108 | |
{ |
109 | 0 | if (dvalue < _minimum.doubleValue()) |
110 | |
{ |
111 | 0 | Object[] args = {_minimum,_MessageUtils.getLabel(facesContext, uiComponent)}; |
112 | 0 | throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MINIMUM_MESSAGE_ID, args)); |
113 | |
} |
114 | |
} |
115 | 0 | else if (_maximum != null) |
116 | |
{ |
117 | 0 | if (dvalue > _maximum.doubleValue()) |
118 | |
{ |
119 | 0 | Object[] args = {_maximum,_MessageUtils.getLabel(facesContext, uiComponent)}; |
120 | 0 | throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MAXIMUM_MESSAGE_ID, args)); |
121 | |
} |
122 | |
} |
123 | 0 | } |
124 | |
|
125 | |
private double parseDoubleValue(FacesContext facesContext, UIComponent uiComponent, Object value) |
126 | |
throws ValidatorException |
127 | |
{ |
128 | 0 | if (value instanceof Number) |
129 | |
{ |
130 | 0 | return ((Number)value).doubleValue(); |
131 | |
} |
132 | |
|
133 | |
try |
134 | |
{ |
135 | 0 | return Double.parseDouble(value.toString()); |
136 | |
} |
137 | 0 | catch (NumberFormatException e) |
138 | |
{ |
139 | 0 | Object[] args = {_MessageUtils.getLabel(facesContext, uiComponent)}; |
140 | 0 | throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, TYPE_MESSAGE_ID, args)); |
141 | |
} |
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
@JSFProperty |
152 | |
public double getMaximum() |
153 | |
{ |
154 | 0 | return _maximum != null ? _maximum.doubleValue() : Double.MAX_VALUE; |
155 | |
} |
156 | |
|
157 | |
public void setMaximum(double maximum) |
158 | |
{ |
159 | 0 | _maximum = new Double(maximum); |
160 | 0 | } |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
@JSFProperty |
167 | |
public double getMinimum() |
168 | |
{ |
169 | 0 | return _minimum != null ? _minimum.doubleValue() : Double.MIN_VALUE; |
170 | |
} |
171 | |
|
172 | |
public void setMinimum(double minimum) |
173 | |
{ |
174 | 0 | _minimum = new Double(minimum); |
175 | 0 | } |
176 | |
|
177 | |
|
178 | |
|
179 | |
public Object saveState(FacesContext context) |
180 | |
{ |
181 | 0 | Object values[] = new Object[2]; |
182 | 0 | values[0] = _maximum; |
183 | 0 | values[1] = _minimum; |
184 | 0 | return values; |
185 | |
} |
186 | |
|
187 | |
public void restoreState(FacesContext context, |
188 | |
Object state) |
189 | |
{ |
190 | 0 | Object values[] = (Object[])state; |
191 | 0 | _maximum = (Double)values[0]; |
192 | 0 | _minimum = (Double)values[1]; |
193 | 0 | } |
194 | |
|
195 | |
public boolean isTransient() |
196 | |
{ |
197 | 0 | return _transient; |
198 | |
} |
199 | |
|
200 | |
public void setTransient(boolean transientValue) |
201 | |
{ |
202 | 0 | _transient = transientValue; |
203 | 0 | } |
204 | |
|
205 | |
|
206 | |
public boolean equals(Object o) |
207 | |
{ |
208 | 0 | if (this == o) return true; |
209 | 0 | if (!(o instanceof DoubleRangeValidator)) return false; |
210 | |
|
211 | 0 | final DoubleRangeValidator doubleRangeValidator = (DoubleRangeValidator)o; |
212 | |
|
213 | 0 | if (_maximum != null ? !_maximum.equals(doubleRangeValidator._maximum) : doubleRangeValidator._maximum != null) return false; |
214 | 0 | if (_minimum != null ? !_minimum.equals(doubleRangeValidator._minimum) : doubleRangeValidator._minimum != null) return false; |
215 | |
|
216 | 0 | return true; |
217 | |
} |
218 | |
|
219 | |
} |