    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 */
    017
    018package org.apache.commons.beanutils.locale;
    019
    020import java.text.ParseException;
    021import java.util.Locale;
    022
    023import org.apache.commons.beanutils.ConversionException;
    024import org.apache.commons.beanutils.ConvertUtils;
    025import org.apache.commons.logging.Log;
    026import org.apache.commons.logging.LogFactory;
    027
    028/**
    029 * <p>The base class for all standart type locale-sensitive converters.
    030 * It has {@link LocaleConverter} and {@link org.apache.commons.beanutils.Converter} implementations,
    031 * that convert an incoming locale-sensitive Object into an object of correspond type,
    032 * optionally using a default value or throwing a {@link ConversionException}
    033 * if a conversion error occurs.</p>
    034 *
    035 */
    036
    037public abstract class BaseLocaleConverter implements LocaleConverter {
    038
    039    /**
    040     * Checks whether the result of a conversion is conform to the specified
    041     * target type. If this is the case, the passed in result object is cast to
    042     * the correct target type. Otherwise, an exception is thrown.
    043     *
    044     * @param <T> the desired result type
    045     * @param type the target class of the conversion
    046     * @param result the conversion result object
    047     * @return the result cast to the target class
    048     * @throws ConversionException if the result object is not compatible with
    049     *         the target type
    050     */
    051    private static <T> T checkConversionResult(final Class<T> type, final Object result) {
    052        if (type == null) {
    053            // in this case we cannot do much; the result object is returned
    054            @SuppressWarnings("unchecked")
    055            final
    056            T temp = (T) result;
    057            return temp;
    058        }
    059
    060        if (result == null) {
    061            return null;
    062        }
    063        if (type.isInstance(result)) {
    064            return type.cast(result);
    065        }
    066        throw new ConversionException("Unsupported target type: " + type);
    067    }
    068
    069    /** All logging goes through this logger */
    070    private final Log log = LogFactory.getLog(BaseLocaleConverter.class);
    071
    072    /** The default value specified to our Constructor, if any. */
    073    private Object defaultValue;
    074
    075    /** Should we return the default value on conversion errors? */
    076    protected boolean useDefault;
    077
    078    /** The locale specified to our Constructor, by default - system locale. */
    079    protected Locale locale = Locale.getDefault();
    080
    081    /** The default pattern specified to our Constructor, if any. */
    082    protected String pattern;
    083
    084    /** The flag indicating whether the given pattern string is localized or not. */
    085    protected boolean locPattern;
    086
    087    /**
    088     * Create a {@link LocaleConverter} that will throw a {@link ConversionException}
    089     * if a conversion error occurs.
    090     * An unlocalized pattern is used for the convertion.
    091     *
    092     * @param locale        The locale
    093     * @param pattern       The convertion pattern
    094     */
    095    protected BaseLocaleConverter(final Locale locale, final String pattern) {
    096
    097        this(null, locale, pattern, false, false);
    098    }
    099
    100    /**
    101     * Create a {@link LocaleConverter} that will throw a {@link ConversionException}
    102     * if a conversion error occurs.
    103     *
    104     * @param locale        The locale
    105     * @param pattern       The convertion pattern
    106     * @param locPattern    Indicate whether the pattern is localized or not
    107     */
    108    protected BaseLocaleConverter(final Locale locale, final String pattern, final boolean locPattern) {
    109
    110        this(null, locale, pattern, false, locPattern);
    111    }
    112
    113    /**
    114     * Create a {@link LocaleConverter} that will return the specified default value
    115     * if a conversion error occurs.
    116     * An unlocalized pattern is used for the convertion.
    117     *
    118     * @param defaultValue  The default value to be returned
    119     * @param locale        The locale
    120     * @param pattern       The convertion pattern
    121     */
    122    protected BaseLocaleConverter(final Object defaultValue, final Locale locale, final String pattern) {
    123
    124        this(defaultValue, locale, pattern, false);
    125    }
    126
    127    /**
    128     * Create a {@link LocaleConverter} that will return the specified default value
    129     * if a conversion error occurs.
    130     *
    131     * @param defaultValue  The default value to be returned
    132     * @param locale        The locale
    133     * @param pattern       The convertion pattern
    134     * @param locPattern    Indicate whether the pattern is localized or not
    135     */
    136    protected BaseLocaleConverter(final Object defaultValue, final Locale locale, final String pattern, final boolean locPattern) {
    137
    138        this(defaultValue, locale, pattern, true, locPattern);
    139    }
    140
    141    /**
    142     * Create a {@link LocaleConverter} that will return the specified default value
    143     * or throw a {@link ConversionException} if a conversion error occurs.
    144     *
    145     * @param defaultValue  The default value to be returned
    146     * @param locale        The locale
    147     * @param pattern       The convertion pattern
    148     * @param useDefault    Indicate whether the default value is used or not
    149     * @param locPattern    Indicate whether the pattern is localized or not
    150     */
    151    private BaseLocaleConverter(final Object defaultValue, final Locale locale,
    152                                final String pattern, final boolean useDefault, final boolean locPattern) {
    153
    154        if (useDefault) {
    155            this.defaultValue = defaultValue;
    156            this.useDefault = true;
    157        }
    158
    159        if (locale != null) {
    160            this.locale = locale;
    161        }
    162
    163        this.pattern = pattern;
    164        this.locPattern = locPattern;
    165    }
    166
    167    /**
    168     * Convert the specified locale-sensitive input object into an output object of the
    169     * specified type. The default pattern is used for the convertion.
    170     *
    171     * @param <T> The desired target type of the conversion
    172     * @param type Data type to which this value should be converted
    173     * @param value The input object to be converted
    174     * @return The converted value
    175     * @throws ConversionException if conversion cannot be performed
    176     *  successfully
    177     */
    178    @Override
    179    public <T> T convert(final Class<T> type, final Object value) {
    180        return convert(type, value, null);
    181    }
    182
    183    /**
    184     * Convert the specified locale-sensitive input object into an output object of the
    185     * specified type.
    186     *
    187     * @param <T> The desired target type of the conversion
    188     * @param type Data is type to which this value should be converted
    189     * @param value is the input object to be converted
    190     * @param pattern is the pattern is used for the conversion; if null is
    191     * passed then the default pattern associated with the converter object
    192     * will be used.
    193     * @return The converted value
    194     * @throws ConversionException if conversion cannot be performed
    195     *  successfully
    196     */
    197    @Override
    198    public <T> T convert(final Class<T> type, final Object value, final String pattern) {
    199        final Class<T> targetType = ConvertUtils.primitiveToWrapper(type);
    200        if (value == null) {
    201            if (useDefault) {
    202                return getDefaultAs(targetType);
    203            }
    204            // symmetric beanutils function allows null
    205            // so do not: throw new ConversionException("No value specified");
    206            log.debug("Null value specified for conversion, returing null");
    207            return null;
    208        }
    209
    210        try {
    211            if (pattern != null) {
    212                return checkConversionResult(targetType, parse(value, pattern));
    213            }
    214            return checkConversionResult(targetType, parse(value, this.pattern));
    215        } catch (final Exception e) {
    216            if (useDefault) {
    217                return getDefaultAs(targetType);
    218            }
    219            if (e instanceof ConversionException) {
    220                throw (ConversionException)e;
    221            }
    222            throw new ConversionException(e);
    223        }
    224    }
    225
    226    /**
    227     * Convert the specified locale-sensitive input object into an output object.
    228     * The default pattern is used for the conversion.
    229     *
    230     * @param value The input object to be converted
    231     * @return The converted value
    232     * @throws ConversionException if conversion cannot be performed
    233     *  successfully
    234     */
    235    public Object convert(final Object value) {
    236        return convert(value, null);
    237    }
    238
    239    /**
    240     * Convert the specified locale-sensitive input object into an output object.
    241     *
    242     * @param value The input object to be converted
    243     * @param pattern The pattern is used for the conversion
    244     * @return The converted value
    245     * @throws ConversionException if conversion cannot be performed
    246     *  successfully
    247     */
    248    public Object convert(final Object value, final String pattern) {
    249        return convert(null, value, pattern);
    250    }
    251
    252    /**
    253     * Returns the default object specified for this converter cast for the
    254     * given target type. If the default value is not conform to the given type,
    255     * an exception is thrown.
    256     *
    257     * @param <T> the desired target type
    258     * @param type the target class of the conversion
    259     * @return the default value in the given target type
    260     * @throws ConversionException if the default object is not compatible with
    261     *         the target type
    262     */
    263    private <T> T getDefaultAs(final Class<T> type) {
    264        return checkConversionResult(type, defaultValue);
    265    }
    266
    267    /**
    268     * Convert the specified locale-sensitive input object into an output object of the
    269     * specified type.
    270     *
    271     * @param value The input object to be converted
    272     * @param pattern The pattern is used for the convertion
    273     * @return The converted value
    274     * @throws ParseException if conversion cannot be performed
    275     *  successfully
    276     */
    277
    278    abstract protected Object parse(Object value, String pattern) throws ParseException;
    279}