View Javadoc
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  
18  package org.apache.commons.beanutils.locale.converters;
19  
20  import org.apache.commons.beanutils.locale.BaseLocaleConverter;
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import java.math.BigDecimal;
25  import java.math.BigInteger;
26  import java.text.DecimalFormat;
27  import java.text.NumberFormat;
28  import java.text.ParseException;
29  import java.text.SimpleDateFormat;
30  import java.util.Date;
31  import java.util.Locale;
32  
33  
34  /**
35   * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter}
36   * implementation that converts an incoming
37   * locale-sensitive object into a <code>java.lang.String</code> object,
38   * optionally using a default value or throwing a
39   * {@link org.apache.commons.beanutils.ConversionException}
40   * if a conversion error occurs.</p>
41   *
42   * @version $Id$
43   */
44  
45  public class StringLocaleConverter extends BaseLocaleConverter {
46  
47      // ----------------------------------------------------- Instance Variables
48  
49      /** All logging goes through this logger */
50      private final Log log = LogFactory.getLog(StringLocaleConverter.class);     //msz fix
51  
52  
53      // ----------------------------------------------------------- Constructors
54  
55      /**
56       * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
57       * that will throw a {@link org.apache.commons.beanutils.ConversionException}
58       * if a conversion error occurs. The locale is the default locale for
59       * this instance of the Java Virtual Machine and an unlocalized pattern is used
60       * for the convertion.
61       *
62       */
63      public StringLocaleConverter() {
64  
65          this(false);
66      }
67  
68      /**
69       * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
70       * that will throw a {@link org.apache.commons.beanutils.ConversionException}
71       * if a conversion error occurs. The locale is the default locale for
72       * this instance of the Java Virtual Machine.
73       *
74       * @param locPattern    Indicate whether the pattern is localized or not
75       */
76      public StringLocaleConverter(final boolean locPattern) {
77  
78          this(Locale.getDefault(), locPattern);
79      }
80  
81      /**
82       * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
83       * that will throw a {@link org.apache.commons.beanutils.ConversionException}
84       * if a conversion error occurs. An unlocalized pattern is used for the convertion.
85       *
86       * @param locale        The locale
87       */
88      public StringLocaleConverter(final Locale locale) {
89  
90          this(locale, false);
91      }
92  
93      /**
94       * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
95       * that will throw a {@link org.apache.commons.beanutils.ConversionException}
96       * if a conversion error occurs.
97       *
98       * @param locale        The locale
99       * @param locPattern    Indicate whether the pattern is localized or not
100      */
101     public StringLocaleConverter(final Locale locale, final 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(final Locale locale, final 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(final Locale locale, final String pattern, final 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(final 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(final Object defaultValue, final 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(final Object defaultValue, final 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(final Object defaultValue, final Locale locale, final 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(final Object defaultValue, final Locale locale, final 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(final Object defaultValue, final Locale locale, final String pattern, final 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      * @throws org.apache.commons.beanutils.ConversionException if conversion
228      * cannot be performed successfully
229      * @throws ParseException if an error occurs
230      */
231     @Override
232     protected Object parse(final Object value, final String pattern) throws ParseException {
233 
234         String result = null;
235 
236         if ((value instanceof Integer) ||
237                 (value instanceof Long) ||
238                 (value instanceof BigInteger) ||
239                 (value instanceof Byte) ||
240                 (value instanceof Short)) {
241 
242             result = getDecimalFormat(locale, pattern).format(((Number) value).longValue());
243         }
244         else if ((value instanceof Double) ||
245                 (value instanceof BigDecimal) ||
246                 (value instanceof Float)) {
247 
248             result = getDecimalFormat(locale, pattern).format(((Number) value).doubleValue());
249         }
250         else if (value instanceof Date) { // java.util.Date, java.sql.Date, java.sql.Time, java.sql.Timestamp
251 
252             final SimpleDateFormat dateFormat =
253                     new SimpleDateFormat(pattern, locale);
254 
255             result = dateFormat.format(value);
256         }
257         else {
258             result = value.toString();
259         }
260 
261         return result;
262     }
263 
264     /**
265      * Make an instance of DecimalFormat.
266      *
267      * @param locale The locale
268      * @param pattern The pattern is used for the convertion
269      * @return The format for the locale and pattern
270      *
271      * @throws ConversionException if conversion cannot be performed
272      *  successfully
273      * @throws ParseException if an error occurs parsing a String to a Number
274      */
275     private DecimalFormat getDecimalFormat(final Locale locale, final String pattern) {
276 
277         final DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getInstance(locale);
278 
279         // if some constructors default pattern to null, it makes only sense to handle null pattern gracefully
280         if (pattern != null) {
281             if (locPattern) {
282                 numberFormat.applyLocalizedPattern(pattern);
283             } else {
284                 numberFormat.applyPattern(pattern);
285             }
286         } else {
287             log.debug("No pattern provided, using default.");
288         }
289 
290         return numberFormat;
291     }
292 }