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