View Javadoc
1   /*
2    * Copyright 2006 Werner Guttmann
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.exolab.castor.xml.validators;
15  
16  import org.exolab.castor.xml.TypeValidator;
17  import org.exolab.castor.xml.ValidationContext;
18  import org.exolab.castor.xml.ValidationException;
19  
20  /**
21   * The Int Validation class. This class handles validation for the primitive <code>int</code> and
22   * <code>java.lang.Integer</code> types as well as all derived types such as unsigned-short.
23   *
24   * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner Guttmann</a>
25   * @version $Revision: 6571 $ $Date: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $
26   */
27  public class IntValidator extends PatternValidator implements TypeValidator {
28  
29    /** If true, we perform "minimum value" validation. */
30    private boolean _useMin = false;
31    /** If true, we perform "maximum value" validation. */
32    private boolean _useMax = false;
33    /** If true, we perform "fixed" validation. */
34    private boolean _useFixed = false;
35    /** Minimum value (inclusive) for this int. (Not used unless _useMin == true.) */
36    private int _min = 0;
37    /** Maximum value (inclusive) for this int. (Not used unless _useMax == true.) */
38    private int _max = 0;
39    /** Maximum number of digits in this int. (Not applied if < 0.) */
40    private int _totalDigits = -1;
41    /** Fixed value of this int. (Not used unless _useFixed == true.) */
42    private int _fixed = 0;
43  
44    /**
45     * Creates a new IntValidator with no restrictions.
46     */
47    public IntValidator() {
48      super();
49    } // -- IntegerValidator
50  
51    /**
52     * Clears the fixed value for this IntValidator.
53     */
54    public void clearFixed() {
55      _useFixed = false;
56    } // -- clearFixed
57  
58    /**
59     * Clears the maximum value for this IntValidator.
60     */
61    public void clearMax() {
62      _useMax = false;
63    } // -- clearMax
64  
65    /**
66     * Clears the minimum value for this IntValidator.
67     */
68    public void clearMin() {
69      _useMin = false;
70    } // -- clearMin
71  
72    /**
73     * Returns the configured fixed value for int validation. Returns null if no fixed value has been
74     * configured.
75     *
76     * @return the fixed value to validate against.
77     */
78    public Integer getFixed() {
79      if (_useFixed) {
80        return Integer.valueOf(_fixed);
81      }
82      return null;
83    } // -- getFixed
84  
85    /**
86     * Returns the configured maximum value for int validation. Returns null if no maximum has been
87     * configured.
88     *
89     * @return the maximum (inclusive) value to validate against.
90     */
91    public Integer getMaxInclusive() {
92      if (_useMax) {
93        return Integer.valueOf(_max);
94      }
95      return null;
96    } // -- getMaxInclusive
97  
98    /**
99     * Returns the configured minimum value for int validation. Returns null if no minimum has been
100    * configured.
101    *
102    * @return the minimum inclusive value to validate against.
103    */
104   public Integer getMinInclusive() {
105     if (_useMin) {
106       return Integer.valueOf(_min);
107     }
108     return null;
109   } // -- getMinInclusive
110 
111   /**
112    * Returns the configured maximum number of digits (inclusive) for int validation. Returns null if
113    * no maximum number of digits has been configured.
114    *
115    * @return the maximum number of digits to validate against.
116    */
117   public Integer getTotalDigits() {
118     if (_totalDigits >= 0) {
119       return Integer.valueOf(_totalDigits);
120     }
121     return null;
122   } // -- getTotalDigits
123 
124   /**
125    * Returns true if a fixed value to validate against has been set.
126    *
127    * @return true if a fixed value has been set.
128    */
129   public boolean hasFixed() {
130     return _useFixed;
131   } // -- hasFixed
132 
133   /**
134    * Sets the fixed value for int validation.
135    * <p>
136    * NOTE: If maximum and/or minimum values have been set and the fixed value is not within that
137    * max/min range, then no int will pass validation. This is as according to the XML Schema spec.
138    *
139    * @param fixedValue the fixed value that a int validated with this validator must be equal to.
140    */
141   public void setFixed(final int fixedValue) {
142     _useFixed = true;
143     this._fixed = fixedValue;
144   } // -- setFixed
145 
146   /**
147    * Sets the fixed value for int validation.
148    * <p>
149    * NOTE: If maximum and/or minimum values have been set and the fixed value is not within that
150    * max/min range, then no int will pass validation. This is as according to the XML Schema spec.
151    *
152    * @param fixedValue the fixed value that a int validated with this validator must be equal to.
153    */
154   public void setFixed(final Integer fixedValue) {
155     _useFixed = true;
156     this._fixed = fixedValue.intValue();
157   }
158 
159   /**
160    * Sets the minimum (exclusive) value for int validation. To pass validation, a int must be
161    * greater than this value.
162    *
163    * @param minValue the minimum (exclusive) value for int validation.
164    */
165   public void setMinExclusive(final int minValue) {
166     _useMin = true;
167     _min = minValue + 1;
168   } // -- setMinExclusive
169 
170   /**
171    * Sets the minimum (inclusive) value for int validation. To pass validation, a int must be
172    * greater than or equal to this value.
173    *
174    * @param minValue the minimum (inclusive) value for int validation.
175    */
176   public void setMinInclusive(final int minValue) {
177     _useMin = true;
178     _min = minValue;
179   } // -- setMinInclusive
180 
181   /**
182    * Sets the maximum (exclusive) value for int validation. To pass validation, a int must be less
183    * than this value.
184    *
185    * @param maxValue the maximum (exclusive) value for int validation.
186    */
187   public void setMaxExclusive(final int maxValue) {
188     _useMax = true;
189     _max = maxValue - 1;
190   } // -- setMaxExclusive
191 
192   /**
193    * Sets the maximum (inclusive) value for int validation. To pass validation, a int must be less
194    * than or equal to this value.
195    *
196    * @param maxValue the maximum (inclusive) value for int validation.
197    */
198   public void setMaxInclusive(final int maxValue) {
199     _useMax = true;
200     _max = maxValue;
201   } // -- setMaxInclusive
202 
203   /**
204    * Sets the maximum number of digits for int validation. To pass validation, a int must have this
205    * many digits or fewer. Leading zeros are not counted.
206    *
207    * @param totalDig the maximum (inclusive) number of digits for int validation. (must be > 0)
208    */
209   public void setTotalDigits(final int totalDig) {
210     if (totalDig <= 0) {
211       throw new IllegalArgumentException(
212           "IntegerValidator: the totalDigits facet must be positive");
213     }
214     _totalDigits = totalDig;
215   }
216 
217   /**
218    * Validates the given Object.
219    *
220    * @param i the long to validate
221    * @param context the ValidationContext
222    * @throws ValidationException if the object fails validation.
223    */
224   public void validate(final int i, final ValidationContext context) throws ValidationException {
225     if (_useFixed && i != _fixed) {
226       String err = "int " + i + " is not equal to the fixed value: " + _fixed;
227       throw new ValidationException(err);
228     }
229 
230     if (_useMin && i < _min) {
231       String err = "int " + i + " is less than the minimum allowed value: " + _min;
232       throw new ValidationException(err);
233     }
234 
235     if (_useMax && i > _max) {
236       String err = "int " + i + " is greater than the maximum allowed value: " + _max;
237       throw new ValidationException(err);
238     }
239 
240     if (_totalDigits != -1) {
241       int length = Integer.toString(i).length();
242       if (i < 0) {
243         length--;
244       }
245       if (length > _totalDigits) {
246         String err =
247             "int " + i + " has too many digits -- must have " + _totalDigits + " digits or fewer.";
248         throw new ValidationException(err);
249       }
250     }
251 
252     if (hasPattern()) {
253       super.validate(Integer.toString(i), context);
254     }
255   } // -- validate
256 
257   /**
258    * Validates the given Object.
259    *
260    * @param object the Object to validate
261    * @throws ValidationException if the object fails validation.
262    */
263   public void validate(final Object object) throws ValidationException {
264     validate(object, (ValidationContext) null);
265   } // -- validate
266 
267   /**
268    * Validates the given Object.
269    *
270    * @param object the Object to validate
271    * @param context the ValidationContext
272    * @throws ValidationException if the object fails validation.
273    */
274   public void validate(final Object object, final ValidationContext context)
275       throws ValidationException {
276     if (object == null) {
277       String err = "IntValidator cannot validate a null object.";
278       throw new ValidationException(err);
279     }
280 
281     int value = 0;
282     try {
283       value = ((Integer) object).intValue();
284     } catch (Exception ex) {
285       String err = "Expecting an Integer, received instead an instance of: ";
286       err += object.getClass().getName();
287       throw new ValidationException(err);
288     }
289     validate(value, context);
290   } // -- validate
291 
292 } // -- IntegerValidator