1 package org.apache.turbine.services.intake.validator;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.util.Map;
58 import org.apache.turbine.util.TurbineException;
59
60 /***
61 * Validates numbers with the following constraints in addition to those
62 * listed in DefaultValidator.
63 *
64 * <table>
65 * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
66 * <tr><td>minValue</td><td>greater than Integer.MIN_VALUE</td>
67 * <td> </td></tr>
68 * <tr><td>maxValue</td><td>less than Integer.MAX_VALUE</td>
69 * <td> </td></tr>
70 * <tr><td>notANumberMessage</td><td>Some text</td>
71 * <td>Entry was not a valid number</td></tr>
72 * </table>
73 *
74 * @author <a href="mailto:jmcnally@collab.net>John McNally</a>
75 * @version $Id: IntegerValidator.java,v 1.3 2002/07/16 16:10:22 henning Exp $
76 */
77 public class IntegerValidator
78 extends NumberValidator
79 {
80 private static String INVALID_NUMBER = "Entry was not a valid integer";
81
82 private int minValue;
83 private int maxValue;
84
85 public IntegerValidator(Map paramMap)
86 throws TurbineException
87 {
88 this();
89 init(paramMap);
90 }
91
92 public IntegerValidator()
93 {
94 // sets the default invalid number message
95 super();
96 }
97
98 protected void doInit(Map paramMap)
99 {
100 minValue = Integer.MIN_VALUE;
101 maxValue = Integer.MAX_VALUE;
102
103 Constraint constraint = (Constraint)paramMap.get("minValue");
104 if ( constraint != null )
105 {
106 String param = constraint.getValue();
107 minValue = Integer.parseInt(param);
108 minValueMessage = constraint.getMessage();
109 }
110
111 constraint = (Constraint)paramMap.get("maxValue");
112 if ( constraint != null )
113 {
114 String param = constraint.getValue();
115 maxValue = Integer.parseInt(param);
116 maxValueMessage = constraint.getMessage();
117 }
118 }
119
120 protected String getDefaultInvalidNumberMessage()
121 {
122 return INVALID_NUMBER;
123 }
124
125 /***
126 * Determine whether a testValue meets the criteria specified
127 * in the constraints defined for this validator
128 *
129 * @param testValue a <code>String</code> to be tested
130 * @exception ValidationException containing an error message if the
131 * testValue did not pass the validation tests.
132 */
133 protected void doAssertValidity(String testValue)
134 throws ValidationException
135 {
136 int i = 0;
137 try
138 {
139 i = Integer.parseInt(testValue);
140 }
141 catch (RuntimeException e)
142 {
143 message = invalidNumberMessage;
144 throw new ValidationException(invalidNumberMessage);
145 }
146
147 if ( i < minValue )
148 {
149 message = minValueMessage;
150 throw new ValidationException(minValueMessage);
151 }
152 if ( i > maxValue )
153 {
154 message = maxValueMessage;
155 throw new ValidationException(maxValueMessage);
156 }
157 }
158
159
160 // ************************************************************
161 // ** Bean accessor methods **
162 // ************************************************************
163
164 /***
165 * Get the value of minValue.
166 * @return value of minValue.
167 */
168 public int getMinValue()
169 {
170 return minValue;
171 }
172
173 /***
174 * Set the value of minValue.
175 * @param v Value to assign to minValue.
176 */
177 public void setMinValue(int v)
178 {
179 this.minValue = v;
180 }
181
182 /***
183 * Get the value of maxValue.
184 * @return value of maxValue.
185 */
186 public int getMaxValue()
187 {
188 return maxValue;
189 }
190
191 /***
192 * Set the value of maxValue.
193 * @param v Value to assign to maxValue.
194 */
195 public void setMaxValue(int v)
196 {
197 this.maxValue = v;
198 }
199 }
This page was automatically generated by Maven