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.torque.om.NumberKey;
59 import org.apache.turbine.util.TurbineException;
60
61 /***
62 * Validates numbers with the following constraints in addition to those
63 * listed in DefaultValidator.
64 *
65 * <table>
66 * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
67 * <tr><td>minValue</td><td>greater than Integer.MIN_VALUE</td>
68 * <td> </td></tr>
69 * <tr><td>maxValue</td><td>less than BigDecimal.MAX_VALUE</td>
70 * <td> </td></tr>
71 * <tr><td>notANumberMessage</td><td>Some text</td>
72 * <td>Entry was not a valid number</td></tr>
73 * </table>
74 *
75 * @author <a href="mailto:jmcnally@collab.net>John McNally</a>
76 * @version $Id: NumberKeyValidator.java,v 1.4 2002/07/16 16:10:22 henning Exp $
77 */
78 public class NumberKeyValidator
79 extends NumberValidator
80 {
81 private static String INVALID_NUMBER = "Entry was not valid.";
82
83 private NumberKey minValue;
84 private NumberKey maxValue;
85
86 public NumberKeyValidator(Map paramMap)
87 throws TurbineException
88 {
89 this();
90 init(paramMap);
91 }
92
93 public NumberKeyValidator()
94 {
95 // sets the default invalid number message
96 super();
97 }
98
99 protected void doInit(Map paramMap)
100 {
101 minValue = null;
102 maxValue = null;
103
104 Constraint constraint = (Constraint)paramMap.get("minValue");
105 if ( constraint != null )
106 {
107 String param = constraint.getValue();
108 minValue = new NumberKey(param);
109 minValueMessage = constraint.getMessage();
110 }
111
112 constraint = (Constraint)paramMap.get("maxValue");
113 if ( constraint != null )
114 {
115 String param = constraint.getValue();
116 maxValue = new NumberKey(param);
117 maxValueMessage = constraint.getMessage();
118 }
119 }
120
121 protected String getDefaultInvalidNumberMessage()
122 {
123 return INVALID_NUMBER;
124 }
125
126 /***
127 * Determine whether a testValue meets the criteria specified
128 * in the constraints defined for this validator
129 *
130 * @param testValue a <code>String</code> to be tested
131 * @exception ValidationException containing an error message if the
132 * testValue did not pass the validation tests.
133 */
134 protected void doAssertValidity(String testValue)
135 throws ValidationException
136 {
137 NumberKey nk = null;
138 try
139 {
140 nk = new NumberKey(testValue);
141 }
142 catch (RuntimeException e)
143 {
144 message = invalidNumberMessage;
145 throw new ValidationException(invalidNumberMessage);
146 }
147 if ( minValue != null && nk.compareTo(minValue) < 0 )
148 {
149 message = minValueMessage;
150 throw new ValidationException(minValueMessage);
151 }
152 if ( maxValue != null && nk.compareTo(maxValue) > 0 )
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 NumberKey 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(NumberKey v)
178 {
179 this.minValue = v;
180 }
181
182 /***
183 * Get the value of maxValue.
184 * @return value of maxValue.
185 */
186 public NumberKey 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(NumberKey v)
196 {
197 this.maxValue = v;
198 }
199 }
This page was automatically generated by Maven