AbstractFormatValidator.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.  *      https://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.routines;

  18. import java.io.Serializable;
  19. import java.text.Format;
  20. import java.text.ParsePosition;
  21. import java.util.Locale;

  22. /**
  23.  * <p>Abstract class for <em>Format</em> based Validation.</p>
  24.  *
  25.  * <p>This is a <em>base</em> class for building Date and Number
  26.  *    Validators using format parsing.</p>
  27.  *
  28.  * @since 1.3.0
  29.  */
  30. public abstract class AbstractFormatValidator implements Serializable {

  31.     private static final long serialVersionUID = -4690687565200568258L;

  32.     /**
  33.      * Whether to use strict format.
  34.      */
  35.     private final boolean strict;

  36.     /**
  37.      * Constructs an instance with the specified strict setting.
  38.      *
  39.      * @param strict {@code true} if strict
  40.      *        {@code Format} parsing should be used.
  41.      */
  42.     public AbstractFormatValidator(final boolean strict) {
  43.         this.strict = strict;
  44.     }

  45.     /**
  46.      * <p>Format an object into a {@link String} using
  47.      * the default Locale.</p>
  48.      *
  49.      * @param value The value validation is being performed on.
  50.      * @return The value formatted as a {@link String}.
  51.      */
  52.     public String format(final Object value) {
  53.         return format(value, (String) null, (Locale) null);
  54.     }

  55.     /**
  56.      * <p>Format a value with the specified {@code Format}.</p>
  57.      *
  58.      * @param value The value to be formatted.
  59.      * @param formatter The Format to use.
  60.      * @return The formatted value.
  61.      */
  62.     protected String format(final Object value, final Format formatter) {
  63.         return formatter.format(value);
  64.     }

  65.     /**
  66.      * <p>Format an object into a {@link String} using
  67.      * the specified Locale.</p>
  68.      *
  69.      * @param value The value validation is being performed on.
  70.      * @param locale The locale to use for the Format.
  71.      * @return The value formatted as a {@link String}.
  72.      */
  73.     public String format(final Object value, final Locale locale) {
  74.         return format(value, (String) null, locale);
  75.     }

  76.     /**
  77.      * <p>Format an object into a {@link String} using
  78.      * the specified pattern.</p>
  79.      *
  80.      * @param value The value validation is being performed on.
  81.      * @param pattern The pattern used to format the value.
  82.      * @return The value formatted as a {@link String}.
  83.      */
  84.     public String format(final Object value, final String pattern) {
  85.         return format(value, pattern, (Locale) null);
  86.     }

  87.     /**
  88.      * <p>Format an object using the specified pattern and/or
  89.      *    {@link Locale}.
  90.      *
  91.      * @param value The value validation is being performed on.
  92.      * @param pattern The pattern used to format the value.
  93.      * @param locale The locale to use for the Format.
  94.      * @return The value formatted as a {@link String}.
  95.      */
  96.     public String format(final Object value, final String pattern, final Locale locale) {
  97.         return format(value, getFormat(pattern, locale));
  98.     }

  99.     /**
  100.      * <p>Returns a {@code Format} for the specified <em>pattern</em>
  101.      *    and/or {@link Locale}.</p>
  102.      *
  103.      * @param pattern The pattern used to validate the value against or
  104.      *        {@code null} to use the default for the {@link Locale}.
  105.      * @param locale The locale to use for the currency format, system default if null.
  106.      * @return The {@code NumberFormat} to created.
  107.      */
  108.     protected abstract Format getFormat(String pattern, Locale locale);

  109.     /**
  110.      * <p>Indicates whether validated values should adhere
  111.      *    strictly to the {@code Format} used.</p>
  112.      *
  113.      * <p>Typically implementations of {@code Format}
  114.      *    ignore invalid characters at the end of the value
  115.      *    and just stop parsing. For example parsing a date
  116.      *    value of {@code 01/01/20x0} using a pattern
  117.      *    of {@code dd/MM/yyyy} will result in a year
  118.      *    of {@code 20} if {@code strict} is set
  119.      *    to {@code false}, whereas setting {@code strict}
  120.      *    to {@code true} will cause this value to fail
  121.      *    validation.</p>
  122.      *
  123.      * @return {@code true} if strict {@code Format}
  124.      *         parsing should be used.
  125.      */
  126.     public boolean isStrict() {
  127.         return strict;
  128.     }

  129.     /**
  130.      * <p>Validate using the default {@link Locale}.
  131.      *
  132.      * @param value The value validation is being performed on.
  133.      * @return {@code true} if the value is valid.
  134.      */
  135.     public boolean isValid(final String value) {
  136.         return isValid(value, (String) null, (Locale) null);
  137.     }

  138.     /**
  139.      * <p>Validate using the specified {@link Locale}.
  140.      *
  141.      * @param value The value validation is being performed on.
  142.      * @param locale The locale to use for the Format, defaults to the default
  143.      * @return {@code true} if the value is valid.
  144.      */
  145.     public boolean isValid(final String value, final Locale locale) {
  146.         return isValid(value, (String) null, locale);
  147.     }

  148.     /**
  149.      * <p>Validate using the specified <em>pattern</em>.
  150.      *
  151.      * @param value The value validation is being performed on.
  152.      * @param pattern The pattern used to validate the value against.
  153.      * @return {@code true} if the value is valid.
  154.      */
  155.     public boolean isValid(final String value, final String pattern) {
  156.         return isValid(value, pattern, (Locale) null);
  157.     }

  158.     /**
  159.      * <p>Validate using the specified pattern and/or {@link Locale}.
  160.      *
  161.      * @param value The value validation is being performed on.
  162.      * @param pattern The pattern used to format the value.
  163.      * @param locale The locale to use for the Format, defaults to the default
  164.      * @return {@code true} if the value is valid.
  165.      */
  166.     public abstract boolean isValid(String value, String pattern, Locale locale);

  167.     /**
  168.      * <p>Parse the value with the specified {@code Format}.</p>
  169.      *
  170.      * @param value The value to be parsed.
  171.      * @param formatter The Format to parse the value with.
  172.      * @return The parsed value if valid or {@code null} if invalid.
  173.      */
  174.     protected Object parse(final String value, final Format formatter) {
  175.         final ParsePosition pos = new ParsePosition(0);
  176.         Object parsedValue = formatter.parseObject(value, pos);
  177.         if (pos.getErrorIndex() > -1 || isStrict() && pos.getIndex() < value.length()) {
  178.             return null;
  179.         }
  180.         if (parsedValue != null) {
  181.             parsedValue = processParsedValue(parsedValue, formatter);
  182.         }
  183.         return parsedValue;

  184.     }

  185.     /**
  186.      * <p>Process the parsed value, performing any further validation
  187.      *    and type conversion required.</p>
  188.      *
  189.      * @param value The parsed object created.
  190.      * @param formatter The Format used to parse the value with.
  191.      * @return The parsed value converted to the appropriate type
  192.      *         if valid or {@code null} if invalid.
  193.      */
  194.     protected abstract Object processParsedValue(Object value, Format formatter);

  195. }