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