    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
    018    package org.apache.commons.beanutils.locale.converters;
    019
    020    import org.apache.commons.beanutils.locale.BaseLocaleConverter;
    021    import org.apache.commons.logging.Log;
    022    import org.apache.commons.logging.LogFactory;
    023
    024    import java.math.BigDecimal;
    025    import java.math.BigInteger;
    026    import java.text.DecimalFormat;
    027    import java.text.NumberFormat;
    028    import java.text.ParseException;
    029    import java.text.SimpleDateFormat;
    030    import java.util.Date;
    031    import java.util.Locale;
    032
    033
    034    /**
    035     * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter}
    036     * implementation that converts an incoming
    037     * locale-sensitive object into a <code>java.lang.String</code> object,
    038     * optionally using a default value or throwing a
    039     * {@link org.apache.commons.beanutils.ConversionException}
    040     * if a conversion error occurs.</p>
    041     *
    042     * @author Yauheny Mikulski
    043     */
    044
    045    public class StringLocaleConverter extends BaseLocaleConverter {
    046
    047        // ----------------------------------------------------- Instance Variables
    048
    049        /** All logging goes through this logger */
    050        private Log log = LogFactory.getLog(StringLocaleConverter.class);     //msz fix
    051
    052
    053        // ----------------------------------------------------------- Constructors
    054
    055        /**
    056         * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    057         * that will throw a {@link org.apache.commons.beanutils.ConversionException}
    058         * if a conversion error occurs. The locale is the default locale for
    059         * this instance of the Java Virtual Machine and an unlocalized pattern is used
    060         * for the convertion.
    061         *
    062         */
    063        public StringLocaleConverter() {
    064
    065            this(false);
    066        }
    067
    068        /**
    069         * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    070         * that will throw a {@link org.apache.commons.beanutils.ConversionException}
    071         * if a conversion error occurs. The locale is the default locale for
    072         * this instance of the Java Virtual Machine.
    073         *
    074         * @param locPattern    Indicate whether the pattern is localized or not
    075         */
    076        public StringLocaleConverter(boolean locPattern) {
    077
    078            this(Locale.getDefault(), locPattern);
    079        }
    080
    081        /**
    082         * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    083         * that will throw a {@link org.apache.commons.beanutils.ConversionException}
    084         * if a conversion error occurs. An unlocalized pattern is used for the convertion.
    085         *
    086         * @param locale        The locale
    087         */
    088        public StringLocaleConverter(Locale locale) {
    089
    090            this(locale, false);
    091        }
    092
    093        /**
    094         * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    095         * that will throw a {@link org.apache.commons.beanutils.ConversionException}
    096         * if a conversion error occurs.
    097         *
    098         * @param locale        The locale
    099         * @param locPattern    Indicate whether the pattern is localized or not
    100         */
    101        public StringLocaleConverter(Locale locale, boolean locPattern) {
    102
    103            this(locale, (String) null, locPattern);
    104        }
    105
    106        /**
    107         * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    108         * that will throw a {@link org.apache.commons.beanutils.ConversionException}
    109         * if a conversion error occurs. An unlocalized pattern is used for the convertion.
    110         *
    111         * @param locale        The locale
    112         * @param pattern       The convertion pattern
    113         */
    114        public StringLocaleConverter(Locale locale, String pattern) {
    115
    116            this(locale, pattern, false);
    117        }
    118
    119        /**
    120         * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    121         * that will throw a {@link org.apache.commons.beanutils.ConversionException}
    122         * if a conversion error occurs.
    123         *
    124         * @param locale        The locale
    125         * @param pattern       The convertion pattern
    126         * @param locPattern    Indicate whether the pattern is localized or not
    127         */
    128        public StringLocaleConverter(Locale locale, String pattern, boolean locPattern) {
    129
    130            super(locale, pattern, locPattern);
    131        }
    132
    133        /**
    134         * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    135         * that will return the specified default value
    136         * if a conversion error occurs. The locale is the default locale for
    137         * this instance of the Java Virtual Machine and an unlocalized pattern is used
    138         * for the convertion.
    139         *
    140         * @param defaultValue  The default value to be returned
    141         */
    142        public StringLocaleConverter(Object defaultValue) {
    143
    144            this(defaultValue, false);
    145        }
    146
    147        /**
    148         * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    149         * that will return the specified default value
    150         * if a conversion error occurs. The locale is the default locale for
    151         * this instance of the Java Virtual Machine.
    152         *
    153         * @param defaultValue  The default value to be returned
    154         * @param locPattern    Indicate whether the pattern is localized or not
    155         */
    156        public StringLocaleConverter(Object defaultValue, boolean locPattern) {
    157
    158            this(defaultValue, Locale.getDefault(), locPattern);
    159        }
    160
    161        /**
    162         * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    163         * that will return the specified default value
    164         * if a conversion error occurs. An unlocalized pattern is used for the convertion.
    165         *
    166         * @param defaultValue  The default value to be returned
    167         * @param locale        The locale
    168         */
    169        public StringLocaleConverter(Object defaultValue, Locale locale) {
    170
    171            this(defaultValue, locale, false);
    172        }
    173
    174        /**
    175         * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    176         * that will return the specified default value
    177         * if a conversion error occurs.
    178         *
    179         * @param defaultValue  The default value to be returned
    180         * @param locale        The locale
    181         * @param locPattern    Indicate whether the pattern is localized or not
    182         */
    183        public StringLocaleConverter(Object defaultValue, Locale locale, boolean locPattern) {
    184
    185            this(defaultValue, locale, null, locPattern);
    186        }
    187
    188        /**
    189         * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    190         * that will return the specified default value
    191         * if a conversion error occurs. An unlocalized pattern is used for the convertion.
    192         *
    193         * @param defaultValue  The default value to be returned
    194         * @param locale        The locale
    195         * @param pattern       The convertion pattern
    196         */
    197        public StringLocaleConverter(Object defaultValue, Locale locale, String pattern) {
    198
    199            this(defaultValue, locale, pattern, false);
    200        }
    201
    202        /**
    203         * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    204         * that will return the specified default value
    205         * if a conversion error occurs.
    206         *
    207         * @param defaultValue  The default value to be returned
    208         * @param locale        The locale
    209         * @param pattern       The convertion pattern
    210         * @param locPattern    Indicate whether the pattern is localized or not
    211         */
    212        public StringLocaleConverter(Object defaultValue, Locale locale, String pattern, boolean locPattern) {
    213
    214            super(defaultValue, locale, pattern, locPattern);
    215        }
    216
    217        // --------------------------------------------------------- Methods
    218
    219        /**
    220         * Convert the specified locale-sensitive input object into an output object of the
    221         * specified type.
    222         *
    223         * @param value The input object to be converted
    224         * @param pattern The pattern is used for the convertion
    225         * @return The converted value
    226         *
    227         * @exception org.apache.commons.beanutils.ConversionException if conversion
    228         * cannot be performed successfully
    229         * @throws ParseException if an error occurs
    230         */
    231        protected Object parse(Object value, String pattern) throws ParseException {
    232
    233            String result = null;
    234
    235            if ((value instanceof Integer) ||
    236                    (value instanceof Long) ||
    237                    (value instanceof BigInteger) ||
    238                    (value instanceof Byte) ||
    239                    (value instanceof Short)) {
    240
    241                result = getDecimalFormat(locale, pattern).format(((Number) value).longValue());
    242            }
    243            else if ((value instanceof Double) ||
    244                    (value instanceof BigDecimal) ||
    245                    (value instanceof Float)) {
    246
    247                result = getDecimalFormat(locale, pattern).format(((Number) value).doubleValue());
    248            }
    249            else if (value instanceof Date) { // java.util.Date, java.sql.Date, java.sql.Time, java.sql.Timestamp
    250
    251                SimpleDateFormat dateFormat =
    252                        new SimpleDateFormat(pattern, locale);
    253
    254                result = dateFormat.format(value);
    255            }
    256            else {
    257                result = value.toString();
    258            }
    259
    260            return result;
    261        }
    262
    263        /**
    264         * Make an instance of DecimalFormat.
    265         *
    266         * @param locale The locale
    267         * @param pattern The pattern is used for the convertion
    268         * @return The format for the locale and pattern
    269         *
    270         * @exception ConversionException if conversion cannot be performed
    271         *  successfully
    272         * @throws ParseException if an error occurs parsing a String to a Number
    273         */
    274        private DecimalFormat getDecimalFormat(Locale locale, String pattern) {
    275
    276            DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getInstance(locale);
    277
    278            // if some constructors default pattern to null, it makes only sense to handle null pattern gracefully
    279            if (pattern != null) {
    280                if (locPattern) {
    281                    numberFormat.applyLocalizedPattern(pattern);
    282                } else {
    283                    numberFormat.applyPattern(pattern);
    284                }
    285            } else {
    286                log.debug("No pattern provided, using default.");
    287            }
    288
    289            return numberFormat;
    290        }
    291    }