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

This page was automatically generated by Maven