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 *      http://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.routines;
018
019import java.io.Serializable;
020import java.text.Format;
021import java.text.ParsePosition;
022import java.util.Locale;
023
024/**
025 * <p>Abstract class for <i>Format</i> based Validation.</p>
026 *
027 * <p>This is a <i>base</i> class for building Date and Number
028 *    Validators using format parsing.</p>
029 *
030 * @since 1.3.0
031 */
032public abstract class AbstractFormatValidator implements Serializable {
033
034    private static final long serialVersionUID = -4690687565200568258L;
035
036    /**
037     * Whether to use strict format.
038     */
039    private final boolean strict;
040
041    /**
042     * Constructs an instance with the specified strict setting.
043     *
044     * @param strict {@code true} if strict
045     *        <code>Format</code> parsing should be used.
046     */
047    public AbstractFormatValidator(final boolean strict) {
048        this.strict = strict;
049    }
050
051    /**
052     * <p>Format an object into a <code>String</code> using
053     * the default Locale.</p>
054     *
055     * @param value The value validation is being performed on.
056     * @return The value formatted as a <code>String</code>.
057     */
058    public String format(final Object value) {
059        return format(value, (String)null, (Locale)null);
060    }
061
062    /**
063     * <p>Format a value with the specified <code>Format</code>.</p>
064     *
065     * @param value The value to be formatted.
066     * @param formatter The Format to use.
067     * @return The formatted value.
068     */
069    protected String format(final Object value, final Format formatter) {
070        return formatter.format(value);
071    }
072
073    /**
074     * <p>Format an object into a <code>String</code> using
075     * the specified Locale.</p>
076     *
077     * @param value The value validation is being performed on.
078     * @param locale The locale to use for the Format.
079     * @return The value formatted as a <code>String</code>.
080     */
081    public String format(final Object value, final Locale locale) {
082        return format(value, (String)null, locale);
083    }
084
085    /**
086     * <p>Format an object into a <code>String</code> using
087     * the specified pattern.</p>
088     *
089     * @param value The value validation is being performed on.
090     * @param pattern The pattern used to format the value.
091     * @return The value formatted as a <code>String</code>.
092     */
093    public String format(final Object value, final String pattern) {
094        return format(value, pattern, (Locale)null);
095    }
096
097    /**
098     * <p>Format an object using the specified pattern and/or
099     *    <code>Locale</code>.
100     *
101     * @param value The value validation is being performed on.
102     * @param pattern The pattern used to format the value.
103     * @param locale The locale to use for the Format.
104     * @return The value formatted as a <code>String</code>.
105     */
106    public String format(final Object value, final String pattern, final Locale locale) {
107        final Format formatter = getFormat(pattern, locale);
108        return format(value, formatter);
109    }
110
111    /**
112     * <p>Returns a <code>Format</code> for the specified <i>pattern</i>
113     *    and/or <code>Locale</code>.</p>
114     *
115     * @param pattern The pattern used to validate the value against or
116     *        <code>null</code> to use the default for the <code>Locale</code>.
117     * @param locale The locale to use for the currency format, system default if null.
118     * @return The <code>NumberFormat</code> to created.
119     */
120    protected abstract Format getFormat(String pattern, Locale locale);
121
122    /**
123     * <p>Indicates whether validated values should adhere
124     *    strictly to the <code>Format</code> used.</p>
125     *
126     * <p>Typically implementations of <code>Format</code>
127     *    ignore invalid characters at the end of the value
128     *    and just stop parsing. For example parsing a date
129     *    value of <code>01/01/20x0</code> using a pattern
130     *    of <code>dd/MM/yyyy</code> will result in a year
131     *    of <code>20</code> if <code>strict</code> is set
132     *    to {@code false}, whereas setting <code>strict</code>
133     *    to {@code true} will cause this value to fail
134     *    validation.</p>
135     *
136     * @return {@code true} if strict <code>Format</code>
137     *         parsing should be used.
138     */
139    public boolean isStrict() {
140        return strict;
141    }
142
143    /**
144     * <p>Validate using the default <code>Locale</code>.
145     *
146     * @param value The value validation is being performed on.
147     * @return {@code true} if the value is valid.
148     */
149    public boolean isValid(final String value) {
150        return isValid(value, (String)null, (Locale)null);
151    }
152
153    /**
154     * <p>Validate using the specified <code>Locale</code>.
155     *
156     * @param value The value validation is being performed on.
157     * @param locale The locale to use for the Format, defaults to the default
158     * @return {@code true} if the value is valid.
159     */
160    public boolean isValid(final String value, final Locale locale) {
161        return isValid(value, (String)null, locale);
162    }
163
164    /**
165     * <p>Validate using the specified <i>pattern</i>.
166     *
167     * @param value The value validation is being performed on.
168     * @param pattern The pattern used to validate the value against.
169     * @return {@code true} if the value is valid.
170     */
171    public boolean isValid(final String value, final String pattern) {
172        return isValid(value, pattern, (Locale)null);
173    }
174
175    /**
176     * <p>Validate using the specified pattern and/or <code>Locale</code>.
177     *
178     * @param value The value validation is being performed on.
179     * @param pattern The pattern used to format the value.
180     * @param locale The locale to use for the Format, defaults to the default
181     * @return {@code true} if the value is valid.
182     */
183    public abstract boolean isValid(String value, String pattern, Locale locale);
184
185    /**
186     * <p>Parse the value with the specified <code>Format</code>.</p>
187     *
188     * @param value The value to be parsed.
189     * @param formatter The Format to parse the value with.
190     * @return The parsed value if valid or <code>null</code> if invalid.
191     */
192    protected Object parse(final String value, final Format formatter) {
193
194        final ParsePosition pos = new ParsePosition(0);
195        Object parsedValue = formatter.parseObject(value, pos);
196        if (pos.getErrorIndex() > -1) {
197            return null;
198        }
199
200        if (isStrict() && pos.getIndex() < value.length()) {
201            return null;
202        }
203
204        if (parsedValue != null) {
205            parsedValue = processParsedValue(parsedValue, formatter);
206        }
207
208        return parsedValue;
209
210    }
211
212    /**
213     * <p>Process the parsed value, performing any further validation
214     *    and type conversion required.</p>
215     *
216     * @param value The parsed object created.
217     * @param formatter The Format used to parse the value with.
218     * @return The parsed value converted to the appropriate type
219     *         if valid or <code>null</code> if invalid.
220     */
221    protected abstract Object processParsedValue(Object value, Format formatter);
222
223}