1. /*
  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 $
  3. * $Revision: 1.13 $
  4. * $Date: 2004/02/21 17:10:29 $
  5. *
  6. * ====================================================================
  7. * Copyright 2001-2004 The Apache Software Foundation
  8. *
  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
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  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.
  20. */
  21. package org.apache.commons.validator;
  22. import java.io.Serializable;
  23. import java.util.Date;
  24. import java.util.Locale;
  25. import java.text.DateFormat;
  26. import java.text.SimpleDateFormat;
  27. import java.text.ParseException;
  28. import org.apache.commons.logging.Log;
  29. import org.apache.commons.logging.LogFactory;
  30. /**
  31. * This class contains basic methods for performing validations that return the
  32. * correctly typed class based on the validation performed.
  33. */
  34. public class GenericTypeValidator implements Serializable {
  35. /*
  36. * Logger.
  37. */
  38. private static Log log = LogFactory.getLog(GenericTypeValidator.class);
  39. /**
  40. * Checks if the value can safely be converted to a byte primitive.
  41. *
  42. * @param value The value validation is being performed on.
  43. */
  44. public static Byte formatByte(String value) {
  45. if (value == null) {
  46. return null;
  47. }
  48. try {
  49. return new Byte(value);
  50. } catch(NumberFormatException e) {
  51. return null;
  52. }
  53. }
  54. /**
  55. * Checks if the value can safely be converted to a short primitive.
  56. *
  57. * @param value The value validation is being performed on.
  58. */
  59. public static Short formatShort(String value) {
  60. if (value == null) {
  61. return null;
  62. }
  63. try {
  64. return new Short(value);
  65. } catch(NumberFormatException e) {
  66. return null;
  67. }
  68. }
  69. /**
  70. * Checks if the value can safely be converted to a int primitive.
  71. *
  72. * @param value The value validation is being performed on.
  73. */
  74. public static Integer formatInt(String value) {
  75. if (value == null) {
  76. return null;
  77. }
  78. try {
  79. return new Integer(value);
  80. } catch(NumberFormatException e) {
  81. return null;
  82. }
  83. }
  84. /**
  85. * Checks if the value can safely be converted to a long primitive.
  86. *
  87. * @param value The value validation is being performed on.
  88. */
  89. public static Long formatLong(String value) {
  90. if (value == null) {
  91. return null;
  92. }
  93. try {
  94. return new Long(value);
  95. } catch(NumberFormatException e) {
  96. return null;
  97. }
  98. }
  99. /**
  100. * Checks if the value can safely be converted to a float primitive.
  101. *
  102. * @param value The value validation is being performed on.
  103. */
  104. public static Float formatFloat(String value) {
  105. if (value == null) {
  106. return null;
  107. }
  108. try {
  109. return new Float(value);
  110. } catch(NumberFormatException e) {
  111. return null;
  112. }
  113. }
  114. /**
  115. * Checks if the value can safely be converted to a double primitive.
  116. *
  117. * @param value The value validation is being performed on.
  118. */
  119. public static Double formatDouble(String value) {
  120. if (value == null) {
  121. return null;
  122. }
  123. try {
  124. return new Double(value);
  125. } catch(NumberFormatException e) {
  126. return null;
  127. }
  128. }
  129. /**
  130. * <p>Checks if the field is a valid date. The <code>Locale</code> is
  131. * used with <code>java.text.DateFormat</code>. The setLenient method
  132. * is set to <code>false</code> for all.</p>
  133. *
  134. * @param value The value validation is being performed on.
  135. * @param locale The Locale to use to parse the date (system default if null)
  136. */
  137. public static Date formatDate(String value, Locale locale) {
  138. Date date = null;
  139. if (value == null) {
  140. return null;
  141. }
  142. try {
  143. DateFormat formatter = null;
  144. if (locale != null) {
  145. formatter =
  146. DateFormat.getDateInstance(DateFormat.SHORT, locale);
  147. } else {
  148. formatter =
  149. DateFormat.getDateInstance(
  150. DateFormat.SHORT,
  151. Locale.getDefault());
  152. }
  153. formatter.setLenient(false);
  154. date = formatter.parse(value);
  155. } catch(ParseException e) {
  156. // Bad date so return null
  157. log.warn(value, e);
  158. }
  159. return date;
  160. }
  161. /**
  162. * <p>Checks if the field is a valid date. The pattern is used with
  163. * <code>java.text.SimpleDateFormat</code>. If strict is true, then the
  164. * length will be checked so '2/12/1999' will not pass validation with
  165. * the format 'MM/dd/yyyy' because the month isn't two digits.
  166. * The setLenient method is set to <code>false</code> for all.</p>
  167. *
  168. * @param value The value validation is being performed on.
  169. * @param datePattern The pattern passed to <code>SimpleDateFormat</code>.
  170. * @param strict Whether or not to have an exact match of the datePattern.
  171. */
  172. public static Date formatDate(String value, String datePattern, boolean strict) {
  173. Date date = null;
  174. if (value == null
  175. || datePattern == null
  176. || datePattern.length() == 0) {
  177. return null;
  178. }
  179. try {
  180. SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
  181. formatter.setLenient(false);
  182. date = formatter.parse(value);
  183. if (strict) {
  184. if (datePattern.length() != value.length()) {
  185. date = null;
  186. }
  187. }
  188. } catch(ParseException e) {
  189. // Bad date so return null
  190. log.warn(value, e);
  191. }
  192. return date;
  193. }
  194. /**
  195. * <p>Checks if the field is a valid credit card number.</p>
  196. * <p>Reference Sean M. Burke's
  197. * <a href="http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl">script</a>.</p>
  198. *
  199. * @param value The value validation is being performed on.
  200. */
  201. public static Long formatCreditCard(String value) {
  202. return GenericValidator.isCreditCard(value) ? new Long(value) : null;
  203. }
  204. }