1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;
24 import java.math.BigInteger;
25 import java.text.DecimalFormat;
26 import java.text.NumberFormat;
27 import java.text.ParseException;
28 import java.text.SimpleDateFormat;
29 import java.util.Date;
30 import java.util.Locale;
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
47
48 /** All logging goes through this logger */
49 private static Log log = LogFactory.getLog(StringLocaleConverter.class);
50
51
52
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 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 locale, boolean locPattern) {
101
102 this(locale, (String) 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 locale, String 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 locale, String 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 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 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 defaultValue, Locale 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 defaultValue, Locale 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 defaultValue, Locale locale, String 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 defaultValue, Locale locale, String pattern, boolean locPattern) {
212
213 super(defaultValue, locale, pattern, locPattern);
214 }
215
216
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 parse(Object value, String pattern) throws ParseException {
229
230 String result = null;
231
232 if ((value instanceof Integer) ||
233 (value instanceof Long) ||
234 (value instanceof BigInteger) ||
235 (value instanceof Byte) ||
236 (value instanceof Short)) {
237
238 result = getDecimalFormat(locale, pattern).format(((Number) value).longValue());
239 }
240 else if ((value instanceof Double) ||
241 (value instanceof BigDecimal) ||
242 (value instanceof Float)) {
243
244 result = getDecimalFormat(locale, pattern).format(((Number) value).doubleValue());
245 }
246 else if (value instanceof Date) {
247
248 SimpleDateFormat dateFormat =
249 new SimpleDateFormat(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 getDecimalFormat(Locale locale, String pattern) {
270
271 DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getInstance(locale);
272
273
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 }