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.text.NumberFormat;
23  import java.text.ParseException;
24  import java.util.Locale;
25  import java.util.Map;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.apache.turbine.services.intake.model.Field;
29  
30  /***
31   * Validates Doubles with the following constraints in addition to those
32   * listed in NumberValidator and DefaultValidator.
33   *
34   * <table>
35   * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
36   * <tr><td>minValue</td><td>greater than Double.MIN_VALUE</td>
37   * <td>&nbsp;</td></tr>
38   * <tr><td>maxValue</td><td>less than Double.MAX_VALUE</td>
39   * <td>&nbsp;</td></tr>
40   * <tr><td>invalidNumberMessage</td><td>Some text</td>
41   * <td>Entry was not a valid number</td></tr>
42   * </table>
43   *
44   * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
45   * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
46   * @author <a href="mailto:jh@byteaction.de">J&uuml;rgen Hoffmann</a>
47   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
48   * @version $Id: DoubleValidator.java 534527 2007-05-02 16:10:59Z tv $
49   */
50  public class DoubleValidator
51          extends NumberValidator
52  {
53      /* Init the minValue to that for a Double */
54      private double minValue = Double.NEGATIVE_INFINITY;
55  
56      /* Init the maxValue to that for a Double */
57      private double maxValue = Double.POSITIVE_INFINITY;
58  
59      /***
60       * Constructor to use when initialising Object
61       *
62       * @param paramMap
63       * @throws InvalidMaskException
64       */
65      public DoubleValidator(Map paramMap)
66              throws InvalidMaskException
67      {
68          invalidNumberMessage = "Entry was not a valid Double";
69          init(paramMap);
70      }
71  
72      /***
73       * Default Constructor
74       */
75      public DoubleValidator()
76      {
77      }
78  
79      /***
80       * Method to initialise Object
81       *
82       * @param paramMap
83       * @throws InvalidMaskException
84       */
85      public void init(Map paramMap)
86              throws InvalidMaskException
87      {
88          super.init(paramMap);
89  
90          Constraint constraint = (Constraint) paramMap.get(MIN_VALUE_RULE_NAME);
91          if (constraint != null)
92          {
93              String param = constraint.getValue();
94              minValue = Double.parseDouble(param);
95              minValueMessage = constraint.getMessage();
96          }
97  
98          constraint = (Constraint) paramMap.get(MAX_VALUE_RULE_NAME);
99          if (constraint != null)
100         {
101             String param = constraint.getValue();
102             maxValue = Double.parseDouble(param);
103             maxValueMessage = constraint.getMessage();
104         }
105     }
106 
107     /***
108      * Determine whether a field meets the criteria specified
109      * in the constraints defined for this validator
110      *
111      * @param field a <code>Field</code> to be tested
112      * @exception ValidationException containing an error message if the
113      * testValue did not pass the validation tests.
114      */
115     public void assertValidity(Field field)
116             throws ValidationException
117     {
118         Locale locale = field.getLocale();
119 
120         if (field.isMultiValued())
121         {
122             String[] stringValues = (String[])field.getTestValue();
123 
124             for (int i = 0; i < stringValues.length; i++)
125             {
126                 assertValidity(stringValues[i], locale);
127             }
128         }
129         else
130         {
131             assertValidity((String)field.getTestValue(), locale);
132         }
133     }
134 
135     /***
136      * Determine whether a testValue meets the criteria specified
137      * in the constraints defined for this validator
138      *
139      * @param testValue a <code>String</code> to be tested
140      * @param locale the Locale of the associated field
141      * @exception ValidationException containing an error message if the
142      * testValue did not pass the validation tests.
143      */
144     public void assertValidity(String testValue, Locale locale)
145             throws ValidationException
146     {
147         super.assertValidity(testValue);
148 
149         double d = 0.0D;
150 
151         if (required || StringUtils.isNotEmpty(testValue))
152         {
153             NumberFormat nf = NumberFormat.getInstance(locale);
154 
155             try
156             {
157                 d = nf.parse(testValue).doubleValue();
158             }
159             catch (ParseException e)
160             {
161                 errorMessage = invalidNumberMessage;
162                 throw new ValidationException(invalidNumberMessage);
163             }
164 
165             if (d < minValue)
166             {
167                 errorMessage = minValueMessage;
168                 throw new ValidationException(minValueMessage);
169             }
170 
171             if (d > maxValue)
172             {
173                 errorMessage = maxValueMessage;
174                 throw new ValidationException(maxValueMessage);
175             }
176         }
177     }
178 
179 
180     // ************************************************************
181     // **                Bean accessor methods                   **
182     // ************************************************************
183 
184     /***
185      * Get the value of minValue.
186      *
187      * @return value of minValue.
188      */
189     public double getMinValue()
190     {
191         return minValue;
192     }
193 
194     /***
195      * Set the value of minValue.
196      *
197      * @param value  Value to assign to minValue.
198      */
199     public void setMinValue(double value)
200     {
201         this.minValue = value;
202     }
203 
204     /***
205      * Get the value of maxValue.
206      *
207      * @return value of maxValue.
208      */
209     public double getMaxValue()
210     {
211         return maxValue;
212     }
213 
214     /***
215      * Set the value of maxValue.
216      *
217      * @param value  Value to assign to maxValue.
218      */
219     public void setMaxValue(double value)
220     {
221         this.maxValue = value;
222     }
223 }