KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > intake > validator > IntegerValidator


1 package org.apache.turbine.services.intake.validator;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.Map JavaDoc;
20
21 import org.apache.commons.lang.StringUtils;
22
23 /**
24  * Validates Integers with the following constraints in addition to those
25  * listed in NumberValidator and DefaultValidator.
26  *
27  * <table>
28  * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
29  * <tr><td>minValue</td><td>greater than Integer.MIN_VALUE</td>
30  * <td>&nbsp;</td></tr>
31  * <tr><td>maxValue</td><td>less than Integer.MAX_VALUE</td>
32  * <td>&nbsp;</td></tr>
33  * <tr><td>invalidNumberMessage</td><td>Some text</td>
34  * <td>Entry was not a valid number</td></tr>
35  * </table>
36  *
37  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
38  * @author <a HREF="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
39  * @version $Id: IntegerValidator.java,v 1.8.2.4 2004/05/20 03:06:47 seade Exp $
40  */

41 public class IntegerValidator
42         extends NumberValidator
43 {
44     /* Init the minValue to that for a Integer */
45     private int minValue = Integer.MIN_VALUE;
46
47     /* Init the maxValue to that for a Integer */
48     private int maxValue = Integer.MAX_VALUE;
49
50     /**
51      * Constructor to use when initialising Object
52      *
53      * @param paramMap
54      * @throws InvalidMaskException
55      */

56     public IntegerValidator(Map JavaDoc paramMap)
57             throws InvalidMaskException
58     {
59         invalidNumberMessage = "Entry was not a valid Integer";
60         init(paramMap);
61     }
62
63     /**
64      * Default Constructor
65      */

66     public IntegerValidator()
67     {
68     }
69
70     /**
71      * Method to initialise Object
72      *
73      * @param paramMap
74      * @throws InvalidMaskException
75      */

76     public void init(Map JavaDoc paramMap)
77             throws InvalidMaskException
78     {
79         super.init(paramMap);
80
81         Constraint constraint = (Constraint) paramMap.get(MIN_VALUE_RULE_NAME);
82         if (constraint != null)
83         {
84             String JavaDoc param = constraint.getValue();
85             minValue = Integer.parseInt(param);
86             minValueMessage = constraint.getMessage();
87         }
88
89         constraint = (Constraint) paramMap.get(MAX_VALUE_RULE_NAME);
90         if (constraint != null)
91         {
92             String JavaDoc param = constraint.getValue();
93             maxValue = Integer.parseInt(param);
94             maxValueMessage = constraint.getMessage();
95         }
96     }
97
98     /**
99      * Determine whether a testValue meets the criteria specified
100      * in the constraints defined for this validator
101      *
102      * @param testValue a <code>String</code> to be tested
103      * @exception ValidationException containing an error message if the
104      * testValue did not pass the validation tests.
105      */

106     public void assertValidity(String JavaDoc testValue)
107             throws ValidationException
108     {
109         super.assertValidity(testValue);
110
111         if (required || StringUtils.isNotEmpty(testValue))
112         {
113             int i = 0;
114             try
115             {
116                 i = Integer.parseInt(testValue);
117             }
118             catch (RuntimeException JavaDoc e)
119             {
120                 errorMessage = invalidNumberMessage;
121                 throw new ValidationException(invalidNumberMessage);
122             }
123             
124             if (i < minValue)
125             {
126                 errorMessage = minValueMessage;
127                 throw new ValidationException(minValueMessage);
128             }
129             if (i > maxValue)
130             {
131                 errorMessage = maxValueMessage;
132                 throw new ValidationException(maxValueMessage);
133             }
134         }
135     }
136
137
138     // ************************************************************
139
// ** Bean accessor methods **
140
// ************************************************************
141

142     /**
143      * Get the value of minValue.
144      *
145      * @return value of minValue.
146      */

147     public int getMinValue()
148     {
149         return minValue;
150     }
151
152     /**
153      * Set the value of minValue.
154      *
155      * @param minValue Value to assign to minValue.
156      */

157     public void setMinValue(int minValue)
158     {
159         this.minValue = minValue;
160     }
161
162     /**
163      * Get the value of maxValue.
164      *
165      * @return value of maxValue.
166      */

167     public int getMaxValue()
168     {
169         return maxValue;
170     }
171
172     /**
173      * Set the value of maxValue.
174      *
175      * @param maxValue Value to assign to maxValue.
176      */

177     public void setMaxValue(int maxValue)
178     {
179         this.maxValue = maxValue;
180     }
181 }
182
Popular Tags