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 2001-2003 (C) Intalio, Inc. All Rights Reserved. 42 * 43 */ 44 package org.exolab.castor.xml.validators; 45 46 import org.exolab.castor.xml.TypeValidator; 47 import org.exolab.castor.xml.ValidationContext; 48 import org.exolab.castor.xml.ValidationException; 49 50 /** 51 * The Short Validation class. This class handles validation for the primitive 52 * <code>short</code> and the <code>java.lang.Short</code> types. 53 * 54 * @author <a href="mailto:visco@intalio.com">Keith Visco</a> 55 * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a> 56 * @version $Revision$ $Date: 2003-03-03 02:57:21 -0700 (Mon, 03 Mar 2003) $ 57 */ 58 public class ShortValidator extends PatternValidator implements TypeValidator { 59 60 /** If true, we perform "minimum value" validation. */ 61 private boolean _useMin = false; 62 /** If true, we perform "maximum value" validation. */ 63 private boolean _useMax = false; 64 /** If true, we perform "fixed" validation. */ 65 private boolean _useFixed = false; 66 /** Minimum value (inclusive) for this short. (Not used unless _useMin == true.) */ 67 private short _min = 0; 68 /** Maximum value (inclusive) for this short. (Not used unless _useMax == true.) */ 69 private short _max = 0; 70 /** Maximum number of digits in this short. (Not applied if < 0.) */ 71 private int _totalDigits = -1; 72 /** Fixed value of this short. (Not used unless _useFixed == true.) */ 73 private short _fixed = 0; 74 75 /** 76 * Creates a new ShortValidator with no restrictions. 77 */ 78 public ShortValidator() { 79 super(); 80 } // -- ShortValidator 81 82 /** 83 * Clears the fixed value for this ShortValidator. 84 */ 85 public void clearFixed() { 86 _useFixed = false; 87 } // -- clearFixed 88 89 /** 90 * Clears the maximum value for this ShortValidator. 91 */ 92 public void clearMax() { 93 _useMax = false; 94 } // -- clearMax 95 96 /** 97 * Clears the minimum value for this ShortValidator. 98 */ 99 public void clearMin() { 100 _useMin = false; 101 } // -- clearMin 102 103 /** 104 * Returns the configured fixed value for short validation. Returns null if 105 * no fixed value has been configured. 106 * 107 * @return the fixed value to validate against. 108 */ 109 public Short getFixed() { 110 if (_useFixed) { 111 return new Short(_fixed); 112 } 113 return null; 114 } // -- getFixed 115 116 /** 117 * Returns the configured maximum value for short validation. Returns null 118 * if no maximum has been configured. 119 * 120 * @return the maximum (inclusive) value to validate against. 121 */ 122 public Short getMaxInclusive() { 123 if (_useMax) { 124 return new Short(_max); 125 } 126 return null; 127 } // -- getMaxInclusive 128 129 /** 130 * Returns the configured minimum value for short validation. Returns null 131 * if no minimum has been configured. 132 * 133 * @return the minimum (inclusive) value to validate against. 134 */ 135 public Short getMinInclusive() { 136 if (_useMin) { 137 return new Short(_min); 138 } 139 return null; 140 } // -- getMinInclusive 141 142 /** 143 * Returns the configured maximum number of digits (inclusive) for 144 * short validation. Returns null if no maximum number of digits has 145 * been configured. 146 * 147 * @return the maximum number of digits to validate against. 148 */ 149 public Integer getTotalDigits() { 150 if (_totalDigits >= 0) { 151 return new Integer(_totalDigits); 152 } 153 return null; 154 } // -- getTotalDigits 155 156 /** 157 * Returns true if a fixed value to validate against has been set. 158 * 159 * @return true if a fixed value has been set. 160 */ 161 public boolean hasFixed() { 162 return _useFixed; 163 } // -- hasFixed 164 165 /** 166 * Sets the fixed value for short validation. 167 * <p> 168 * NOTE: If maximum and/or minimum values have been set and the fixed value 169 * is not within that max/min range, then no short will pass validation. 170 * This is as according to the XML Schema spec. 171 * 172 * @param fixedValue 173 * the fixed value that a short validated with this validator 174 * must be equal to. 175 */ 176 public void setFixed(final short fixedValue) { 177 _useFixed = true; 178 this._fixed = fixedValue; 179 } // -- setFixed 180 181 /** 182 * Sets the minimum (exclusive) value for short validation. To pass 183 * validation, a short must be greater than this value. 184 * 185 * @param minValue 186 * the minimum (exclusive) value for short validation. 187 */ 188 public void setMinExclusive(final short minValue) { 189 _useMin = true; 190 _min = (short) (minValue + 1); 191 } // -- setMinExclusive 192 193 /** 194 * Sets the minimum (inclusive) value for short validation. To pass 195 * validation, a short must be greater than or equal to this value. 196 * 197 * @param minValue 198 * the minimum (inclusive) value for short validation. 199 */ 200 public void setMinInclusive(final short minValue) { 201 _useMin = true; 202 _min = minValue; 203 } // -- setMinInclusive 204 205 /** 206 * Sets the maximum (exclusive) value for short validation. To pass 207 * validation, a short must be less than this value. 208 * 209 * @param maxValue 210 * the maximum (exclusive) value for short validation. 211 */ 212 public void setMaxExclusive(final short maxValue) { 213 _useMax = true; 214 _max = (short) (maxValue - 1); 215 } // -- setMaxExclusive 216 217 /** 218 * Sets the maximum (inclusive) value for short validation. To pass 219 * validation, a short must be less than or equal to this value. 220 * 221 * @param maxValue 222 * the maximum (inclusive) value for short validation. 223 */ 224 public void setMaxInclusive(final short maxValue) { 225 _useMax = true; 226 _max = maxValue; 227 } // -- setMaxInclusive 228 229 /** 230 * Sets the maximum number of digits for short validation. To pass 231 * validation, a short must have this many digits or fewer. Leading zeros 232 * are not counted. 233 * 234 * @param totalDig 235 * the maximum (inclusive) number of digits for short validation. 236 * (must be > 0) 237 */ 238 public void setTotalDigits(final int totalDig) { 239 if (totalDig <= 0) { 240 throw new IllegalArgumentException( 241 "ShortValidator: the totalDigits facet must be positive"); 242 } 243 _totalDigits = totalDig; 244 } 245 246 /** 247 * Validates the given Object. 248 * 249 * @param s 250 * the short to validate 251 * @param context 252 * the ValidationContext 253 * @throws ValidationException if the object fails validation. 254 */ 255 public void validate(final short s, final ValidationContext context) 256 throws ValidationException { 257 if (_useFixed && s != _fixed) { 258 String err = "short " + s + " is not equal to the fixed value: " + _fixed; 259 throw new ValidationException(err); 260 } 261 262 if (_useMin && s < _min) { 263 String err = "short " + s + " is less than the minimum allowed value: " + _min; 264 throw new ValidationException(err); 265 } 266 267 if (_useMax && s > _max) { 268 String err = "short " + s + " is greater than the maximum allowed value: " + _max; 269 throw new ValidationException(err); 270 } 271 272 if (_totalDigits != -1) { 273 int length = Short.toString(s).length(); 274 if (s < 0) { 275 length--; 276 } 277 if (length > _totalDigits) { 278 String err = "short " + s + " has too many digits -- must have " 279 + _totalDigits + " digits or fewer."; 280 throw new ValidationException(err); 281 } 282 } 283 284 if (hasPattern()) { 285 super.validate(Short.toString(s), context); 286 } 287 } // -- validate 288 289 /** 290 * Validates the given Object. 291 * 292 * @param object 293 * the Object to validate 294 * @throws ValidationException if the object fails validation. 295 */ 296 public void validate(final Object object) throws ValidationException { 297 validate(object, (ValidationContext) null); 298 } // -- validate 299 300 /** 301 * Validates the given Object. 302 * 303 * @param object 304 * the Object to validate 305 * @param context 306 * the ValidationContext 307 * @throws ValidationException if the object fails validation. 308 */ 309 public void validate(final Object object, final ValidationContext context) 310 throws ValidationException { 311 if (object == null) { 312 String err = "ShortValidator cannot validate a null object."; 313 throw new ValidationException(err); 314 } 315 316 short value = 0; 317 try { 318 value = ((Short) object).shortValue(); 319 } catch (Exception ex) { 320 String err = "Expecting a Short, received instead: " + object.getClass().getName(); 321 throw new ValidationException(err); 322 } 323 validate(value, context); 324 } //-- validate 325 326 } //-- ShortValidator