Coverage report

  %line %branch
org.apache.commons.validator.GenericValidator
47% 
97% 

 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  
 
 22  
 import org.apache.oro.text.perl.Perl5Util;
 23  
 
 24  
 /**
 25  
  * This class contains basic methods for performing validations.
 26  
  *
 27  
  * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
 28  
  */
 29  0
 public class GenericValidator implements Serializable {
 30  
 
 31  
     /**
 32  
      * UrlValidator used in wrapper method.
 33  
      */
 34  30
     private static final UrlValidator URL_VALIDATOR = new UrlValidator();
 35  
 
 36  
     /**
 37  
      * CreditCardValidator used in wrapper method.
 38  
      */
 39  15
     private static final CreditCardValidator CREDIT_CARD_VALIDATOR =
 40  
         new CreditCardValidator();
 41  
 
 42  
     /**
 43  
      * <p>Checks if the field isn't null and length of the field is greater 
 44  
      * than zero not including whitespace.</p>
 45  
      *
 46  
      * @param value The value validation is being performed on.
 47  
      * @return true if blank or null.
 48  
      */
 49  
     public static boolean isBlankOrNull(String value) {
 50  9533
         return ((value == null) || (value.trim().length() == 0));
 51  
     }
 52  
 
 53  
     /**
 54  
      * <p>Checks if the value matches the regular expression.</p>
 55  
      *
 56  
      * @param value The value validation is being performed on.
 57  
      * @param regexp The regular expression.
 58  
      * @return true if matches the regular expression.
 59  
      */
 60  
     public static boolean matchRegexp(String value, String regexp) {
 61  0
         if (regexp == null || regexp.length() <= 0) {
 62  0
             return false;
 63  
         }
 64  
 
 65  0
         Perl5Util matcher = new Perl5Util();
 66  0
         return matcher.match("/" + regexp + "/", value);
 67  
     }
 68  
 
 69  
     /**
 70  
      * <p>Checks if the value can safely be converted to a byte primitive.</p>
 71  
      *
 72  
      * @param value The value validation is being performed on.
 73  
      * @return true if the value can be converted to a Byte.
 74  
      */
 75  
     public static boolean isByte(String value) {
 76  8
         return (GenericTypeValidator.formatByte(value) != null);
 77  
     }
 78  
 
 79  
     /**
 80  
      * <p>Checks if the value can safely be converted to a short primitive.</p>
 81  
      *
 82  
      * @param value The value validation is being performed on.
 83  
      * @return true if the value can be converted to a Short.
 84  
      */
 85  
     public static boolean isShort(String value) {
 86  6
         return (GenericTypeValidator.formatShort(value) != null);
 87  
     }
 88  
 
 89  
     /**
 90  
      * <p>Checks if the value can safely be converted to a int primitive.</p>
 91  
      *
 92  
      * @param value The value validation is being performed on.
 93  
      * @return true if the value can be converted to an Integer.
 94  
      */
 95  
     public static boolean isInt(String value) {
 96  14
         return (GenericTypeValidator.formatInt(value) != null);
 97  
     }
 98  
 
 99  
     /**
 100  
      * <p>Checks if the value can safely be converted to a long primitive.</p>
 101  
      *
 102  
      * @param value The value validation is being performed on.
 103  
      * @return true if the value can be converted to a Long.
 104  
      */
 105  
     public static boolean isLong(String value) {
 106  8
         return (GenericTypeValidator.formatLong(value) != null);
 107  
     }
 108  
 
 109  
     /**
 110  
      * <p>Checks if the value can safely be converted to a float primitive.</p>
 111  
      *
 112  
      * @param value The value validation is being performed on.
 113  
      * @return true if the value can be converted to a Float.
 114  
      */
 115  
     public static boolean isFloat(String value) {
 116  6
         return (GenericTypeValidator.formatFloat(value) != null);
 117  
     }
 118  
 
 119  
     /**
 120  
      * <p>Checks if the value can safely be converted to a double primitive.</p>
 121  
      *
 122  
      * @param value The value validation is being performed on.
 123  
      * @return true if the value can be converted to a Double.
 124  
      */
 125  
     public static boolean isDouble(String value) {
 126  6
         return (GenericTypeValidator.formatDouble(value) != null);
 127  
     }
 128  
 
 129  
     /**
 130  
      * <p>Checks if the field is a valid date.  The <code>Locale</code> is
 131  
      * used with <code>java.text.DateFormat</code>.  The setLenient method
 132  
      * is set to <code>false</code> for all.</p>
 133  
      *
 134  
      * @param value The value validation is being performed on.
 135  
      * @param locale The locale to use for the date format, defaults to the 
 136  
      * system default if null.
 137  
      * @return true if the value can be converted to a Date.
 138  
      */
 139  
     public static boolean isDate(String value, Locale locale) {
 140  0
         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  
      * @return true if the value can be converted to a Date.
 154  
      */
 155  
     public static boolean isDate(String value, String datePattern, class="keyword">boolean strict) {
 156  0
         return DateValidator.getInstance().isValid(value, datePattern, strict);
 157  
     }
 158  
 
 159  
     /**
 160  
     * <p>Checks if a value is within a range (min &amp; max specified
 161  
     * in the vars attribute).</p>
 162  
     *
 163  
     * @param value The value validation is being performed on.
 164  
     * @param min The minimum value of the range.
 165  
     * @param max The maximum value of the range.
 166  
      * @return true if the value is in the specified range.
 167  
     */
 168  
     public static boolean isInRange(byte value, byte min, byte max) {
 169  0
         return ((value >= min) && (value <= max));
 170  
     }
 171  
 
 172  
     /**
 173  
      * <p>Checks if a value is within a range (min &amp; 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  
      * @return true if the value is in the specified range.
 180  
      */
 181  
     public static boolean isInRange(int value, class="keyword">int min, class="keyword">int max) {
 182  0
         return ((value >= min) && (value <= max));
 183  
     }
 184  
 
 185  
     /**
 186  
      * <p>Checks if a value is within a range (min &amp; max specified
 187  
      * in the vars attribute).</p>
 188  
      *
 189  
      * @param value The value validation is being performed on.
 190  
      * @param min The minimum value of the range.
 191  
      * @param max The maximum value of the range.
 192  
      * @return true if the value is in the specified range.
 193  
      */
 194  
     public static boolean isInRange(float value, class="keyword">float min, class="keyword">float max) {
 195  0
         return ((value >= min) && (value <= max));
 196  
     }
 197  
 
 198  
     /**
 199  
      * <p>Checks if a value is within a range (min &amp; max specified
 200  
      * in the vars attribute).</p>
 201  
      *
 202  
      * @param value The value validation is being performed on.
 203  
      * @param min The minimum value of the range.
 204  
      * @param max The maximum value of the range.
 205  
      * @return true if the value is in the specified range.
 206  
      */
 207  
     public static boolean isInRange(short value, class="keyword">short min, class="keyword">short max) {
 208  0
         return ((value >= min) && (value <= max));
 209  
     }
 210  
 
 211  
     /**
 212  
      * <p>Checks if a value is within a range (min &amp; max specified
 213  
      * in the vars attribute).</p>
 214  
      *
 215  
      * @param value The value validation is being performed on.
 216  
      * @param min The minimum value of the range.
 217  
      * @param max The maximum value of the range.
 218  
      * @return true if the value is in the specified range.
 219  
      */
 220  
     public static boolean isInRange(long value, class="keyword">long min, class="keyword">long max) {
 221  0
         return ((value >= min) && (value <= max));
 222  
     }
 223  
 
 224  
     /**
 225  
      * <p>Checks if a value is within a range (min &amp; max specified
 226  
      * in the vars attribute).</p>
 227  
      *
 228  
      * @param value The value validation is being performed on.
 229  
      * @param min The minimum value of the range.
 230  
      * @param max The maximum value of the range.
 231  
      * @return true if the value is in the specified range.
 232  
      */
 233  
     public static boolean isInRange(double value, class="keyword">double min, class="keyword">double max) {
 234  0
         return ((value >= min) && (value <= max));
 235  
     }
 236  
 
 237  
     /**
 238  
      * Checks if the field is a valid credit card number.
 239  
      * @param value The value validation is being performed on.
 240  
      * @return true if the value is valid Credit Card Number.
 241  
      */
 242  
     public static boolean isCreditCard(String value) {
 243  0
         return CREDIT_CARD_VALIDATOR.isValid(value);
 244  
     }
 245  
 
 246  
     /**
 247  
      * <p>Checks if a field has a valid e-mail address.</p>
 248  
      *
 249  
      * @param value The value validation is being performed on.
 250  
      * @return true if the value is valid Email Address.
 251  
      */
 252  
     public static boolean isEmail(String value) {
 253  29
         return EmailValidator.getInstance().isValid(value);
 254  
     }
 255  
 
 256  
     /**
 257  
      * <p>Checks if a field is a valid url address.</p>
 258  
      * If you need to modify what is considered valid then
 259  
      * consider using the UrlValidator directly.
 260  
      *
 261  
      * @param value The value validation is being performed on.
 262  
      * @return true if the value is valid Url.
 263  
      */
 264  
     public static boolean isUrl(String value) {
 265  0
         return URL_VALIDATOR.isValid(value);
 266  
     }
 267  
 
 268  
     /**
 269  
      * <p>Checks if the value's length is less than or equal to the max.</p>
 270  
      *
 271  
      * @param value The value validation is being performed on.
 272  
      * @param max The maximum length.
 273  
      * @return true if the value's length is less than the specified maximum.
 274  
      */
 275  
     public static boolean maxLength(String value, int max) {
 276  0
         return (value.length() <= max);
 277  
     }
 278  
 
 279  
     /**
 280  
      * <p>Checks if the value's adjusted length is less than or equal to the max.</p>
 281  
      *
 282  
      * @param value The value validation is being performed on.
 283  
      * @param max The maximum length.
 284  
      * @param lineEndLength The length to use for line endings.
 285  
      * @return true if the value's length is less than the specified maximum.
 286  
      */
 287  
     public static boolean maxLength(String value, int max, class="keyword">int lineEndLength) {
 288  12
         int adjustAmount = adjustForLineEnding(value, lineEndLength);
 289  12
         return ((value.length() + adjustAmount) <= max);
 290  
     }
 291  
 
 292  
     /**
 293  
      * <p>Checks if the value's length is greater than or equal to the min.</p>
 294  
      *
 295  
      * @param value The value validation is being performed on.
 296  
      * @param min The minimum length.
 297  
      * @return true if the value's length is more than the specified minimum.
 298  
      */
 299  
     public static boolean minLength(String value, int min) {
 300  0
         return (value.length() >= min);
 301  
     }
 302  
 
 303  
     /**
 304  
      * <p>Checks if the value's adjusted length is greater than or equal to the min.</p>
 305  
      *
 306  
      * @param value The value validation is being performed on.
 307  
      * @param min The minimum length.
 308  
      * @param lineEndLength The length to use for line endings.
 309  
      * @return true if the value's length is more than the specified minimum.
 310  
      */
 311  
     public static boolean minLength(String value, int min, class="keyword">int lineEndLength) {
 312  12
         int adjustAmount = adjustForLineEnding(value, lineEndLength);
 313  12
         return ((value.length() + adjustAmount) >= min);
 314  
     }
 315  
 
 316  
     /**
 317  
      * Calculate an adjustment amount for line endings.
 318  
      *
 319  
      * See Bug 37962 for the rational behind this.
 320  
      *
 321  
      * @param value The value validation is being performed on.
 322  
      * @param lineEndLength The length to use for line endings.
 323  
      * @return the adjustment amount.
 324  
      */
 325  
     private static int adjustForLineEnding(String value, class="keyword">int lineEndLength) {
 326  24
         int nCount = 0;
 327  24
         int rCount = 0;
 328  192
         for (int i = 0; i < value.length(); i++) {
 329  168
             if (value.charAt(i) == '\n') {
 330  24
                 nCount++;
 331  
             }
 332  168
             if (value.charAt(i) == '\r') {
 333  24
                 rCount++;
 334  
             }
 335  
         }
 336  24
         return ((nCount * lineEndLength) - (rCount + nCount));
 337  
     }
 338  
     
 339  
     // See http://issues.apache.org/bugzilla/show_bug.cgi?id=29015 WRT the "value" methods
 340  
 
 341  
     /**
 342  
      * <p>Checks if the value is greater than or equal to the min.</p>
 343  
      *
 344  
      * @param value The value validation is being performed on.
 345  
      * @param min The minimum numeric value.
 346  
      * @return true if the value is &gt;= the specified minimum.
 347  
      */
 348  
     public static boolean minValue(int value, class="keyword">int min) {
 349  0
         return (value >= min);
 350  
     }
 351  
 
 352  
     /**
 353  
      * <p>Checks if the value is greater than or equal to the min.</p>
 354  
      *
 355  
      * @param value The value validation is being performed on.
 356  
      * @param min The minimum numeric value.
 357  
      * @return true if the value is &gt;= the specified minimum.
 358  
      */
 359  
     public static boolean minValue(long value, class="keyword">long min) {
 360  0
         return (value >= min);
 361  
     }
 362  
 
 363  
     /**
 364  
      * <p>Checks if the value is greater than or equal to the min.</p>
 365  
      *
 366  
      * @param value The value validation is being performed on.
 367  
      * @param min The minimum numeric value.
 368  
      * @return true if the value is &gt;= the specified minimum.
 369  
      */
 370  
     public static boolean minValue(double value, class="keyword">double min) {
 371  0
         return (value >= min);
 372  
     }
 373  
 
 374  
     /**
 375  
      * <p>Checks if the value is greater than or equal to the min.</p>
 376  
      *
 377  
      * @param value The value validation is being performed on.
 378  
      * @param min The minimum numeric value.
 379  
      * @return true if the value is &gt;= the specified minimum.
 380  
      */
 381  
     public static boolean minValue(float value, class="keyword">float min) {
 382  0
         return (value >= min);
 383  
     }
 384  
 
 385  
     /**
 386  
      * <p>Checks if the value is less than or equal to the max.</p>
 387  
      *
 388  
      * @param value The value validation is being performed on.
 389  
      * @param max The maximum numeric value.
 390  
      * @return true if the value is &lt;= the specified maximum.
 391  
      */
 392  
     public static boolean maxValue(int value, class="keyword">int max) {
 393  0
         return (value <= max);
 394  
     }
 395  
 
 396  
     /**
 397  
      * <p>Checks if the value is less than or equal to the max.</p>
 398  
      *
 399  
      * @param value The value validation is being performed on.
 400  
      * @param max The maximum numeric value.
 401  
      * @return true if the value is &lt;= the specified maximum.
 402  
      */
 403  
     public static boolean maxValue(long value, class="keyword">long max) {
 404  0
         return (value <= max);
 405  
     }
 406  
 
 407  
     /**
 408  
      * <p>Checks if the value is less than or equal to the max.</p>
 409  
      *
 410  
      * @param value The value validation is being performed on.
 411  
      * @param max The maximum numeric value.
 412  
      * @return true if the value is &lt;= the specified maximum.
 413  
      */
 414  
     public static boolean maxValue(double value, class="keyword">double max) {
 415  0
         return (value <= max);
 416  
     }
 417  
 
 418  
     /**
 419  
      * <p>Checks if the value is less than or equal to the max.</p>
 420  
      *
 421  
      * @param value The value validation is being performed on.
 422  
      * @param max The maximum numeric value.
 423  
      * @return true if the value is &lt;= the specified maximum.
 424  
      */
 425  
     public static boolean maxValue(float value, class="keyword">float max) {
 426  0
         return (value <= max);
 427  
     }
 428  
 
 429  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.