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