KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > intake > validator > NumberValidator


1 package org.apache.fulcrum.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.math.BigDecimal JavaDoc;
58 import java.util.Map JavaDoc;
59
60 import org.apache.fulcrum.ServiceException;
61
62 /**
63  * Validates numbers with the following constraints in addition to those
64  * listed in DefaultValidator.
65  *
66  * <table>
67  * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
68  * <tr><td>minValue</td><td>greater than BigDecimal.MIN_VALUE</td>
69  * <td>&nbsp;</td></tr>
70  * <tr><td>maxValue</td><td>less than BigDecimal.MAX_VALUE</td>
71  * <td>&nbsp;</td></tr>
72  * <tr><td>notANumberMessage</td><td>Some text</td>
73  * <td>Entry was not a valid number</td></tr>
74  * </table>
75  *
76  * @author <a HREF="mailto:jmcnally@collab.net>John McNally</a>
77  * @version $Id: NumberValidator.java,v 1.1 2004/11/12 10:26:02 epugh Exp $
78  */

79 public class NumberValidator
80     extends DefaultValidator
81 {
82     private static String JavaDoc INVALID_NUMBER = "Entry was not a valid number";
83
84     private BigDecimal JavaDoc minValue;
85     protected String JavaDoc minValueMessage;
86     private BigDecimal JavaDoc maxValue;
87     protected String JavaDoc maxValueMessage;
88     protected String JavaDoc invalidNumberMessage;
89
90     public NumberValidator(Map JavaDoc paramMap)
91         throws ServiceException
92     {
93         this();
94         init(paramMap);
95     }
96
97     public NumberValidator()
98     {
99         invalidNumberMessage = getDefaultInvalidNumberMessage();
100     }
101
102     /**
103      * Extract the relevant parameters from the constraints listed
104      * in <input-param> tags within the intake.xml file.
105      *
106      * @param inputParameters a <code>Map</code> of <code>InputParam</code>'s
107      * containing constraints on the input.
108      * @exception ServiceException if an error occurs
109      */

110     public void init(Map JavaDoc paramMap)
111         throws ServiceException
112     {
113         super.init(paramMap);
114
115         minValueMessage = null;
116         maxValueMessage = null;
117
118         doInit(paramMap);
119
120         Constraint constraint = (Constraint)paramMap.get("notANumberMessage");
121         if ( constraint != null )
122         {
123             String JavaDoc param = constraint.getValue();
124             if ( param != null && param.length() != 0 )
125             {
126                 invalidNumberMessage = param;
127             }
128             else if ( constraint.getMessage().length() != 0 )
129             {
130                 invalidNumberMessage = constraint.getMessage();
131             }
132         }
133     }
134
135     protected void doInit(Map JavaDoc paramMap)
136     {
137         minValue = null;
138         maxValue = null;
139
140         Constraint constraint = (Constraint)paramMap.get("minValue");
141         if ( constraint != null )
142         {
143             String JavaDoc param = constraint.getValue();
144             minValue = new BigDecimal JavaDoc(param);
145             minValueMessage = constraint.getMessage();
146         }
147
148         constraint = (Constraint)paramMap.get("maxValue");
149         if ( constraint != null )
150         {
151             String JavaDoc param = constraint.getValue();
152             maxValue = new BigDecimal JavaDoc(param);
153             maxValueMessage = constraint.getMessage();
154         }
155     }
156
157     protected String JavaDoc getDefaultInvalidNumberMessage()
158     {
159         return INVALID_NUMBER;
160     }
161
162     /**
163      * Determine whether a testValue meets the criteria specified
164      * in the constraints defined for this validator
165      *
166      * @param testValue a <code>String</code> to be tested
167      * @exception ValidationException containing an error message if the
168      * testValue did not pass the validation tests.
169      */

170     protected void doAssertValidity(String JavaDoc testValue)
171         throws ValidationException
172     {
173         BigDecimal JavaDoc bd = null;
174         try
175         {
176             bd = new BigDecimal JavaDoc(testValue);
177         }
178         catch (RuntimeException JavaDoc e)
179         {
180             message = invalidNumberMessage;
181             throw new ValidationException(invalidNumberMessage);
182         }
183
184         if ( minValue != null && bd.compareTo(minValue) < 0 )
185         {
186             message = minValueMessage;
187             throw new ValidationException(minValueMessage);
188         }
189         if ( maxValue != null && bd.compareTo(maxValue) > 0 )
190         {
191             message = maxValueMessage;
192             throw new ValidationException(maxValueMessage);
193         }
194     }
195
196     // ************************************************************
197
// ** Bean accessor methods **
198
// ************************************************************
199

200     /**
201      * Get the value of minValue.
202      * @return value of minValue.
203      */

204     public BigDecimal JavaDoc getMinValueAsBigDecimal()
205     {
206         return minValue;
207     }
208
209     /**
210      * Set the value of minValue.
211      * @param v Value to assign to minValue.
212      */

213     public void setMinValue(BigDecimal JavaDoc v)
214     {
215         this.minValue = v;
216     }
217
218     /**
219      * Get the value of minValueMessage.
220      * @return value of minValueMessage.
221      */

222     public String JavaDoc getMinValueMessage()
223     {
224         return minValueMessage;
225     }
226
227     /**
228      * Set the value of minValueMessage.
229      * @param v Value to assign to minValueMessage.
230      */

231     public void setMinValueMessage(String JavaDoc v)
232     {
233         this.minValueMessage = v;
234     }
235
236     /**
237      * Get the value of maxValue.
238      * @return value of maxValue.
239      */

240     public BigDecimal JavaDoc getMaxValueAsBigDecimal()
241     {
242         return maxValue;
243     }
244
245     /**
246      * Set the value of maxValue.
247      * @param v Value to assign to maxValue.
248      */

249     public void setMaxValue(BigDecimal JavaDoc v)
250     {
251         this.maxValue = v;
252     }
253
254     /**
255      * Get the value of maxValueMessage.
256      * @return value of maxValueMessage.
257      */

258     public String JavaDoc getMaxValueMessage()
259     {
260         return maxValueMessage;
261     }
262
263     /**
264      * Set the value of maxValueMessage.
265      * @param v Value to assign to maxValueMessage.
266      */

267     public void setMaxValueMessage(String JavaDoc v)
268     {
269         this.maxValueMessage = v;
270     }
271
272     /**
273      * Get the value of invalidNumberMessage.
274      * @return value of invalidNumberMessage.
275      */

276     public String JavaDoc getInvalidNumberMessage()
277     {
278         return invalidNumberMessage;
279     }
280
281     /**
282      * Set the value of invalidNumberMessage.
283      * @param v Value to assign to invalidNumberMessage.
284      */

285     public void setInvalidNumberMessage(String JavaDoc v)
286     {
287         this.invalidNumberMessage = v;
288     }
289
290 }
291
Popular Tags