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.commons.lang.StringUtils;
25  
26  /***
27   * Validates Shorts with the following constraints in addition to those
28   * listed in NumberValidator and 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 Short.MIN_VALUE</td>
33   * <td>&nbsp;</td></tr>
34   * <tr><td>maxValue</td><td>less than Short.MAX_VALUE</td>
35   * <td>&nbsp;</td></tr>
36   * <tr><td>invalidNumberMessage</td><td>Some text</td>
37   * <td>Entry was not a valid Short</td></tr>
38   * </table>
39   *
40   * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
41   * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
42   * @version $Id: ShortValidator.java 534527 2007-05-02 16:10:59Z tv $
43   */
44  public class ShortValidator
45          extends NumberValidator
46  {
47      /* Init the minValue to that for a Short */
48      private short minValue = Short.MIN_VALUE;
49  
50      /* Init the maxValue to that for a Short */
51      private short maxValue = Short.MAX_VALUE;
52  
53      /***
54       * Constructor to use when initialising Object
55       *
56       * @param paramMap
57       * @throws InvalidMaskException
58       */
59      public ShortValidator(Map paramMap)
60              throws InvalidMaskException
61      {
62          invalidNumberMessage = "Entry was not a valid Short";
63          init(paramMap);
64      }
65  
66      /***
67       * Default Constructor
68       */
69      public ShortValidator()
70      {
71      }
72  
73      /***
74       * Method to initialise Object
75       *
76       * @param paramMap
77       * @throws InvalidMaskException
78       */
79      public void init(Map paramMap)
80              throws InvalidMaskException
81      {
82          super.init(paramMap);
83  
84          Constraint constraint = (Constraint) paramMap.get(MIN_VALUE_RULE_NAME);
85          if (constraint != null)
86          {
87              String param = constraint.getValue();
88              minValue = Short.parseShort(param);
89              minValueMessage = constraint.getMessage();
90          }
91  
92          constraint = (Constraint) paramMap.get(MAX_VALUE_RULE_NAME);
93          if (constraint != null)
94          {
95              String param = constraint.getValue();
96              maxValue = Short.parseShort(param);
97              maxValueMessage = constraint.getMessage();
98          }
99      }
100 
101     /***
102      * Determine whether a testValue meets the criteria specified
103      * in the constraints defined for this validator
104      *
105      * @param testValue a <code>String</code> to be tested
106      * @exception ValidationException containing an error message if the
107      * testValue did not pass the validation tests.
108      */
109     public void assertValidity(String testValue)
110             throws ValidationException
111     {
112         super.assertValidity(testValue);
113 
114         if (required || StringUtils.isNotEmpty(testValue))
115         {
116             short s = 0;
117             try
118             {
119                 s = Short.parseShort(testValue);
120             }
121             catch (RuntimeException e)
122             {
123                 errorMessage = invalidNumberMessage;
124                 throw new ValidationException(invalidNumberMessage);
125             }
126 
127             if (s < minValue)
128             {
129                 errorMessage = minValueMessage;
130                 throw new ValidationException(minValueMessage);
131             }
132             if (s > maxValue)
133             {
134                 errorMessage = maxValueMessage;
135                 throw new ValidationException(maxValueMessage);
136             }
137         }
138     }
139 
140 
141     // ************************************************************
142     // **                Bean accessor methods                   **
143     // ************************************************************
144 
145     /***
146      * Get the value of minValue.
147      *
148      * @return value of minValue.
149      */
150     public short getMinValue()
151     {
152         return minValue;
153     }
154 
155     /***
156      * Set the value of minValue.
157      *
158      * @param minValue  Value to assign to minValue.
159      */
160     public void setMinValue(short minValue)
161     {
162         this.minValue = minValue;
163     }
164 
165     /***
166      * Get the value of maxValue.
167      *
168      * @return value of maxValue.
169      */
170     public short getMaxValue()
171     {
172         return maxValue;
173     }
174 
175     /***
176      * Set the value of maxValue.
177      *
178      * @param maxValue  Value to assign to maxValue.
179      */
180     public void setMaxValue(short maxValue)
181     {
182         this.maxValue = maxValue;
183     }
184 }