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