Coverage Report - org.apache.commons.validator.GenericValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
GenericValidator
47%
22/46
35%
26/74
1.406
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.validator;
 18  
 
 19  
 import java.io.Serializable;
 20  
 import java.util.Locale;
 21  
 import java.util.regex.Pattern;
 22  
 
 23  
 import org.apache.commons.validator.routines.UrlValidator;
 24  
 import org.apache.commons.validator.routines.CreditCardValidator;
 25  
 import org.apache.commons.validator.routines.DateValidator;
 26  
 import org.apache.commons.validator.routines.EmailValidator;
 27  
 
 28  
 /**
 29  
  * This class contains basic methods for performing validations.
 30  
  *
 31  
  * @version $Revision: 1649191 $
 32  
  */
 33  0
 public class GenericValidator implements Serializable {
 34  
 
 35  
     private static final long serialVersionUID = -7212095066891517618L;
 36  
 
 37  
     /**
 38  
      * UrlValidator used in wrapper method.
 39  
      */
 40  1
     private static final UrlValidator URL_VALIDATOR = new UrlValidator();
 41  
 
 42  
     /**
 43  
      * CreditCardValidator used in wrapper method.
 44  
      */
 45  1
     private static final CreditCardValidator CREDIT_CARD_VALIDATOR =
 46  
         new CreditCardValidator();
 47  
 
 48  
     /**
 49  
      * <p>Checks if the field isn't null and length of the field is greater
 50  
      * than zero not including whitespace.</p>
 51  
      *
 52  
      * @param value The value validation is being performed on.
 53  
      * @return true if blank or null.
 54  
      */
 55  
     public static boolean isBlankOrNull(String value) {
 56  9557
         return ((value == null) || (value.trim().length() == 0));
 57  
     }
 58  
 
 59  
     /**
 60  
      * <p>Checks if the value matches the regular expression.</p>
 61  
      *
 62  
      * @param value The value validation is being performed on.
 63  
      * @param regexp The regular expression.
 64  
      * @return true if matches the regular expression.
 65  
      */
 66  
     public static boolean matchRegexp(String value, String regexp) {
 67  0
         if (regexp == null || regexp.length() <= 0) {
 68  0
             return false;
 69  
         }
 70  
 
 71  0
         return Pattern.matches(regexp, value);
 72  
     }
 73  
 
 74  
     /**
 75  
      * <p>Checks if the value can safely be converted to a byte primitive.</p>
 76  
      *
 77  
      * @param value The value validation is being performed on.
 78  
      * @return true if the value can be converted to a Byte.
 79  
      */
 80  
     public static boolean isByte(String value) {
 81  8
         return (GenericTypeValidator.formatByte(value) != null);
 82  
     }
 83  
 
 84  
     /**
 85  
      * <p>Checks if the value can safely be converted to a short primitive.</p>
 86  
      *
 87  
      * @param value The value validation is being performed on.
 88  
      * @return true if the value can be converted to a Short.
 89  
      */
 90  
     public static boolean isShort(String value) {
 91  6
         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  
      * @return true if the value can be converted to an Integer.
 99  
      */
 100  
     public static boolean isInt(String value) {
 101  20
         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  8
         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  6
         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  6
         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  0
         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  0
         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 &amp; 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  0
         return ((value >= min) && (value <= max));
 176  
     }
 177  
 
 178  
     /**
 179  
      * <p>Checks if a value is within a range (min &amp; 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  0
         return ((value >= min) && (value <= max));
 189  
     }
 190  
 
 191  
     /**
 192  
      * <p>Checks if a value is within a range (min &amp; 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  0
         return ((value >= min) && (value <= max));
 202  
     }
 203  
 
 204  
     /**
 205  
      * <p>Checks if a value is within a range (min &amp; 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  0
         return ((value >= min) && (value <= max));
 215  
     }
 216  
 
 217  
     /**
 218  
      * <p>Checks if a value is within a range (min &amp; 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  0
         return ((value >= min) && (value <= max));
 228  
     }
 229  
 
 230  
     /**
 231  
      * <p>Checks if a value is within a range (min &amp; 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  0
         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  0
         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  32
         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  0
         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  0
         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  12
         int adjustAmount = adjustForLineEnding(value, lineEndLength);
 295  12
         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  0
         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  12
         int adjustAmount = adjustForLineEnding(value, lineEndLength);
 319  12
         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  24
         int nCount = 0;
 333  24
         int rCount = 0;
 334  192
         for (int i = 0; i < value.length(); i++) {
 335  168
             if (value.charAt(i) == '\n') {
 336  24
                 nCount++;
 337  
             }
 338  168
             if (value.charAt(i) == '\r') {
 339  24
                 rCount++;
 340  
             }
 341  
         }
 342  24
         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 &gt;= the specified minimum.
 353  
      */
 354  
     public static boolean minValue(int value, int min) {
 355  0
         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 &gt;= the specified minimum.
 364  
      */
 365  
     public static boolean minValue(long value, long min) {
 366  0
         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 &gt;= the specified minimum.
 375  
      */
 376  
     public static boolean minValue(double value, double min) {
 377  0
         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 &gt;= the specified minimum.
 386  
      */
 387  
     public static boolean minValue(float value, float min) {
 388  0
         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 &lt;= the specified maximum.
 397  
      */
 398  
     public static boolean maxValue(int value, int max) {
 399  0
         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 &lt;= the specified maximum.
 408  
      */
 409  
     public static boolean maxValue(long value, long max) {
 410  0
         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 &lt;= the specified maximum.
 419  
      */
 420  
     public static boolean maxValue(double value, double max) {
 421  0
         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 &lt;= the specified maximum.
 430  
      */
 431  
     public static boolean maxValue(float value, float max) {
 432  0
         return (value <= max);
 433  
     }
 434  
 
 435  
 }