001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.validator; 018 019import java.io.Serializable; 020import java.util.Locale; 021import java.util.regex.Pattern; 022 023import org.apache.commons.validator.routines.UrlValidator; 024import org.apache.commons.validator.routines.CreditCardValidator; 025import org.apache.commons.validator.routines.DateValidator; 026import org.apache.commons.validator.routines.EmailValidator; 027 028/** 029 * This class contains basic methods for performing validations. 030 * 031 * @version $Revision$ 032 */ 033public class GenericValidator implements Serializable { 034 035 private static final long serialVersionUID = -7212095066891517618L; 036 037 /** 038 * UrlValidator used in wrapper method. 039 */ 040 private static final UrlValidator URL_VALIDATOR = new UrlValidator(); 041 042 /** 043 * CreditCardValidator used in wrapper method. 044 */ 045 private static final CreditCardValidator CREDIT_CARD_VALIDATOR = 046 new CreditCardValidator(); 047 048 /** 049 * <p>Checks if the field isn't null and length of the field is greater 050 * than zero not including whitespace.</p> 051 * 052 * @param value The value validation is being performed on. 053 * @return true if blank or null. 054 */ 055 public static boolean isBlankOrNull(String value) { 056 return ((value == null) || (value.trim().length() == 0)); 057 } 058 059 /** 060 * <p>Checks if the value matches the regular expression.</p> 061 * 062 * @param value The value validation is being performed on. 063 * @param regexp The regular expression. 064 * @return true if matches the regular expression. 065 */ 066 public static boolean matchRegexp(String value, String regexp) { 067 if (regexp == null || regexp.length() <= 0) { 068 return false; 069 } 070 071 return Pattern.matches(regexp, value); 072 } 073 074 /** 075 * <p>Checks if the value can safely be converted to a byte primitive.</p> 076 * 077 * @param value The value validation is being performed on. 078 * @return true if the value can be converted to a Byte. 079 */ 080 public static boolean isByte(String value) { 081 return (GenericTypeValidator.formatByte(value) != null); 082 } 083 084 /** 085 * <p>Checks if the value can safely be converted to a short primitive.</p> 086 * 087 * @param value The value validation is being performed on. 088 * @return true if the value can be converted to a Short. 089 */ 090 public static boolean isShort(String value) { 091 return (GenericTypeValidator.formatShort(value) != null); 092 } 093 094 /** 095 * <p>Checks if the value can safely be converted to a int primitive.</p> 096 * 097 * @param value The value validation is being performed on. 098 * @return true if the value can be converted to an Integer. 099 */ 100 public static boolean isInt(String value) { 101 return (GenericTypeValidator.formatInt(value) != null); 102 } 103 104 /** 105 * <p>Checks if the value can safely be converted to a long primitive.</p> 106 * 107 * @param value The value validation is being performed on. 108 * @return true if the value can be converted to a Long. 109 */ 110 public static boolean isLong(String value) { 111 return (GenericTypeValidator.formatLong(value) != null); 112 } 113 114 /** 115 * <p>Checks if the value can safely be converted to a float primitive.</p> 116 * 117 * @param value The value validation is being performed on. 118 * @return true if the value can be converted to a Float. 119 */ 120 public static boolean isFloat(String value) { 121 return (GenericTypeValidator.formatFloat(value) != null); 122 } 123 124 /** 125 * <p>Checks if the value can safely be converted to a double primitive.</p> 126 * 127 * @param value The value validation is being performed on. 128 * @return true if the value can be converted to a Double. 129 */ 130 public static boolean isDouble(String value) { 131 return (GenericTypeValidator.formatDouble(value) != null); 132 } 133 134 /** 135 * <p>Checks if the field is a valid date. The <code>Locale</code> is 136 * used with <code>java.text.DateFormat</code>. The setLenient method 137 * is set to <code>false</code> for all.</p> 138 * 139 * @param value The value validation is being performed on. 140 * @param locale The locale to use for the date format, defaults to the 141 * system default if null. 142 * @return true if the value can be converted to a Date. 143 */ 144 public static boolean isDate(String value, Locale locale) { 145 return DateValidator.getInstance().isValid(value, locale); 146 } 147 148 /** 149 * <p>Checks if the field is a valid date. The pattern is used with 150 * <code>java.text.SimpleDateFormat</code>. If strict is true, then the 151 * length will be checked so '2/12/1999' will not pass validation with 152 * the format 'MM/dd/yyyy' because the month isn't two digits. 153 * The setLenient method is set to <code>false</code> for all.</p> 154 * 155 * @param value The value validation is being performed on. 156 * @param datePattern The pattern passed to <code>SimpleDateFormat</code>. 157 * @param strict Whether or not to have an exact match of the datePattern. 158 * @return true if the value can be converted to a Date. 159 */ 160 public static boolean isDate(String value, String datePattern, boolean strict) { 161 // TODO method isValid() not yet supported in routines version 162 return org.apache.commons.validator.DateValidator.getInstance().isValid(value, datePattern, strict); 163 } 164 165 /** 166 * <p>Checks if a value is within a range (min & max specified 167 * in the vars attribute).</p> 168 * 169 * @param value The value validation is being performed on. 170 * @param min The minimum value of the range. 171 * @param max The maximum value of the range. 172 * @return true if the value is in the specified range. 173 */ 174 public static boolean isInRange(byte value, byte min, byte max) { 175 return ((value >= min) && (value <= max)); 176 } 177 178 /** 179 * <p>Checks if a value is within a range (min & max specified 180 * in the vars attribute).</p> 181 * 182 * @param value The value validation is being performed on. 183 * @param min The minimum value of the range. 184 * @param max The maximum value of the range. 185 * @return true if the value is in the specified range. 186 */ 187 public static boolean isInRange(int value, int min, int max) { 188 return ((value >= min) && (value <= max)); 189 } 190 191 /** 192 * <p>Checks if a value is within a range (min & max specified 193 * in the vars attribute).</p> 194 * 195 * @param value The value validation is being performed on. 196 * @param min The minimum value of the range. 197 * @param max The maximum value of the range. 198 * @return true if the value is in the specified range. 199 */ 200 public static boolean isInRange(float value, float min, float max) { 201 return ((value >= min) && (value <= max)); 202 } 203 204 /** 205 * <p>Checks if a value is within a range (min & max specified 206 * in the vars attribute).</p> 207 * 208 * @param value The value validation is being performed on. 209 * @param min The minimum value of the range. 210 * @param max The maximum value of the range. 211 * @return true if the value is in the specified range. 212 */ 213 public static boolean isInRange(short value, short min, short max) { 214 return ((value >= min) && (value <= max)); 215 } 216 217 /** 218 * <p>Checks if a value is within a range (min & max specified 219 * in the vars attribute).</p> 220 * 221 * @param value The value validation is being performed on. 222 * @param min The minimum value of the range. 223 * @param max The maximum value of the range. 224 * @return true if the value is in the specified range. 225 */ 226 public static boolean isInRange(long value, long min, long max) { 227 return ((value >= min) && (value <= max)); 228 } 229 230 /** 231 * <p>Checks if a value is within a range (min & max specified 232 * in the vars attribute).</p> 233 * 234 * @param value The value validation is being performed on. 235 * @param min The minimum value of the range. 236 * @param max The maximum value of the range. 237 * @return true if the value is in the specified range. 238 */ 239 public static boolean isInRange(double value, double min, double max) { 240 return ((value >= min) && (value <= max)); 241 } 242 243 /** 244 * Checks if the field is a valid credit card number. 245 * @param value The value validation is being performed on. 246 * @return true if the value is valid Credit Card Number. 247 */ 248 public static boolean isCreditCard(String value) { 249 return CREDIT_CARD_VALIDATOR.isValid(value); 250 } 251 252 /** 253 * <p>Checks if a field has a valid e-mail address.</p> 254 * 255 * @param value The value validation is being performed on. 256 * @return true if the value is valid Email Address. 257 */ 258 public static boolean isEmail(String value) { 259 return EmailValidator.getInstance().isValid(value); 260 } 261 262 /** 263 * <p>Checks if a field is a valid url address.</p> 264 * If you need to modify what is considered valid then 265 * consider using the UrlValidator directly. 266 * 267 * @param value The value validation is being performed on. 268 * @return true if the value is valid Url. 269 */ 270 public static boolean isUrl(String value) { 271 return URL_VALIDATOR.isValid(value); 272 } 273 274 /** 275 * <p>Checks if the value's length is less than or equal to the max.</p> 276 * 277 * @param value The value validation is being performed on. 278 * @param max The maximum length. 279 * @return true if the value's length is less than the specified maximum. 280 */ 281 public static boolean maxLength(String value, int max) { 282 return (value.length() <= max); 283 } 284 285 /** 286 * <p>Checks if the value's adjusted length is less than or equal to the max.</p> 287 * 288 * @param value The value validation is being performed on. 289 * @param max The maximum length. 290 * @param lineEndLength The length to use for line endings. 291 * @return true if the value's length is less than the specified maximum. 292 */ 293 public static boolean maxLength(String value, int max, int lineEndLength) { 294 int adjustAmount = adjustForLineEnding(value, lineEndLength); 295 return ((value.length() + adjustAmount) <= max); 296 } 297 298 /** 299 * <p>Checks if the value's length is greater than or equal to the min.</p> 300 * 301 * @param value The value validation is being performed on. 302 * @param min The minimum length. 303 * @return true if the value's length is more than the specified minimum. 304 */ 305 public static boolean minLength(String value, int min) { 306 return (value.length() >= min); 307 } 308 309 /** 310 * <p>Checks if the value's adjusted length is greater than or equal to the min.</p> 311 * 312 * @param value The value validation is being performed on. 313 * @param min The minimum length. 314 * @param lineEndLength The length to use for line endings. 315 * @return true if the value's length is more than the specified minimum. 316 */ 317 public static boolean minLength(String value, int min, int lineEndLength) { 318 int adjustAmount = adjustForLineEnding(value, lineEndLength); 319 return ((value.length() + adjustAmount) >= min); 320 } 321 322 /** 323 * Calculate an adjustment amount for line endings. 324 * 325 * See Bug 37962 for the rational behind this. 326 * 327 * @param value The value validation is being performed on. 328 * @param lineEndLength The length to use for line endings. 329 * @return the adjustment amount. 330 */ 331 private static int adjustForLineEnding(String value, int lineEndLength) { 332 int nCount = 0; 333 int rCount = 0; 334 for (int i = 0; i < value.length(); i++) { 335 if (value.charAt(i) == '\n') { 336 nCount++; 337 } 338 if (value.charAt(i) == '\r') { 339 rCount++; 340 } 341 } 342 return ((nCount * lineEndLength) - (rCount + nCount)); 343 } 344 345 // See http://issues.apache.org/bugzilla/show_bug.cgi?id=29015 WRT the "value" methods 346 347 /** 348 * <p>Checks if the value is greater than or equal to the min.</p> 349 * 350 * @param value The value validation is being performed on. 351 * @param min The minimum numeric value. 352 * @return true if the value is >= the specified minimum. 353 */ 354 public static boolean minValue(int value, int min) { 355 return (value >= min); 356 } 357 358 /** 359 * <p>Checks if the value is greater than or equal to the min.</p> 360 * 361 * @param value The value validation is being performed on. 362 * @param min The minimum numeric value. 363 * @return true if the value is >= the specified minimum. 364 */ 365 public static boolean minValue(long value, long min) { 366 return (value >= min); 367 } 368 369 /** 370 * <p>Checks if the value is greater than or equal to the min.</p> 371 * 372 * @param value The value validation is being performed on. 373 * @param min The minimum numeric value. 374 * @return true if the value is >= the specified minimum. 375 */ 376 public static boolean minValue(double value, double min) { 377 return (value >= min); 378 } 379 380 /** 381 * <p>Checks if the value is greater than or equal to the min.</p> 382 * 383 * @param value The value validation is being performed on. 384 * @param min The minimum numeric value. 385 * @return true if the value is >= the specified minimum. 386 */ 387 public static boolean minValue(float value, float min) { 388 return (value >= min); 389 } 390 391 /** 392 * <p>Checks if the value is less than or equal to the max.</p> 393 * 394 * @param value The value validation is being performed on. 395 * @param max The maximum numeric value. 396 * @return true if the value is <= the specified maximum. 397 */ 398 public static boolean maxValue(int value, int max) { 399 return (value <= max); 400 } 401 402 /** 403 * <p>Checks if the value is less than or equal to the max.</p> 404 * 405 * @param value The value validation is being performed on. 406 * @param max The maximum numeric value. 407 * @return true if the value is <= the specified maximum. 408 */ 409 public static boolean maxValue(long value, long max) { 410 return (value <= max); 411 } 412 413 /** 414 * <p>Checks if the value is less than or equal to the max.</p> 415 * 416 * @param value The value validation is being performed on. 417 * @param max The maximum numeric value. 418 * @return true if the value is <= the specified maximum. 419 */ 420 public static boolean maxValue(double value, double max) { 421 return (value <= max); 422 } 423 424 /** 425 * <p>Checks if the value is less than or equal to the max.</p> 426 * 427 * @param value The value validation is being performed on. 428 * @param max The maximum numeric value. 429 * @return true if the value is <= the specified maximum. 430 */ 431 public static boolean maxValue(float value, float max) { 432 return (value <= max); 433 } 434 435}