View Javadoc

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