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