1 /*
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Intalio, Inc. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Intalio, Inc. Exolab is a registered
23 * trademark of Intalio, Inc.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2000-2003 (C) Intalio, Inc. All Rights Reserved.
42 *
43 * $Id$
44 * Date Author Changes
45 * 12/06/2000 Arnaud Blandin Created
46 */
47 package org.exolab.castor.xml.validators;
48
49 import org.exolab.castor.xml.TypeValidator;
50 import org.exolab.castor.xml.ValidationContext;
51 import org.exolab.castor.xml.ValidationException;
52
53 /**
54 * The Double Validation class. This class handles validation for the primitive
55 * <code>double</code> and <code>java.lang.Double</code> types.
56 *
57 * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
58 * @version $Revision$ $Date: 2003-03-03 02:57:21 -0700 (Mon, 03 Mar 2003) $
59 */
60 public class DoubleValidator extends PatternValidator implements TypeValidator {
61
62 /** If true, we perform "minimum inclusive value" validation. */
63 private boolean _useMinInclusive = false;
64 /** If true, we perform "minimum exclusive value" validation. */
65 private boolean _useMinExclusive = false;
66 /** If true, we perform "maximum inclusive value" validation. */
67 private boolean _useMaxInclusive = false;
68 /** If true, we perform "maximum exclusive value" validation. */
69 private boolean _useMaxExclusive = false;
70
71 private boolean _usePositiveInfinity = false;
72 private boolean _useNegativeInfinity = false;
73
74 /** If true, we perform "fixed" validation. */
75 private boolean _useFixed = false;
76 /** Minimum value (inclusive) for this double. (Not used unless _useMinInclusive == true.) */
77 private double _minInclusive = 0;
78 /** Minimum value (exclusive) for this double. (Not used unless _useMinExclusive == true.) */
79 private double _minExclusive = 0;
80 /** Maximum value (inclusive) for this double. (Not used unless _useMaxInclusive == true.) */
81 private double _maxInclusive = 0;
82 /** Maximum value (exclusive) for this double. (Not used unless _useMaxExclusive == true.) */
83 private double _maxExclusive = 0;
84 /** Fixed value of this double. (Not used unless _isFixed == true.) */
85 private double _fixed = 0;
86
87 private double _positiveInfinity = 0;
88 private double _negativeInfinity = 0;
89
90 /**
91 * Creates a new DoubleValidator with no restrictions.
92 */
93 public DoubleValidator() {
94 super();
95 }
96
97 /**
98 * Clears the fixed value for this DoubleValidator.
99 */
100 public void clearFixed() {
101 _useFixed = false;
102 }
103
104 /**
105 * Clears the maximum value for this DoubleValidator.
106 */
107 public void clearMax() {
108 _useMaxExclusive = false;
109 _useMaxInclusive = false;
110 }
111
112 /**
113 * Clears the minimum value for this DoubleValidator.
114 */
115 public void clearMin() {
116 _useMinExclusive = false;
117 _useMinInclusive = false;
118 }
119
120 public void clearPositiveInfinity() {
121 _usePositiveInfinity = false;
122 }
123
124 public void clearNegativeInfinity() {
125 _useNegativeInfinity = false;
126 }
127
128 /**
129 * Returns the configured fixed value for double validation. Returns null if
130 * no fixed value has been configured.
131 *
132 * @return the fixed value to validate against.
133 */
134 public Double getFixed() {
135 if (_useFixed) {
136 return new Double(_fixed);
137 }
138 return null;
139 }
140
141 /**
142 * Returns the configured inclusive maximum value for double validation.
143 * Returns null if no inclusive maximum has been configured.
144 *
145 * @return the inclusive maximum value to validate against.
146 */
147 public Double getMaxInclusive() {
148 if (_useMaxInclusive) {
149 return new Double(_maxInclusive);
150 }
151 return null;
152 }
153
154 /**
155 * Returns the configured exclusive maximum value for double validation.
156 * Returns null if no exclusive maximum has been configured.
157 *
158 * @return the exclusive maximum value to validate against.
159 */
160 public Double getMaxExclusive() {
161 if (_useMaxExclusive) {
162 return new Double(_maxExclusive);
163 }
164 return null;
165 }
166
167 /**
168 * Returns the configured inclusive minimum value for double validation.
169 * Returns null if no inclusive minimum has been configured.
170 *
171 * @return the inclusive minimum value to validate against.
172 */
173 public Double getMinInclusive() {
174 if (_useMinInclusive) {
175 return new Double(_minInclusive);
176 }
177 return null;
178 }
179
180 /**
181 * Returns the configured exclusive minimum value for double validation.
182 * Returns null if no exclusive minimum has been configured.
183 *
184 * @return the exclusive minimum value to validate against.
185 */
186 public Double getMinExclusive() {
187 if (_useMinExclusive) {
188 return new Double(_minExclusive);
189 }
190 return null;
191 }
192
193 public Double getPositiveInfinity() {
194 return new Double(_positiveInfinity);
195 }
196
197 public Double getNegativeInfinity() {
198 return new Double(_negativeInfinity);
199 }
200
201 /**
202 * Returns true if a fixed value to validate against has been set.
203 *
204 * @return true if a fixed value has been set.
205 */
206 public boolean hasFixed() {
207 return _useFixed;
208 }
209
210 /**
211 * Sets the fixed value for double validation.
212 * <p>
213 * NOTE: If maximum and/or minimum values have been set and the fixed value
214 * is not within that max/min range, then no double will pass validation.
215 * This is as according to the XML Schema spec.
216 *
217 * @param fixedValue
218 * the fixed value that a double validated with this validator
219 * must be equal to.
220 */
221 public void setFixed(final double fixedValue) {
222 _fixed = fixedValue;
223 _useFixed = true;
224 }
225
226 /**
227 * Sets the minimum (exclusive) value for double validation. To pass
228 * validation, a double must be greater than this value.
229 *
230 * @param minValue
231 * the minimum (exclusive) value for double validation.
232 */
233 public void setMinExclusive(final double minValue) {
234 _minExclusive = minValue;
235 _useMinExclusive = true;
236 }
237
238 /**
239 * Sets the minimum (inclusive) value for double validation. To pass
240 * validation, a double must be greater than or equal to this value.
241 *
242 * @param minValue
243 * the minimum (inclusive) value for double validation.
244 */
245 public void setMinInclusive(final double minValue) {
246 _minInclusive = minValue;
247 _useMinInclusive = true;
248 }
249
250 /**
251 * Sets the maximum (exclusive) value for double validation. To pass
252 * validation, a double must be less than this value.
253 *
254 * @param maxValue
255 * the maximum (exclusive) value for double validation.
256 */
257 public void setMaxExclusive(final double maxValue) {
258 _maxExclusive = maxValue;
259 _useMaxExclusive = true;
260 }
261
262 /**
263 * Sets the maximum (inclusive) value for double validation. To pass
264 * validation, a double must be less than or equal to this value.
265 *
266 * @param maxValue
267 * the maximum (inclusive) value for double validation.
268 */
269 public void setMaxInclusive(final double maxValue) {
270 _maxInclusive = maxValue;
271 _useMaxInclusive = true;
272 }
273
274 public void setPositiveInfinity(final double positiveInfinity) {
275 _positiveInfinity = positiveInfinity;
276 _usePositiveInfinity = true;
277 }
278
279 public void setNegativeInfinity(final double negativeInfinity) {
280 _negativeInfinity = negativeInfinity;
281 _useNegativeInfinity = true;
282 }
283
284 /**
285 * Validates the given Object.
286 *
287 * @param d
288 * the double to validate
289 * @param context
290 * the ValidationContext
291 * @throws ValidationException if the object fails validation.
292 */
293 public void validate(final double d, final ValidationContext context)
294 throws ValidationException {
295 if (_useFixed && d != _fixed) {
296 String err = "double " + d + " is not equal to the fixed value: " + _fixed;
297 throw new ValidationException(err);
298 }
299
300 if (_useMinInclusive && d < _minInclusive && d != _negativeInfinity) {
301 String err = "double " + d + " is less than the minimum allowed value: "
302 + _minInclusive;
303 throw new ValidationException(err);
304 }
305
306 if (_useMinExclusive && d <= _minExclusive) {
307 String err = "double " + d
308 + " is less than or equal to the maximum exclusive value: " + _minExclusive;
309 throw new ValidationException(err);
310 }
311
312 if (_useMaxInclusive && d > _maxInclusive && d != _positiveInfinity) {
313 String err = "double " + d + " is greater than the maximum allowed value: "
314 + _maxInclusive;
315 throw new ValidationException(err);
316 }
317
318 if (_useMaxExclusive && d >= _maxExclusive) {
319 String err = "double " + d
320 + " is greater than or equal to the maximum exclusive value: " + _maxExclusive;
321 throw new ValidationException(err);
322 }
323
324 if (hasPattern()) {
325 super.validate(Double.toString(d), context);
326 }
327 } // -- validate
328
329 /**
330 * Validates the given Object.
331 *
332 * @param object
333 * the Object to validate
334 * @throws ValidationException if the object fails validation.
335 */
336 public void validate(final Object object) throws ValidationException {
337 validate(object, (ValidationContext) null);
338 } // -- validate
339
340 /**
341 * Validates the given Object.
342 *
343 * @param object
344 * the Object to validate
345 * @param context
346 * the ValidationContext
347 * @throws ValidationException if the object fails validation.
348 */
349 public void validate(final Object object, final ValidationContext context)
350 throws ValidationException {
351 if (object == null) {
352 String err = "doubleValidator cannot validate a null object.";
353 throw new ValidationException(err);
354 }
355
356 double value = 0;
357 try {
358 value = new java.lang.Double(object.toString()).doubleValue();
359 } catch (Exception ex) {
360 String err = "Expecting a double, received instead: ";
361 err += object.getClass().getName();
362 throw new ValidationException(err);
363 }
364 validate(value, context);
365 } //-- validate
366
367 } //-- doubleValidator