View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.validator;
18  
19  import java.util.*;
20  
21  import org.apache.commons.validator.util.ValidatorUtils;
22                                                            
23  /**                                                       
24   * Contains validation methods for different unit tests.
25   *
26   * @version $Revision$
27   */
28  public class GenericTypeValidatorImpl {
29  
30     /**
31      * Checks if the field can be successfully converted to a <code>byte</code>.
32      *
33      * @param bean The value validation is being performed on.
34      * @param field the field to use
35      * @return boolean If the field can be successfully converted 
36      * to a <code>byte</code> <code>true</code> is returned.  
37      * Otherwise <code>false</code>.
38      */
39     public static Byte validateByte(Object bean, Field field) {
40        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
41  
42        return GenericTypeValidator.formatByte(value);
43     }
44  
45     /**
46      * Checks if the field can be successfully converted to a <code>byte</code>.
47      *
48      * @param bean The value validation is being performed on.
49      * @param field the field to use
50      * @return boolean If the field can be successfully converted 
51      * to a <code>byte</code> <code>true</code> is returned.  
52      * Otherwise <code>false</code>.
53      */
54     public static Byte validateByte(Object bean, Field field, Locale locale) {
55        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
56  
57        return GenericTypeValidator.formatByte(value, locale);
58     }
59  
60     /**
61      * Checks if the field can be successfully converted to a <code>short</code>.
62      *
63      * @param bean The value validation is being performed on.
64      * @param field the field to use
65      * @return boolean If the field can be successfully converted 
66      * to a <code>short</code> <code>true</code> is returned.  
67      * Otherwise <code>false</code>.
68      */
69     public static Short validateShort(Object bean, Field field) {
70        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
71  
72        return GenericTypeValidator.formatShort(value);
73     }
74  
75     /**
76      * Checks if the field can be successfully converted to a <code>short</code>.
77      *
78      * @param bean The value validation is being performed on.
79      * @param field the field to use
80      * @return boolean If the field can be successfully converted 
81      * to a <code>short</code> <code>true</code> is returned.  
82      * Otherwise <code>false</code>.
83      */
84     public static Short validateShort(Object bean, Field field, Locale locale) {
85        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
86  
87        return GenericTypeValidator.formatShort(value, locale);
88     }
89  
90     /**
91      * Checks if the field can be successfully converted to a <code>int</code>.
92      *
93      * @param bean The value validation is being performed on.
94      * @param field the field to use
95      * @return boolean If the field can be successfully converted 
96      * to a <code>int</code> <code>true</code> is returned.  
97      * Otherwise <code>false</code>.
98      */
99     public static Integer validateInt(Object bean, Field field) {
100       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
101 
102       return GenericTypeValidator.formatInt(value);
103    }
104 
105    /**
106     * Checks if the field can be successfully converted to a <code>int</code>.
107     *
108     * @param bean The value validation is being performed on.
109     * @param field the field to use
110     * @return boolean If the field can be successfully converted 
111     * to a <code>int</code> <code>true</code> is returned.  
112     * Otherwise <code>false</code>.
113     */
114    public static Integer validateInt(Object bean, Field field, Locale locale) {
115       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
116 
117       return GenericTypeValidator.formatInt(value, locale);
118    }
119 
120    /**
121     * Checks if the field can be successfully converted to a <code>long</code>.
122     *
123     * @param bean The value validation is being performed on.
124     * @param field the field to use
125     * @return boolean If the field can be successfully converted 
126     * to a <code>long</code> <code>true</code> is returned.  
127     * Otherwise <code>false</code>.
128     */
129    public static Long validateLong(Object bean, Field field) {
130       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
131 
132       return GenericTypeValidator.formatLong(value);
133    }
134 
135    /**
136     * Checks if the field can be successfully converted to a <code>long</code>.
137     *
138     * @param bean The value validation is being performed on.
139     * @param field the field to use
140     * @return boolean If the field can be successfully converted 
141     * to a <code>long</code> <code>true</code> is returned.  
142     * Otherwise <code>false</code>.
143     */
144    public static Long validateLong(Object bean, Field field, Locale locale) {
145       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
146 
147       return GenericTypeValidator.formatLong(value, locale);
148    }
149 
150    /**
151     * Checks if the field can be successfully converted to a <code>float</code>.
152     *
153     * @param bean The value validation is being performed on.
154     * @param field the field to use
155     * @return boolean If the field can be successfully converted 
156     * to a <code>float</code> <code>true</code> is returned.  
157     * Otherwise <code>false</code>.
158     */
159    public static Float validateFloat(Object bean, Field field) {
160       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
161 
162       return GenericTypeValidator.formatFloat(value);
163    }
164    
165    /**
166     * Checks if the field can be successfully converted to a <code>float</code>.
167     *
168     * @param bean The value validation is being performed on.
169     * @param field the field to use
170     * @return boolean If the field can be successfully converted 
171     * to a <code>float</code> <code>true</code> is returned.  
172     * Otherwise <code>false</code>.
173     */
174    public static Float validateFloat(Object bean, Field field, Locale locale) {
175       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
176 
177       return GenericTypeValidator.formatFloat(value, locale);
178    }
179    
180    /**
181     * Checks if the field can be successfully converted to a <code>double</code>.
182     *
183     * @param bean The value validation is being performed on.
184     * @param field the field to use
185     * @return boolean If the field can be successfully converted 
186     * to a <code>double</code> <code>true</code> is returned.  
187     * Otherwise <code>false</code>.
188     */
189    public static Double validateDouble(Object bean, Field field) {
190       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
191 
192       return GenericTypeValidator.formatDouble(value);
193    }
194    
195    /**
196     * Checks if the field can be successfully converted to a <code>double</code>.
197     *
198     * @param bean The value validation is being performed on.
199     * @param field the field to use
200     * @return boolean If the field can be successfully converted 
201     * to a <code>double</code> <code>true</code> is returned.  
202     * Otherwise <code>false</code>.
203     */
204    public static Double validateDouble(Object bean, Field field, Locale locale) {
205       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
206 
207       return GenericTypeValidator.formatDouble(value, locale);
208    }
209    
210    /**
211     * Checks if the field can be successfully converted to a <code>date</code>.
212     *
213     * @param bean The value validation is being performed on.
214     * @param field the field to use
215     * @return boolean If the field can be successfully converted 
216     * to a <code>date</code> <code>true</code> is returned.  
217     * Otherwise <code>false</code>.
218     */
219    public static Date validateDate(Object bean, Field field, Locale locale) {
220       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
221 
222       return GenericTypeValidator.formatDate(value, locale);
223    }
224    
225    /**
226     * Checks if the field can be successfully converted to a <code>date</code>.
227     *
228     * @param bean The value validation is being performed on.
229     * @param field the field to use
230     * @return boolean If the field can be successfully converted 
231     * to a <code>date</code> <code>true</code> is returned.  
232     * Otherwise <code>false</code>.
233     */
234    public static Date validateDate(Object bean, Field field) {
235       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
236       String datePattern = field.getVarValue("datePattern");
237       String datePatternStrict = field.getVarValue("datePatternStrict");
238       
239       Date result = null;
240       if (datePattern != null && datePattern.length() > 0) {
241             result = GenericTypeValidator.formatDate(value, datePattern, false);
242         } else if (datePatternStrict != null && datePatternStrict.length() > 0) {
243             result = GenericTypeValidator.formatDate(value, datePatternStrict, true);
244         } 
245 
246       return result;
247    }
248 }