1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 package org.exolab.castor.xml.validators;
48
49 import org.exolab.castor.types.DateTime;
50 import org.exolab.castor.types.DateTimeBase;
51
52 import org.exolab.castor.xml.ValidationContext;
53 import org.exolab.castor.xml.ValidationException;
54 import org.exolab.castor.xml.TypeValidator;
55
56
57
58
59
60
61
62 public class DateTimeValidator extends PatternValidator implements TypeValidator {
63
64 private DateTimeBase _maxInclusive;
65
66 private DateTimeBase _maxExclusive;
67
68 private DateTimeBase _minInclusive;
69
70 private DateTimeBase _minExclusive;
71
72 private DateTimeBase _fixed;
73
74
75
76
77 public DateTimeValidator() {
78 super();
79 }
80
81
82
83
84 public void clearFixed() {
85 _fixed = null;
86 }
87
88
89
90
91 public void clearMax() {
92 _maxInclusive = null;
93 _maxExclusive = null;
94 }
95
96
97
98
99 public void clearMin() {
100 _minInclusive = null;
101 _minExclusive = null;
102 }
103
104
105
106
107
108
109
110 public DateTimeBase getFixed() {
111 return _fixed;
112 }
113
114
115
116
117
118
119
120 public DateTimeBase getMaxInclusive() {
121 return _maxInclusive;
122 }
123
124
125
126
127
128
129
130 public DateTimeBase getMaxExclusive() {
131 return _maxExclusive;
132 }
133
134
135
136
137
138
139
140 public DateTimeBase getMinInclusive() {
141 return _minInclusive;
142 }
143
144
145
146
147
148
149
150 public DateTimeBase getMinExclusive() {
151 return _minExclusive;
152 }
153
154
155
156
157
158
159 public boolean hasFixed() {
160 return _fixed != null;
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174 public void setFixed(final DateTimeBase fixedValue) {
175 _fixed = fixedValue;
176 }
177
178
179
180
181
182
183
184
185 public void setMinExclusive(final DateTimeBase minValue) {
186 _minExclusive = minValue;
187 _minInclusive = null;
188 }
189
190
191
192
193
194
195
196
197 public void setMinInclusive(final DateTimeBase minValue) {
198 _minExclusive = null;
199 _minInclusive = minValue;
200 }
201
202
203
204
205
206
207
208
209 public void setMaxExclusive(final DateTimeBase maxValue) {
210 _maxExclusive = maxValue;
211 _maxInclusive = null;
212 }
213
214
215
216
217
218
219
220
221 public void setMaxInclusive(final DateTimeBase maxValue) {
222 _maxExclusive = null;
223 _maxInclusive = maxValue;
224 }
225
226
227
228
229
230
231 public void validate(final DateTimeBase dateTime) throws ValidationException {
232 validate(dateTime, (ValidationContext) null);
233 }
234
235
236
237
238
239
240
241 public void validate(final DateTimeBase dateTime, final ValidationContext context)
242 throws ValidationException {
243 boolean isThereMinInclusive = (_minInclusive != null);
244 boolean isThereMinExclusive = (_minExclusive != null);
245 boolean isThereMaxInclusive = (_maxInclusive != null);
246 boolean isThereMaxExclusive = (_maxExclusive != null);
247
248 if (isThereMinExclusive && isThereMinInclusive) {
249 throw new ValidationException("Both minInclusive and minExclusive are defined");
250 }
251
252 if (isThereMaxExclusive && isThereMaxInclusive) {
253 throw new ValidationException("Both maxInclusive and maxExclusive are defined");
254 }
255
256 if (_fixed != null) {
257 int comparison = dateTime.compareTo(_fixed);
258 if (comparison == DateTimeBase.INDETERMINATE) {
259 String err = dateTime.getClass().getName() + " " + dateTime
260 + " comparison to the fixed value " + _fixed
261 + " is indeterminate";
262 throw new ValidationException(err);
263 } else if (comparison != DateTimeBase.EQUALS) {
264 String err = dateTime.getClass().getName() + " " + dateTime
265 + " is not equal to the fixed value: " + _fixed;
266 throw new ValidationException(err);
267 }
268 }
269
270 if (isThereMinInclusive && dateTime.compareTo(_minInclusive) != DateTimeBase.GREATER_THAN
271 && !dateTime.equals(_minInclusive)) {
272 String err = dateTime.getClass().getName() + " " + dateTime
273 + " is less than the minimum allowed value: " + _minInclusive;
274 throw new ValidationException(err);
275 }
276
277 if (isThereMinExclusive && dateTime.compareTo(_minExclusive) != DateTimeBase.GREATER_THAN) {
278 String err = dateTime.getClass().getName() + " " + dateTime
279 + " is less than or equal to the minimum (exclusive) value: " + _minExclusive;
280 throw new ValidationException(err);
281 }
282
283 if (isThereMaxInclusive && dateTime.compareTo(_maxInclusive) != DateTimeBase.LESS_THAN
284 && !dateTime.equals(_maxInclusive)) {
285 String err = dateTime.getClass().getName() + " " + dateTime
286 + " is greater than the maximum allowed value: " + _maxInclusive;
287 throw new ValidationException(err);
288 }
289
290 if (isThereMaxExclusive && dateTime.compareTo(_maxExclusive) != DateTimeBase.LESS_THAN) {
291 String err = dateTime.getClass().getName() + " " + dateTime
292 + " is greater than or equal to the maximum (exclusive) value: "
293 + _maxExclusive;
294 throw new ValidationException(err);
295 }
296
297 if (hasPattern()) {
298 super.validate(dateTime.toString(), context);
299 }
300 }
301
302
303
304
305
306
307
308 public void validate(final Object object) throws ValidationException {
309 validate(object, (ValidationContext) null);
310 }
311
312
313
314
315
316
317
318
319 public void validate(final Object object, final ValidationContext context)
320 throws ValidationException {
321 if (object == null) {
322 String err = "DateTimeValidator cannot validate a null object.";
323 throw new ValidationException(err);
324 }
325
326 if (object instanceof String) {
327 try {
328 DateTime dateTime = new DateTime((String) object);
329 validate(dateTime, context);
330 return;
331 } catch (java.text.ParseException pe) {
332 String err = "String provided fails to parse into a DateTime: " + (String) object;
333 throw new ValidationException(err, pe);
334 }
335 }
336
337 DateTimeBase value = null;
338
339 try {
340 value = (DateTimeBase) object;
341 } catch (Exception ex) {
342 String err = ex.toString() + "\nExpecting a DateTime, received instead: "
343 + object.getClass().getName();
344 throw new ValidationException(err);
345 }
346
347 validate(value, context);
348 }
349
350 }