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