KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > beanutils > locale > converters > StringLocaleConverter


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16  
17 package org.apache.commons.beanutils.locale.converters;
18
19 import org.apache.commons.beanutils.locale.BaseLocaleConverter;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import java.math.BigDecimal JavaDoc;
24 import java.math.BigInteger JavaDoc;
25 import java.text.DecimalFormat JavaDoc;
26 import java.text.NumberFormat JavaDoc;
27 import java.text.ParseException JavaDoc;
28 import java.text.SimpleDateFormat JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.Locale JavaDoc;
31
32
33 /**
34  * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter}
35  * implementation that converts an incoming
36  * locale-sensitive object into a <code>java.lang.String</code> object,
37  * optionally using a default value or throwing a
38  * {@link org.apache.commons.beanutils.ConversionException}
39  * if a conversion error occurs.</p>
40  *
41  * @author Yauheny Mikulski
42  */

43
44 public class StringLocaleConverter extends BaseLocaleConverter {
45
46     // ----------------------------------------------------- Instance Variables
47

48     /** All logging goes through this logger */
49     private static Log log = LogFactory.getLog(StringLocaleConverter.class); //msz fix
50

51
52     // ----------------------------------------------------------- Constructors
53

54     /**
55      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
56      * that will throw a {@link org.apache.commons.beanutils.ConversionException}
57      * if a conversion error occurs. The locale is the default locale for
58      * this instance of the Java Virtual Machine and an unlocalized pattern is used
59      * for the convertion.
60      *
61      */

62     public StringLocaleConverter() {
63
64         this(false);
65     }
66
67     /**
68      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
69      * that will throw a {@link org.apache.commons.beanutils.ConversionException}
70      * if a conversion error occurs. The locale is the default locale for
71      * this instance of the Java Virtual Machine.
72      *
73      * @param locPattern Indicate whether the pattern is localized or not
74      */

75     public StringLocaleConverter(boolean locPattern) {
76
77         this(Locale.getDefault(), locPattern);
78     }
79
80     /**
81      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
82      * that will throw a {@link org.apache.commons.beanutils.ConversionException}
83      * if a conversion error occurs. An unlocalized pattern is used for the convertion.
84      *
85      * @param locale The locale
86      */

87     public StringLocaleConverter(Locale JavaDoc locale) {
88
89         this(locale, false);
90     }
91
92     /**
93      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
94      * that will throw a {@link org.apache.commons.beanutils.ConversionException}
95      * if a conversion error occurs.
96      *
97      * @param locale The locale
98      * @param locPattern Indicate whether the pattern is localized or not
99      */

100     public StringLocaleConverter(Locale JavaDoc locale, boolean locPattern) {
101
102         this(locale, (String JavaDoc) null, locPattern);
103     }
104
105     /**
106      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
107      * that will throw a {@link org.apache.commons.beanutils.ConversionException}
108      * if a conversion error occurs. An unlocalized pattern is used for the convertion.
109      *
110      * @param locale The locale
111      * @param pattern The convertion pattern
112      */

113     public StringLocaleConverter(Locale JavaDoc locale, String JavaDoc pattern) {
114
115         this(locale, pattern, false);
116     }
117
118     /**
119      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
120      * that will throw a {@link org.apache.commons.beanutils.ConversionException}
121      * if a conversion error occurs.
122      *
123      * @param locale The locale
124      * @param pattern The convertion pattern
125      * @param locPattern Indicate whether the pattern is localized or not
126      */

127     public StringLocaleConverter(Locale JavaDoc locale, String JavaDoc pattern, boolean locPattern) {
128
129         super(locale, pattern, locPattern);
130     }
131
132     /**
133      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
134      * that will return the specified default value
135      * if a conversion error occurs. The locale is the default locale for
136      * this instance of the Java Virtual Machine and an unlocalized pattern is used
137      * for the convertion.
138      *
139      * @param defaultValue The default value to be returned
140      */

141     public StringLocaleConverter(Object JavaDoc defaultValue) {
142
143         this(defaultValue, false);
144     }
145
146     /**
147      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
148      * that will return the specified default value
149      * if a conversion error occurs. The locale is the default locale for
150      * this instance of the Java Virtual Machine.
151      *
152      * @param defaultValue The default value to be returned
153      * @param locPattern Indicate whether the pattern is localized or not
154      */

155     public StringLocaleConverter(Object JavaDoc defaultValue, boolean locPattern) {
156
157         this(defaultValue, Locale.getDefault(), locPattern);
158     }
159
160     /**
161      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
162      * that will return the specified default value
163      * if a conversion error occurs. An unlocalized pattern is used for the convertion.
164      *
165      * @param defaultValue The default value to be returned
166      * @param locale The locale
167      */

168     public StringLocaleConverter(Object JavaDoc defaultValue, Locale JavaDoc locale) {
169
170         this(defaultValue, locale, false);
171     }
172
173     /**
174      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
175      * that will return the specified default value
176      * if a conversion error occurs.
177      *
178      * @param defaultValue The default value to be returned
179      * @param locale The locale
180      * @param locPattern Indicate whether the pattern is localized or not
181      */

182     public StringLocaleConverter(Object JavaDoc defaultValue, Locale JavaDoc locale, boolean locPattern) {
183
184         this(defaultValue, locale, null, locPattern);
185     }
186
187     /**
188      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
189      * that will return the specified default value
190      * if a conversion error occurs. An unlocalized pattern is used for the convertion.
191      *
192      * @param defaultValue The default value to be returned
193      * @param locale The locale
194      * @param pattern The convertion pattern
195      */

196     public StringLocaleConverter(Object JavaDoc defaultValue, Locale JavaDoc locale, String JavaDoc pattern) {
197
198         this(defaultValue, locale, pattern, false);
199     }
200
201     /**
202      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
203      * that will return the specified default value
204      * if a conversion error occurs.
205      *
206      * @param defaultValue The default value to be returned
207      * @param locale The locale
208      * @param pattern The convertion pattern
209      * @param locPattern Indicate whether the pattern is localized or not
210      */

211     public StringLocaleConverter(Object JavaDoc defaultValue, Locale JavaDoc locale, String JavaDoc pattern, boolean locPattern) {
212
213         super(defaultValue, locale, pattern, locPattern);
214     }
215
216     // --------------------------------------------------------- Methods
217

218     /**
219      * Convert the specified locale-sensitive input object into an output object of the
220      * specified type.
221      *
222      * @param value The input object to be converted
223      * @param pattern The pattern is used for the convertion
224      *
225      * @exception ConversionException if conversion cannot be performed
226      * successfully
227      */

228     protected Object JavaDoc parse(Object JavaDoc value, String JavaDoc pattern) throws ParseException JavaDoc {
229
230         String JavaDoc result = null;
231
232         if ((value instanceof Integer JavaDoc) ||
233                 (value instanceof Long JavaDoc) ||
234                 (value instanceof BigInteger JavaDoc) ||
235                 (value instanceof Byte JavaDoc) ||
236                 (value instanceof Short JavaDoc)) {
237
238             result = getDecimalFormat(locale, pattern).format(((Number JavaDoc) value).longValue());
239         }
240         else if ((value instanceof Double JavaDoc) ||
241                 (value instanceof BigDecimal JavaDoc) ||
242                 (value instanceof Float JavaDoc)) {
243
244             result = getDecimalFormat(locale, pattern).format(((Number JavaDoc) value).doubleValue());
245         }
246         else if (value instanceof Date JavaDoc) { // java.util.Date, java.sql.Date, java.sql.Time, java.sql.Timestamp
247

248             SimpleDateFormat JavaDoc dateFormat =
249                     new SimpleDateFormat JavaDoc(pattern, locale);
250
251             result = dateFormat.format(value);
252         }
253         else {
254             result = value.toString();
255         }
256
257         return result;
258     }
259
260     /**
261      * Make an instance of DecimalFormat.
262      *
263      * @param locale The locale
264      * @param pattern The pattern is used for the convertion
265      *
266      * @exception ConversionException if conversion cannot be performed
267      * successfully
268      */

269     private DecimalFormat JavaDoc getDecimalFormat(Locale JavaDoc locale, String JavaDoc pattern) {
270
271         DecimalFormat JavaDoc numberFormat = (DecimalFormat JavaDoc) NumberFormat.getInstance(locale);
272
273         // if some constructors default pattern to null, it makes only sense to handle null pattern gracefully
274
if (pattern != null) {
275             if (locPattern) {
276                 numberFormat.applyLocalizedPattern(pattern);
277             } else {
278                 numberFormat.applyPattern(pattern);
279             }
280         } else {
281             log.warn("No pattern provided, using default.");
282         }
283
284         return numberFormat;
285     }
286 }
287
Popular Tags