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