View Javadoc

1   package org.apache.turbine.services.intake.validator;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Map;
23  
24  import org.apache.torque.om.NumberKey;
25  
26  /***
27   * Validates numbers with the following constraints in addition to those
28   * listed in DefaultValidator.
29   *
30   * <table>
31   * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
32   * <tr><td>minValue</td><td>greater than Integer.MIN_VALUE</td>
33   * <td>&nbsp;</td></tr>
34   * <tr><td>maxValue</td><td>less than BigDecimal.MAX_VALUE</td>
35   * <td>&nbsp;</td></tr>
36   * <tr><td>notANumberMessage</td><td>Some text</td>
37   * <td>Entry was not a valid number</td></tr>
38   * </table>
39   *
40   * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
41   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
42   * @version $Id: NumberKeyValidator.java 534527 2007-05-02 16:10:59Z tv $
43   * @deprecated No replacement
44   */
45  public class NumberKeyValidator
46          extends NumberValidator
47  {
48      private static String INVALID_NUMBER = "Entry was not valid.";
49  
50      private NumberKey minValue;
51      private NumberKey maxValue;
52  
53      public NumberKeyValidator(Map paramMap)
54              throws InvalidMaskException
55      {
56          this();
57          init(paramMap);
58      }
59  
60      public NumberKeyValidator()
61      {
62          // sets the default invalid number message
63          super();
64      }
65  
66      protected void doInit(Map paramMap)
67      {
68          minValue = null;
69          maxValue = null;
70  
71          Constraint constraint = (Constraint) paramMap.get(MIN_VALUE_RULE_NAME);
72          if (constraint != null)
73          {
74              String param = constraint.getValue();
75              minValue = new NumberKey(param);
76              minValueMessage = constraint.getMessage();
77          }
78  
79          constraint = (Constraint) paramMap.get(MAX_VALUE_RULE_NAME);
80          if (constraint != null)
81          {
82              String param = constraint.getValue();
83              maxValue = new NumberKey(param);
84              maxValueMessage = constraint.getMessage();
85          }
86      }
87  
88      protected String getDefaultInvalidNumberMessage()
89      {
90          return INVALID_NUMBER;
91      }
92  
93      /***
94       * Determine whether a testValue meets the criteria specified
95       * in the constraints defined for this validator
96       *
97       * @param testValue a <code>String</code> to be tested
98       * @exception ValidationException containing an error message if the
99       * testValue did not pass the validation tests.
100      */
101     public void assertValidity(String testValue)
102             throws ValidationException
103     {
104         NumberKey nk = null;
105         try
106         {
107             nk = new NumberKey(testValue);
108         }
109         catch (RuntimeException e)
110         {
111             errorMessage = invalidNumberMessage;
112             throw new ValidationException(invalidNumberMessage);
113         }
114         if (minValue != null && nk.compareTo(minValue) < 0)
115         {
116             errorMessage = minValueMessage;
117             throw new ValidationException(minValueMessage);
118         }
119         if (maxValue != null && nk.compareTo(maxValue) > 0)
120         {
121             errorMessage = maxValueMessage;
122             throw new ValidationException(maxValueMessage);
123         }
124     }
125 
126 
127     // ************************************************************
128     // **                Bean accessor methods                   **
129     // ************************************************************
130 
131     /***
132      * Get the value of minValue.
133      *
134      * @return value of minValue.
135      */
136     public NumberKey getMinValue()
137     {
138         return minValue;
139     }
140 
141     /***
142      * Set the value of minValue.
143      *
144      * @param minValue Value to assign to minValue.
145      */
146     public void setMinValue(NumberKey minValue)
147     {
148         this.minValue = minValue;
149     }
150 
151     /***
152      * Get the value of maxValue.
153      *
154      * @return value of maxValue.
155      */
156     public NumberKey getMaxValue()
157     {
158         return maxValue;
159     }
160 
161     /***
162      * Set the value of maxValue.
163      *
164      * @param maxValue Value to assign to maxValue.
165      */
166     public void setMaxValue(NumberKey maxValue)
167     {
168         this.maxValue = maxValue;
169     }
170 }