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