KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Map JavaDoc;
58
59 import org.apache.fulcrum.ServiceException;
60 import org.apache.torque.om.NumberKey;
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 Integer.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: NumberKeyValidator.java,v 1.1 2004/11/12 10:26:02 epugh Exp $
78  */

79 public class NumberKeyValidator
80     extends NumberValidator
81 {
82     private static String JavaDoc INVALID_NUMBER = "Entry was not valid.";
83
84     private NumberKey minValue;
85     private NumberKey maxValue;
86
87     public NumberKeyValidator(Map JavaDoc paramMap)
88         throws ServiceException
89     {
90         this();
91         init(paramMap);
92     }
93
94     public NumberKeyValidator()
95     {
96         // sets the default invalid number message
97
super();
98     }
99
100     protected void doInit(Map JavaDoc paramMap)
101     {
102         minValue = null;
103         maxValue = null;
104
105         Constraint constraint = (Constraint)paramMap.get("minValue");
106         if ( constraint != null )
107         {
108             String JavaDoc param = constraint.getValue();
109             minValue = new NumberKey(param);
110             minValueMessage = constraint.getMessage();
111         }
112
113         constraint = (Constraint)paramMap.get("maxValue");
114         if ( constraint != null )
115         {
116             String JavaDoc param = constraint.getValue();
117             maxValue = new NumberKey(param);
118             maxValueMessage = constraint.getMessage();
119         }
120     }
121
122     protected String JavaDoc getDefaultInvalidNumberMessage()
123     {
124         return INVALID_NUMBER;
125     }
126
127     /**
128      * Determine whether a testValue meets the criteria specified
129      * in the constraints defined for this validator
130      *
131      * @param testValue a <code>String</code> to be tested
132      * @exception ValidationException containing an error message if the
133      * testValue did not pass the validation tests.
134      */

135     protected void doAssertValidity(String JavaDoc testValue)
136         throws ValidationException
137     {
138         NumberKey nk = null;
139         try
140         {
141             nk = new NumberKey(testValue);
142         }
143         catch (RuntimeException JavaDoc e)
144         {
145             message = invalidNumberMessage;
146             throw new ValidationException(invalidNumberMessage);
147         }
148         if ( minValue != null && nk.compareTo(minValue) < 0 )
149         {
150             message = minValueMessage;
151             throw new ValidationException(minValueMessage);
152         }
153         if ( maxValue != null && nk.compareTo(maxValue) > 0 )
154         {
155             message = maxValueMessage;
156             throw new ValidationException(maxValueMessage);
157         }
158     }
159
160
161     // ************************************************************
162
// ** Bean accessor methods **
163
// ************************************************************
164

165     /**
166      * Get the value of minValue.
167      * @return value of minValue.
168      */

169     public NumberKey getMinValue()
170     {
171         return minValue;
172     }
173
174     /**
175      * Set the value of minValue.
176      * @param v Value to assign to minValue.
177      */

178     public void setMinValue(NumberKey v)
179     {
180         this.minValue = v;
181     }
182
183     /**
184      * Get the value of maxValue.
185      * @return value of maxValue.
186      */

187     public NumberKey getMaxValue()
188     {
189         return maxValue;
190     }
191
192     /**
193      * Set the value of maxValue.
194      * @param v Value to assign to maxValue.
195      */

196     public void setMaxValue(NumberKey v)
197     {
198         this.maxValue = v;
199     }
200 }
201
Popular Tags