View Javadoc

1   package org.apache.turbine.services.intake.validator;
2   
3   /*
4    * Copyright 2001-2005 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;
20  
21  import org.apache.torque.om.NumberKey;
22  
23  /***
24   * Validates numbers with the following constraints in addition to those
25   * listed in 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 BigDecimal.MAX_VALUE</td>
32   * <td>&nbsp;</td></tr>
33   * <tr><td>notANumberMessage</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:quintonm@bellsouth.net">Quinton McCombs</a>
39   * @version $Id: NumberKeyValidator.java 264148 2005-08-29 14:21:04Z henning $
40   * @deprecated No replacement
41   */
42  public class NumberKeyValidator
43          extends NumberValidator
44  {
45      private static String INVALID_NUMBER = "Entry was not valid.";
46  
47      private NumberKey minValue;
48      private NumberKey maxValue;
49  
50      public NumberKeyValidator(Map paramMap)
51              throws InvalidMaskException
52      {
53          this();
54          init(paramMap);
55      }
56  
57      public NumberKeyValidator()
58      {
59          // sets the default invalid number message
60          super();
61      }
62  
63      protected void doInit(Map paramMap)
64      {
65          minValue = null;
66          maxValue = null;
67  
68          Constraint constraint = (Constraint) paramMap.get(MIN_VALUE_RULE_NAME);
69          if (constraint != null)
70          {
71              String param = constraint.getValue();
72              minValue = new NumberKey(param);
73              minValueMessage = constraint.getMessage();
74          }
75  
76          constraint = (Constraint) paramMap.get(MAX_VALUE_RULE_NAME);
77          if (constraint != null)
78          {
79              String param = constraint.getValue();
80              maxValue = new NumberKey(param);
81              maxValueMessage = constraint.getMessage();
82          }
83      }
84  
85      protected String getDefaultInvalidNumberMessage()
86      {
87          return INVALID_NUMBER;
88      }
89  
90      /***
91       * Determine whether a testValue meets the criteria specified
92       * in the constraints defined for this validator
93       *
94       * @param testValue a <code>String</code> to be tested
95       * @exception ValidationException containing an error message if the
96       * testValue did not pass the validation tests.
97       */
98      public void assertValidity(String testValue)
99              throws ValidationException
100     {
101         NumberKey nk = null;
102         try
103         {
104             nk = new NumberKey(testValue);
105         }
106         catch (RuntimeException e)
107         {
108             errorMessage = invalidNumberMessage;
109             throw new ValidationException(invalidNumberMessage);
110         }
111         if (minValue != null && nk.compareTo(minValue) < 0)
112         {
113             errorMessage = minValueMessage;
114             throw new ValidationException(minValueMessage);
115         }
116         if (maxValue != null && nk.compareTo(maxValue) > 0)
117         {
118             errorMessage = maxValueMessage;
119             throw new ValidationException(maxValueMessage);
120         }
121     }
122 
123 
124     // ************************************************************
125     // **                Bean accessor methods                   **
126     // ************************************************************
127 
128     /***
129      * Get the value of minValue.
130      *
131      * @return value of minValue.
132      */
133     public NumberKey getMinValue()
134     {
135         return minValue;
136     }
137 
138     /***
139      * Set the value of minValue.
140      *
141      * @param minValue Value to assign to minValue.
142      */
143     public void setMinValue(NumberKey minValue)
144     {
145         this.minValue = minValue;
146     }
147 
148     /***
149      * Get the value of maxValue.
150      *
151      * @return value of maxValue.
152      */
153     public NumberKey getMaxValue()
154     {
155         return maxValue;
156     }
157 
158     /***
159      * Set the value of maxValue.
160      *
161      * @param maxValue Value to assign to maxValue.
162      */
163     public void setMaxValue(NumberKey maxValue)
164     {
165         this.maxValue = maxValue;
166     }
167 }