GenericValidator.java

  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. import java.io.Serializable;
  19. import java.util.Locale;
  20. import java.util.regex.Pattern;

  21. import org.apache.commons.validator.routines.CreditCardValidator;
  22. import org.apache.commons.validator.routines.DateValidator;
  23. import org.apache.commons.validator.routines.EmailValidator;
  24. import org.apache.commons.validator.routines.UrlValidator;

  25. /**
  26.  * This class contains basic methods for performing validations.
  27.  */
  28. public class GenericValidator implements Serializable {

  29.     private static final long serialVersionUID = -7212095066891517618L;

  30.     /**
  31.      * UrlValidator used in wrapper method.
  32.      */
  33.     private static final UrlValidator URL_VALIDATOR = new UrlValidator();

  34.     /**
  35.      * CreditCardValidator used in wrapper method.
  36.      */
  37.     private static final CreditCardValidator CREDIT_CARD_VALIDATOR = new CreditCardValidator();

  38.     /**
  39.      * Calculate an adjustment amount for line endings.
  40.      *
  41.      * See Bug 37962 for the rational behind this.
  42.      *
  43.      * @param value The value validation is being performed on.
  44.      * @param lineEndLength The length to use for line endings.
  45.      * @return the adjustment amount.
  46.      */
  47.     private static int adjustForLineEnding(final String value, final int lineEndLength) {
  48.         int nCount = 0;
  49.         int rCount = 0;
  50.         for (int i = 0; i < value.length(); i++) {
  51.             if (value.charAt(i) == '\n') {
  52.                 nCount++;
  53.             }
  54.             if (value.charAt(i) == '\r') {
  55.                 rCount++;
  56.             }
  57.         }
  58.         final int rnCount = rCount + nCount;
  59.         return nCount * lineEndLength - rnCount;
  60.     }

  61.     /**
  62.      * <p>Checks if the field isn't null and length of the field is greater
  63.      * than zero not including whitespace.</p>
  64.      *
  65.      * @param value The value validation is being performed on.
  66.      * @return true if blank or null.
  67.      */
  68.     public static boolean isBlankOrNull(final String value) {
  69.         // Don't trim is already empty.
  70.         return value == null || value.isEmpty() || value.trim().isEmpty();
  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(final String value) {
  79.         return GenericTypeValidator.formatByte(value) != null;
  80.     }

  81.     /**
  82.      * Checks if the field is a valid credit card number.
  83.      * @param value The value validation is being performed on.
  84.      * @return true if the value is valid Credit Card Number.
  85.      */
  86.     public static boolean isCreditCard(final String value) {
  87.         return CREDIT_CARD_VALIDATOR.isValid(value);
  88.     }

  89.     /**
  90.      * <p>Checks if the field is a valid date.  The <code>Locale</code> is
  91.      * used with <code>java.text.DateFormat</code>.  The setLenient method
  92.      * is set to {@code false} for all.</p>
  93.      *
  94.      * @param value The value validation is being performed on.
  95.      * @param locale The locale to use for the date format, defaults to the
  96.      * system default if null.
  97.      * @return true if the value can be converted to a Date.
  98.      */
  99.     public static boolean isDate(final String value, final Locale locale) {
  100.         return DateValidator.getInstance().isValid(value, locale);
  101.     }

  102.     /**
  103.      * <p>Checks if the field is a valid date.  The pattern is used with
  104.      * <code>java.text.SimpleDateFormat</code>.  If strict is true, then the
  105.      * length will be checked so '2/12/1999' will not pass validation with
  106.      * the format 'MM/dd/yyyy' because the month isn't two digits.
  107.      * The setLenient method is set to {@code false} for all.</p>
  108.      *
  109.      * @param value The value validation is being performed on.
  110.      * @param datePattern The pattern passed to <code>SimpleDateFormat</code>.
  111.      * @param strict Whether or not to have an exact match of the datePattern.
  112.      * @return true if the value can be converted to a Date.
  113.      */
  114.     public static boolean isDate(final String value, final String datePattern, final boolean strict) {
  115.         // TODO method isValid() not yet supported in routines version
  116.         return org.apache.commons.validator.DateValidator.getInstance().isValid(value, datePattern, strict);
  117.     }

  118.     /**
  119.      * <p>Checks if the value can safely be converted to a double primitive.</p>
  120.      *
  121.      * @param value The value validation is being performed on.
  122.      * @return true if the value can be converted to a Double.
  123.      */
  124.     public static boolean isDouble(final String value) {
  125.         return GenericTypeValidator.formatDouble(value) != null;
  126.     }

  127.     /**
  128.      * <p>Checks if a field has a valid e-mail address.</p>
  129.      *
  130.      * @param value The value validation is being performed on.
  131.      * @return true if the value is valid Email Address.
  132.      */
  133.     public static boolean isEmail(final String value) {
  134.         return EmailValidator.getInstance().isValid(value);
  135.     }

  136.     /**
  137.      * <p>Checks if the value can safely be converted to a float primitive.</p>
  138.      *
  139.      * @param value The value validation is being performed on.
  140.      * @return true if the value can be converted to a Float.
  141.      */
  142.     public static boolean isFloat(final String value) {
  143.         return GenericTypeValidator.formatFloat(value) != null;
  144.     }

  145.     /**
  146.     * <p>Checks if a value is within a range (min &amp; max specified
  147.     * in the vars attribute).</p>
  148.     *
  149.     * @param value The value validation is being performed on.
  150.     * @param min The minimum value of the range.
  151.     * @param max The maximum value of the range.
  152.      * @return true if the value is in the specified range.
  153.     */
  154.     public static boolean isInRange(final byte value, final byte min, final byte max) {
  155.         return value >= min && value <= max;
  156.     }

  157.     /**
  158.      * <p>Checks if a value is within a range (min &amp; 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(final double value, final double min, final double max) {
  167.         return value >= min && value <= max;
  168.     }

  169.     /**
  170.      * <p>Checks if a value is within a range (min &amp; max specified
  171.      * in the vars attribute).</p>
  172.      *
  173.      * @param value The value validation is being performed on.
  174.      * @param min The minimum value of the range.
  175.      * @param max The maximum value of the range.
  176.      * @return true if the value is in the specified range.
  177.      */
  178.     public static boolean isInRange(final float value, final float min, final float max) {
  179.         return value >= min && value <= max;
  180.     }

  181.     /**
  182.      * <p>Checks if a value is within a range (min &amp; max specified
  183.      * in the vars attribute).</p>
  184.      *
  185.      * @param value The value validation is being performed on.
  186.      * @param min The minimum value of the range.
  187.      * @param max The maximum value of the range.
  188.      * @return true if the value is in the specified range.
  189.      */
  190.     public static boolean isInRange(final int value, final int min, final int max) {
  191.         return value >= min && value <= max;
  192.     }

  193.     /**
  194.      * <p>Checks if a value is within a range (min &amp; max specified
  195.      * in the vars attribute).</p>
  196.      *
  197.      * @param value The value validation is being performed on.
  198.      * @param min The minimum value of the range.
  199.      * @param max The maximum value of the range.
  200.      * @return true if the value is in the specified range.
  201.      */
  202.     public static boolean isInRange(final long value, final long min, final long max) {
  203.         return value >= min && value <= max;
  204.     }

  205.     /**
  206.      * <p>Checks if a value is within a range (min &amp; max specified
  207.      * in the vars attribute).</p>
  208.      *
  209.      * @param value The value validation is being performed on.
  210.      * @param min The minimum value of the range.
  211.      * @param max The maximum value of the range.
  212.      * @return true if the value is in the specified range.
  213.      */
  214.     public static boolean isInRange(final short value, final short min, final short max) {
  215.         return value >= min && value <= max;
  216.     }

  217.     /**
  218.      * <p>Checks if the value can safely be converted to a int primitive.</p>
  219.      *
  220.      * @param value The value validation is being performed on.
  221.      * @return true if the value can be converted to an Integer.
  222.      */
  223.     public static boolean isInt(final String value) {
  224.         return GenericTypeValidator.formatInt(value) != null;
  225.     }

  226.     /**
  227.      * <p>Checks if the value can safely be converted to a long primitive.</p>
  228.      *
  229.      * @param value The value validation is being performed on.
  230.      * @return true if the value can be converted to a Long.
  231.      */
  232.     public static boolean isLong(final String value) {
  233.         return GenericTypeValidator.formatLong(value) != null;
  234.     }

  235.     /**
  236.      * <p>Checks if the value can safely be converted to a short primitive.</p>
  237.      *
  238.      * @param value The value validation is being performed on.
  239.      * @return true if the value can be converted to a Short.
  240.      */
  241.     public static boolean isShort(final String value) {
  242.         return GenericTypeValidator.formatShort(value) != null;
  243.     }

  244.     /**
  245.      * <p>Checks if a field is a valid URL address.</p>
  246.      * If you need to modify what is considered valid then
  247.      * consider using the UrlValidator directly.
  248.      *
  249.      * @param value The value validation is being performed on.
  250.      * @return true if the value is valid Url.
  251.      */
  252.     public static boolean isUrl(final String value) {
  253.         return URL_VALIDATOR.isValid(value);
  254.     }

  255.     /**
  256.      * <p>Checks if the value matches the regular expression.</p>
  257.      *
  258.      * @param value The value validation is being performed on.
  259.      * @param regexp The regular expression.
  260.      * @return true if matches the regular expression.
  261.      */
  262.     public static boolean matchRegexp(final String value, final String regexp) {
  263.         if (regexp == null || regexp.isEmpty()) {
  264.             return false;
  265.         }

  266.         return Pattern.matches(regexp, value);
  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(final String value, final int max) {
  276.         return value.length() <= max;
  277.     }

  278.     /**
  279.      * <p>Checks if the value's adjusted length is less than or equal to the max.</p>
  280.      *
  281.      * @param value The value validation is being performed on.
  282.      * @param max The maximum length.
  283.      * @param lineEndLength The length to use for line endings.
  284.      * @return true if the value's length is less than the specified maximum.
  285.      */
  286.     public static boolean maxLength(final String value, final int max, final int lineEndLength) {
  287.         final int adjustAmount = adjustForLineEnding(value, lineEndLength);
  288.         return value.length() + adjustAmount <= max;
  289.     }

  290.     /**
  291.      * <p>Checks if the value is less than or equal to the max.</p>
  292.      *
  293.      * @param value The value validation is being performed on.
  294.      * @param max The maximum numeric value.
  295.      * @return true if the value is &lt;= the specified maximum.
  296.      */
  297.     public static boolean maxValue(final double value, final double max) {
  298.         return value <= max;
  299.     }

  300.     /**
  301.      * <p>Checks if the value is less than or equal to the max.</p>
  302.      *
  303.      * @param value The value validation is being performed on.
  304.      * @param max The maximum numeric value.
  305.      * @return true if the value is &lt;= the specified maximum.
  306.      */
  307.     public static boolean maxValue(final float value, final float max) {
  308.         return value <= max;
  309.     }

  310.     // See https://issues.apache.org/bugzilla/show_bug.cgi?id=29015 WRT the "value" methods

  311.     /**
  312.      * <p>Checks if the value is less than or equal to the max.</p>
  313.      *
  314.      * @param value The value validation is being performed on.
  315.      * @param max The maximum numeric value.
  316.      * @return true if the value is &lt;= the specified maximum.
  317.      */
  318.     public static boolean maxValue(final int value, final int max) {
  319.         return value <= max;
  320.     }

  321.     /**
  322.      * <p>Checks if the value is less than or equal to the max.</p>
  323.      *
  324.      * @param value The value validation is being performed on.
  325.      * @param max The maximum numeric value.
  326.      * @return true if the value is &lt;= the specified maximum.
  327.      */
  328.     public static boolean maxValue(final long value, final long max) {
  329.         return value <= max;
  330.     }

  331.     /**
  332.      * <p>Checks if the value's length is greater than or equal to the min.</p>
  333.      *
  334.      * @param value The value validation is being performed on.
  335.      * @param min The minimum length.
  336.      * @return true if the value's length is more than the specified minimum.
  337.      */
  338.     public static boolean minLength(final String value, final int min) {
  339.         return value.length() >= min;
  340.     }

  341.     /**
  342.      * <p>Checks if the value's adjusted length 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 length.
  346.      * @param lineEndLength The length to use for line endings.
  347.      * @return true if the value's length is more than the specified minimum.
  348.      */
  349.     public static boolean minLength(final String value, final int min, final int lineEndLength) {
  350.         final int adjustAmount = adjustForLineEnding(value, lineEndLength);
  351.         return value.length() + adjustAmount >= min;
  352.     }

  353.     /**
  354.      * <p>Checks if the value is greater than or equal to the min.</p>
  355.      *
  356.      * @param value The value validation is being performed on.
  357.      * @param min The minimum numeric value.
  358.      * @return true if the value is &gt;= the specified minimum.
  359.      */
  360.     public static boolean minValue(final double value, final double min) {
  361.         return value >= min;
  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(final float value, final float min) {
  371.         return value >= min;
  372.     }

  373.     /**
  374.      * <p>Checks if the value is greater than or equal to the min.</p>
  375.      *
  376.      * @param value The value validation is being performed on.
  377.      * @param min The minimum numeric value.
  378.      * @return true if the value is &gt;= the specified minimum.
  379.      */
  380.     public static boolean minValue(final int value, final int min) {
  381.         return value >= min;
  382.     }

  383.     /**
  384.      * <p>Checks if the value is greater than or equal to the min.</p>
  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 long value, final long min) {
  391.         return value >= min;
  392.     }

  393. }