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: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, StateHolder
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 = new Long(maximum);
74 }
75
76 public LongRangeValidator(long maximum,
77 long minimum)
78 {
79 _maximum = new Long(maximum);
80 _minimum = new Long(minimum);
81 }
82
83
84 public void validate(FacesContext facesContext,
85 UIComponent uiComponent,
86 Object value)
87 throws ValidatorException
88 {
89 if (facesContext == null) throw new NullPointerException("facesContext");
90 if (uiComponent == null) throw new NullPointerException("uiComponent");
91
92 if (value == null)
93 {
94 return;
95 }
96
97 double dvalue = parseLongValue(facesContext, uiComponent,value);
98 if (_minimum != null && _maximum != null)
99 {
100 if (dvalue < _minimum.longValue() ||
101 dvalue > _maximum.longValue())
102 {
103 Object[] args = {_minimum, _maximum,_MessageUtils.getLabel(facesContext, uiComponent)};
104 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, NOT_IN_RANGE_MESSAGE_ID, args));
105 }
106 }
107 else if (_minimum != null)
108 {
109 if (dvalue < _minimum.longValue())
110 {
111 Object[] args = {_minimum,_MessageUtils.getLabel(facesContext, uiComponent)};
112 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MINIMUM_MESSAGE_ID, args));
113 }
114 }
115 else if (_maximum != null)
116 {
117 if (dvalue > _maximum.longValue())
118 {
119 Object[] args = {_maximum,_MessageUtils.getLabel(facesContext, uiComponent)};
120 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MAXIMUM_MESSAGE_ID, args));
121 }
122 }
123 }
124
125 private long parseLongValue(FacesContext facesContext, UIComponent uiComponent, Object value)
126 throws ValidatorException
127 {
128 if (value instanceof Number)
129 {
130 return ((Number)value).longValue();
131 }
132
133 try
134 {
135 return Long.parseLong(value.toString());
136 }
137 catch (NumberFormatException e)
138 {
139 Object[] args = {_MessageUtils.getLabel(facesContext, uiComponent)};
140 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, TYPE_MESSAGE_ID, args));
141 }
142
143 }
144
145
146
147
148
149
150
151
152 @JSFProperty
153 public long getMaximum()
154 {
155 return _maximum != null ? _maximum.longValue() : Long.MAX_VALUE;
156 }
157
158 public void setMaximum(long maximum)
159 {
160 _maximum = new Long(maximum);
161 }
162
163
164
165
166
167 @JSFProperty
168 public long getMinimum()
169 {
170 return _minimum != null ? _minimum.longValue() : Long.MIN_VALUE;
171 }
172
173 public void setMinimum(long minimum)
174 {
175 _minimum = new Long(minimum);
176 }
177
178 public boolean isTransient()
179 {
180 return _transient;
181 }
182
183 public void setTransient(boolean transientValue)
184 {
185 _transient = transientValue;
186 }
187
188
189 public Object saveState(FacesContext context)
190 {
191 Object values[] = new Object[2];
192 values[0] = _maximum;
193 values[1] = _minimum;
194 return values;
195 }
196
197 public void restoreState(FacesContext context,
198 Object state)
199 {
200 Object values[] = (Object[])state;
201 _maximum = (Long)values[0];
202 _minimum = (Long)values[1];
203 }
204
205
206 public boolean equals(Object o)
207 {
208 if (this == o) return true;
209 if (!(o instanceof LongRangeValidator)) return false;
210
211 final LongRangeValidator longRangeValidator = (LongRangeValidator)o;
212
213 if (_maximum != null ? !_maximum.equals(longRangeValidator._maximum) : longRangeValidator._maximum != null) return false;
214 if (_minimum != null ? !_minimum.equals(longRangeValidator._minimum) : longRangeValidator._minimum != null) return false;
215
216 return true;
217 }
218
219 }