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.  *      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.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 <i>Format</i> based Validation.</p>
  24.  *
  25.  * <p>This is a <i>base</i> 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</code> parsing should be used.
  41.      */
  42.     public AbstractFormatValidator(final boolean strict) {
  43.         this.strict = strict;
  44.     }

  45.     /**
  46.      * <p>Format an object into a <code>String</code> using
  47.      * the default Locale.</p>
  48.      *
  49.      * @param value The value validation is being performed on.
  50.      * @return The value formatted as a <code>String</code>.
  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</code>.</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 <code>String</code> 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 <code>String</code>.
  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 <code>String</code> 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 <code>String</code>.
  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.      *    <code>Locale</code>.
  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 <code>String</code>.
  95.      */
  96.     public String format(final Object value, final String pattern, final Locale locale) {
  97.         final Format formatter = getFormat(pattern, locale);
  98.         return format(value, formatter);
  99.     }

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

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

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

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

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

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

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

  188.     }

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

  199. }