2
* $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/GenericTypeValidator.java,v 1.13 2004/02/21 17:10:29 rleland Exp $
4
* $Date: 2004/02/21 17:10:29 $
6
* ====================================================================
7
* Copyright 2001-2004 The Apache Software Foundation
9
* Licensed under the Apache License, Version 2.0 (the "License");
10
* you may not use this file except in compliance with the License.
11
* You may obtain a copy of the License at
13
* http://www.apache.org/licenses/LICENSE-2.0
15
* Unless required by applicable law or agreed to in writing, software
16
* distributed under the License is distributed on an "AS IS" BASIS,
17
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
* See the License for the specific language governing permissions and
19
* limitations under the License.
22
package org.apache.commons.validator;
24
import java.io.Serializable;
25
import java.util.Date;
26
import java.util.Locale;
27
import java.text.DateFormat;
28
import java.text.SimpleDateFormat;
29
import java.text.ParseException;
30
import org.apache.commons.logging.Log;
31
import org.apache.commons.logging.LogFactory;
34
* This class contains basic methods for performing validations that return the
35
* correctly typed class based on the validation performed.
37
public class GenericTypeValidator implements Serializable {
41
private static Log log = LogFactory.getLog(GenericTypeValidator.class);
44
* Checks if the value can safely be converted to a byte primitive.
46
* @param value The value validation is being performed on.
48
public static Byte formatByte(String value) {
54
return new Byte(value);
55
} catch(NumberFormatException e) {
62
* Checks if the value can safely be converted to a short primitive.
64
* @param value The value validation is being performed on.
66
public static Short formatShort(String value) {
72
return new Short(value);
73
} catch(NumberFormatException e) {
80
* Checks if the value can safely be converted to a int primitive.
82
* @param value The value validation is being performed on.
84
public static Integer formatInt(String value) {
90
return new Integer(value);
91
} catch(NumberFormatException e) {
98
* Checks if the value can safely be converted to a long primitive.
100
* @param value The value validation is being performed on.
102
public static Long formatLong(String value) {
108
return new Long(value);
109
} catch(NumberFormatException e) {
116
* Checks if the value can safely be converted to a float primitive.
118
* @param value The value validation is being performed on.
120
public static Float formatFloat(String value) {
126
return new Float(value);
127
} catch(NumberFormatException e) {
134
* Checks if the value can safely be converted to a double primitive.
136
* @param value The value validation is being performed on.
138
public static Double formatDouble(String value) {
144
return new Double(value);
145
} catch(NumberFormatException e) {
152
* <p>Checks if the field is a valid date. The <code>Locale</code> is
153
* used with <code>java.text.DateFormat</code>. The setLenient method
154
* is set to <code>false</code> for all.</p>
156
* @param value The value validation is being performed on.
157
* @param locale The Locale to use to parse the date (system default if null)
159
public static Date formatDate(String value, Locale locale) {
167
DateFormat formatter = null;
168
if (locale != null) {
170
DateFormat.getDateInstance(DateFormat.SHORT, locale);
173
DateFormat.getDateInstance(
175
Locale.getDefault());
178
formatter.setLenient(false);
180
date = formatter.parse(value);
181
} catch(ParseException e) {
182
// Bad date so return null
190
* <p>Checks if the field is a valid date. The pattern is used with
191
* <code>java.text.SimpleDateFormat</code>. If strict is true, then the
192
* length will be checked so '2/12/1999' will not pass validation with
193
* the format 'MM/dd/yyyy' because the month isn't two digits.
194
* The setLenient method is set to <code>false</code> for all.</p>
196
* @param value The value validation is being performed on.
197
* @param datePattern The pattern passed to <code>SimpleDateFormat</code>.
198
* @param strict Whether or not to have an exact match of the datePattern.
200
public static Date formatDate(String value, String datePattern, boolean strict) {
204
|| datePattern == null
205
|| datePattern.length() == 0) {
210
SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
211
formatter.setLenient(false);
213
date = formatter.parse(value);
216
if (datePattern.length() != value.length()) {
220
} catch(ParseException e) {
221
// Bad date so return null
229
* <p>Checks if the field is a valid credit card number.</p>
230
* <p>Reference Sean M. Burke's
231
* <a href="http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl">script</a>.</p>
233
* @param value The value validation is being performed on.
235
public static Long formatCreditCard(String value) {
236
return GenericValidator.isCreditCard(value) ? new Long(value) : null;