You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dg...@apache.org on 2003/04/29 03:14:29 UTC
cvs commit: jakarta-commons/validator/src/share/org/apache/commons/validator GenericTypeValidator.java
dgraham 2003/04/28 18:14:29
Modified: validator/src/share/org/apache/commons/validator
GenericTypeValidator.java
Log:
General code cleanup.
Revision Changes Path
1.6 +92 -77 jakarta-commons/validator/src/share/org/apache/commons/validator/GenericTypeValidator.java
Index: GenericTypeValidator.java
===================================================================
RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/GenericTypeValidator.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- GenericTypeValidator.java 11 Oct 2002 01:16:36 -0000 1.5
+++ GenericTypeValidator.java 29 Apr 2003 01:14:29 -0000 1.6
@@ -7,7 +7,7 @@
*
* The Apache Software License, Version 1.1
*
- * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
+ * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -69,29 +69,29 @@
import java.text.SimpleDateFormat;
import java.text.ParseException;
-
/**
- * <p>This class contains basic methods for
- * performing validations that return the
- * correctly typed class based on the
- * validation performed.</p>
+ * <p>This class contains basic methods for performing validations that return the
+ * correctly typed class based on the validation performed.</p>
*
* @author David Winterfeldt
+ * @author <a href="mailto:husted@apache.org">Ted Husted</a>
+ * @author David Graham
* @version $Revision$ $Date$
-*/
+ */
public class GenericTypeValidator implements Serializable {
/**
* <p>Checks if the value can safely be converted to a byte primitive.</p>
*
* @param value The value validation is being performed on.
- */
+ */
public static Byte formatByte(String value) {
Byte result = null;
try {
result = new Byte(value);
} catch (Exception e) {
+ // TODO Why are we catching Exception? Should this be NumberFormatException?
}
return result;
@@ -101,13 +101,14 @@
* <p>Checks if the value can safely be converted to a short primitive.</p>
*
* @param value The value validation is being performed on.
- */
+ */
public static Short formatShort(String value) {
Short result = null;
try {
result = new Short(value);
} catch (Exception e) {
+ // TODO Why are we catching Exception? Should this be NumberFormatException?
}
return result;
@@ -117,29 +118,31 @@
* <p>Checks if the value can safely be converted to a int primitive.</p>
*
* @param value The value validation is being performed on.
- */
- public static Integer formatInt(String value) {
- Integer result = null;
-
- try {
- result = new Integer(value);
- } catch (Exception e) {
- }
-
- return result;
- }
+ */
+ public static Integer formatInt(String value) {
+ Integer result = null;
+
+ try {
+ result = new Integer(value);
+ } catch (Exception e) {
+ // TODO Why are we catching Exception? Should this be NumberFormatException?
+ }
+
+ return result;
+ }
/**
* <p>Checks if the value can safely be converted to a long primitive.</p>
*
* @param value The value validation is being performed on.
- */
+ */
public static Long formatLong(String value) {
Long result = null;
try {
result = new Long(value);
} catch (Exception e) {
+ // TODO Why are we catching Exception? Should this be NumberFormatException?
}
return result;
@@ -149,13 +152,14 @@
* <p>Checks if the value can safely be converted to a float primitive.</p>
*
* @param value The value validation is being performed on.
- */
+ */
public static Float formatFloat(String value) {
Float result = null;
try {
result = new Float(value);
} catch (Exception e) {
+ // TODO Why are we catching Exception? Should this be NumberFormatException?
}
return result;
@@ -165,13 +169,14 @@
* <p>Checks if the value can safely be converted to a double primitive.</p>
*
* @param value The value validation is being performed on.
- */
+ */
public static Double formatDouble(String value) {
Double result = null;
try {
result = new Double(value);
} catch (Exception e) {
+ // TODO Why are we catching Exception? Should this be NumberFormatException?
}
return result;
@@ -184,27 +189,35 @@
*
* @param value The value validation is being performed on.
* @param Locale The Locale to use to parse the date (system default if null)
- */
+ */
public static Date formatDate(String value, Locale locale) {
- Date date = null;
+ Date date = null;
- if (value != null) {
- try {
- DateFormat formatter = null;
- if (locale != null) {
- formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
- } else {
- formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
- }
-
- formatter.setLenient(false);
-
- date = formatter.parse(value);
- } catch (ParseException e) {
- }
- }
+ if (value == null) {
+ return null;
+ }
+
+ try {
+ DateFormat formatter = null;
+ if (locale != null) {
+ formatter =
+ DateFormat.getDateInstance(DateFormat.SHORT, locale);
+ } else {
+ formatter =
+ DateFormat.getDateInstance(
+ DateFormat.SHORT,
+ Locale.getDefault());
+ }
+
+ formatter.setLenient(false);
+
+ date = formatter.parse(value);
+ } catch (ParseException e) {
+ // TODO Should either document why exception is swallowed or
+ // not swallow it.
+ }
- return date;
+ return date;
}
/**
@@ -217,47 +230,49 @@
* @param value The value validation is being performed on.
* @param datePattern The pattern passed to <code>SimpleDateFormat</code>.
* @param strict Whether or not to have an exact match of the datePattern.
- */
+ */
public static Date formatDate(String value, String datePattern, boolean strict) {
- Date date = null;
+ Date date = null;
- if (value != null && datePattern != null && datePattern.length() > 0) {
- try {
- SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
- formatter.setLenient(false);
-
- date = formatter.parse(value);
-
- if (strict) {
- if (datePattern.length() != value.length()) {
- date = null;
- }
- }
- } catch (ParseException e) {
- }
- }
+ if (value == null
+ || datePattern == null
+ || datePattern.length() == 0) {
+ return null;
+ }
+
+ try {
+ SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
+ formatter.setLenient(false);
+
+ date = formatter.parse(value);
+
+ if (strict) {
+ if (datePattern.length() != value.length()) {
+ date = null;
+ }
+ }
+ } catch (ParseException e) {
+ // TODO Should either document why exception is swallowed or
+ // not swallow it.
+ }
- return date;
+ return date;
}
/**
* <p>Checks if the field is a valid credit card number.</p>
- * <p>Translated to Java by Ted Husted (<a href="mailto:husted@apache.org">husted@apache.org</a>).<br>
- * Reference Sean M. Burke's script at http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl</p>
+ * Reference Sean M. Burke's script at http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl</p>
*
- * @param value The value validation is being performed on.
- */
+ * @param value The value validation is being performed on.
+ */
public static Long formatCreditCard(String value) {
- Long result = null;
-
- try {
- if (GenericValidator.validateCreditCardLuhnCheck(value) && GenericValidator.validateCreditCardPrefixCheck(value)) {
- result = new Long(value);
- }
- } catch (Exception e) {
- }
-
- return result;
+ Long result = null;
+
+ if (GenericValidator.isCreditCard(value)) {
+ result = new Long(value);
+ }
+
+ return result;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org