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