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