1. /*
  2. * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/GenericValidator.java,v 1.29 2004/02/21 17:10:29 rleland Exp $
  3. * $Revision: 1.29 $
  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.Locale;
  24. import org.apache.oro.text.perl.Perl5Util;
  25. /**
  26. * This class contains basic methods for performing validations.
  27. */
  28. public class GenericValidator implements Serializable {
  29. /**
  30. * Delimiter to put around a regular expression following Perl 5 syntax.
  31. * @deprecated Use ValidatorUtils.REGEXP_DELIMITER instead.
  32. */
  33. public final static String REGEXP_DELIM = ValidatorUtil.REGEXP_DELIMITER;
  34. /**
  35. * UrlValidator used in wrapper method.
  36. */
  37. private static final UrlValidator urlValidator = new UrlValidator();
  38. /**
  39. * CreditCardValidator used in wrapper method.
  40. */
  41. private static final CreditCardValidator creditCardValidator =
  42. new CreditCardValidator();
  43. /**
  44. * <p>Checks if the field isn't null and length of the field is greater than zero not
  45. * including whitespace.</p>
  46. *
  47. * @param value The value validation is being performed on.
  48. */
  49. public static boolean isBlankOrNull(String value) {
  50. return ((value == null) || (value.trim().length() == 0));
  51. }
  52. /**
  53. * <p>Checks if the value matches the regular expression.</p>
  54. *
  55. * @param value The value validation is being performed on.
  56. * @param regexp The regular expression.
  57. */
  58. public static boolean matchRegexp(String value, String regexp) {
  59. if (regexp == null || regexp.length() <= 0) {
  60. return false;
  61. }
  62. Perl5Util matcher = new Perl5Util();
  63. return matcher.match("/" + regexp + "/", value);
  64. }
  65. /**
  66. * <p>Checks if the value can safely be converted to a byte primitive.</p>
  67. *
  68. * @param value The value validation is being performed on.
  69. */
  70. public static boolean isByte(String value) {
  71. return (GenericTypeValidator.formatByte(value) != null);
  72. }
  73. /**
  74. * <p>Checks if the value can safely be converted to a short primitive.</p>
  75. *
  76. * @param value The value validation is being performed on.
  77. */
  78. public static boolean isShort(String value) {
  79. return (GenericTypeValidator.formatShort(value) != null);
  80. }
  81. /**
  82. * <p>Checks if the value can safely be converted to a int primitive.</p>
  83. *
  84. * @param value The value validation is being performed on.
  85. */
  86. public static boolean isInt(String value) {
  87. return (GenericTypeValidator.formatInt(value) != null);
  88. }
  89. /**
  90. * <p>Checks if the value can safely be converted to a long primitive.</p>
  91. *
  92. * @param value The value validation is being performed on.
  93. */
  94. public static boolean isLong(String value) {
  95. return (GenericTypeValidator.formatLong(value) != null);
  96. }
  97. /**
  98. * <p>Checks if the value can safely be converted to a float primitive.</p>
  99. *
  100. * @param value The value validation is being performed on.
  101. */
  102. public static boolean isFloat(String value) {
  103. return (GenericTypeValidator.formatFloat(value) != null);
  104. }
  105. /**
  106. * <p>Checks if the value can safely be converted to a double primitive.</p>
  107. *
  108. * @param value The value validation is being performed on.
  109. */
  110. public static boolean isDouble(String value) {
  111. return (GenericTypeValidator.formatDouble(value) != null);
  112. }
  113. /**
  114. * <p>Checks if the field is a valid date. The <code>Locale</code> is
  115. * used with <code>java.text.DateFormat</code>. The setLenient method
  116. * is set to <code>false</code> for all.</p>
  117. *
  118. * @param value The value validation is being performed on.
  119. * @param locale The locale to use for the date format, defaults to the default
  120. * system default if null.
  121. */
  122. public static boolean isDate(String value, Locale locale) {
  123. return DateValidator.getInstance().isValid(value, locale);
  124. }
  125. /**
  126. * <p>Checks if the field is a valid date. The pattern is used with
  127. * <code>java.text.SimpleDateFormat</code>. If strict is true, then the
  128. * length will be checked so '2/12/1999' will not pass validation with
  129. * the format 'MM/dd/yyyy' because the month isn't two digits.
  130. * The setLenient method is set to <code>false</code> for all.</p>
  131. *
  132. * @param value The value validation is being performed on.
  133. * @param datePattern The pattern passed to <code>SimpleDateFormat</code>.
  134. * @param strict Whether or not to have an exact match of the datePattern.
  135. */
  136. public static boolean isDate(String value, String datePattern, boolean strict) {
  137. return DateValidator.getInstance().isValid(value, datePattern, strict);
  138. }
  139. /**
  140. * <p>Checks if a value is within a range (min & max specified
  141. * in the vars attribute).</p>
  142. *
  143. * @param value The value validation is being performed on.
  144. * @param min The minimum value of the range.
  145. * @param max The maximum value of the range.
  146. */
  147. public static boolean isInRange(byte value, byte min, byte max) {
  148. return ((value >= min) && (value <= max));
  149. }
  150. /**
  151. * <p>Checks if a value is within a range (min & max specified
  152. * in the vars attribute).</p>
  153. *
  154. * @param value The value validation is being performed on.
  155. * @param min The minimum value of the range.
  156. * @param max The maximum value of the range.
  157. */
  158. public static boolean isInRange(int value, int min, int max) {
  159. return ((value >= min) && (value <= max));
  160. }
  161. /**
  162. * <p>Checks if a value is within a range (min & max specified
  163. * in the vars attribute).</p>
  164. *
  165. * @param value The value validation is being performed on.
  166. * @param min The minimum value of the range.
  167. * @param max The maximum value of the range.
  168. */
  169. public static boolean isInRange(float value, float min, float max) {
  170. return ((value >= min) && (value <= max));
  171. }
  172. /**
  173. * <p>Checks if a value is within a range (min & max specified
  174. * in the vars attribute).</p>
  175. *
  176. * @param value The value validation is being performed on.
  177. * @param min The minimum value of the range.
  178. * @param max The maximum value of the range.
  179. */
  180. public static boolean isInRange(short value, short min, short max) {
  181. return ((value >= min) && (value <= max));
  182. }
  183. /**
  184. * <p>Checks if a value is within a range (min & max specified
  185. * in the vars attribute).</p>
  186. *
  187. * @param value The value validation is being performed on.
  188. * @param min The minimum value of the range.
  189. * @param max The maximum value of the range.
  190. */
  191. public static boolean isInRange(long value, long min, long max) {
  192. return ((value >= min) && (value <= max));
  193. }
  194. /**
  195. * <p>Checks if a value is within a range (min & max specified
  196. * in the vars attribute).</p>
  197. *
  198. * @param value The value validation is being performed on.
  199. * @param min The minimum value of the range.
  200. * @param max The maximum value of the range.
  201. */
  202. public static boolean isInRange(double value, double min, double max) {
  203. return ((value >= min) && (value <= max));
  204. }
  205. /**
  206. * Checks if the field is a valid credit card number.
  207. * @param value The value validation is being performed on.
  208. */
  209. public static boolean isCreditCard(String value) {
  210. return creditCardValidator.isValid(value);
  211. }
  212. /**
  213. * Checks for a valid credit card number.
  214. *
  215. * @param cardNumber Credit Card Number.
  216. * @deprecated This functionality has moved to CreditCardValidator.
  217. */
  218. protected static boolean validateCreditCardLuhnCheck(String cardNumber) {
  219. return (new CreditCardValidator()).luhnCheck(cardNumber);
  220. }
  221. /**
  222. * Checks for a valid credit card number.
  223. *
  224. * @param cardNumber Credit Card Number.
  225. * @deprecated This functionality has move to CreditCardValidator.
  226. */
  227. protected boolean validateCreditCardPrefixCheck(String cardNumber) {
  228. return (new CreditCardValidator()).isValidPrefix(cardNumber);
  229. }
  230. /**
  231. * <p>Checks if a field has a valid e-mail address.</p>
  232. *
  233. * @param value The value validation is being performed on.
  234. */
  235. public static boolean isEmail(String value) {
  236. return EmailValidator.getInstance().isValid(value);
  237. }
  238. /**
  239. * <p>Checks if a field is a valid url address.</p>
  240. * If you need to modify what is considered valid then
  241. * consider using the UrlValidator directly.
  242. *
  243. * @param value The value validation is being performed on.
  244. */
  245. public static boolean isUrl(String value) {
  246. return urlValidator.isValid(value);
  247. }
  248. /**
  249. * <p>Checks if the value's length is less than or equal to the max.</p>
  250. *
  251. * @param value The value validation is being performed on.
  252. * @param max The maximum length.
  253. */
  254. public static boolean maxLength(String value, int max) {
  255. return (value.length() <= max);
  256. }
  257. /**
  258. * <p>Checks if the value's length is greater than or equal to the min.</p>
  259. *
  260. * @param value The value validation is being performed on.
  261. * @param min The minimum length.
  262. */
  263. public static boolean minLength(String value, int min) {
  264. return (value.length() >= min);
  265. }
  266. /**
  267. * Adds a '/' on either side of the regular expression.
  268. * @deprecated use ValidatorUtils.getDelimitedRegExp() instead.
  269. */
  270. protected static String getDelimittedRegexp(String regexp) {
  271. return ValidatorUtil.getDelimitedRegExp(regexp);
  272. }
  273. }