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