Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
StringLocaleConverter |
|
| 1.7857142857142858;1.786 |
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 | * @author Yauheny Mikulski | |
43 | */ | |
44 | ||
45 | public class StringLocaleConverter extends BaseLocaleConverter { | |
46 | ||
47 | // ----------------------------------------------------- Instance Variables | |
48 | ||
49 | /** All logging goes through this logger */ | |
50 | 23 | private 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 | 0 | this(false); |
66 | 0 | } |
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(boolean locPattern) { | |
77 | ||
78 | 0 | this(Locale.getDefault(), locPattern); |
79 | 0 | } |
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(Locale locale) { | |
89 | ||
90 | 0 | this(locale, false); |
91 | 0 | } |
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(Locale locale, boolean locPattern) { | |
102 | ||
103 | 23 | this(locale, (String) null, locPattern); |
104 | 23 | } |
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 | 0 | this(locale, pattern, false); |
117 | 0 | } |
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 | 23 | super(locale, pattern, locPattern); |
131 | 23 | } |
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 | 0 | this(defaultValue, false); |
145 | 0 | } |
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 | 0 | this(defaultValue, Locale.getDefault(), locPattern); |
159 | 0 | } |
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 | 0 | this(defaultValue, locale, false); |
172 | 0 | } |
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 | 0 | this(defaultValue, locale, null, locPattern); |
186 | 0 | } |
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 | 0 | this(defaultValue, locale, pattern, false); |
200 | 0 | } |
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 | 0 | super(defaultValue, locale, pattern, locPattern); |
215 | 0 | } |
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 | 10 | String result = null; |
234 | ||
235 | 10 | if ((value instanceof Integer) || |
236 | (value instanceof Long) || | |
237 | (value instanceof BigInteger) || | |
238 | (value instanceof Byte) || | |
239 | (value instanceof Short)) { | |
240 | ||
241 | 4 | result = getDecimalFormat(locale, pattern).format(((Number) value).longValue()); |
242 | } | |
243 | 6 | else if ((value instanceof Double) || |
244 | (value instanceof BigDecimal) || | |
245 | (value instanceof Float)) { | |
246 | ||
247 | 2 | result = getDecimalFormat(locale, pattern).format(((Number) value).doubleValue()); |
248 | } | |
249 | 4 | else if (value instanceof Date) { // java.util.Date, java.sql.Date, java.sql.Time, java.sql.Timestamp |
250 | ||
251 | 0 | SimpleDateFormat dateFormat = |
252 | new SimpleDateFormat(pattern, locale); | |
253 | ||
254 | 0 | result = dateFormat.format(value); |
255 | 0 | } |
256 | else { | |
257 | 4 | result = value.toString(); |
258 | } | |
259 | ||
260 | 10 | 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 | 6 | 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 | 6 | if (pattern != null) { |
280 | 0 | if (locPattern) { |
281 | 0 | numberFormat.applyLocalizedPattern(pattern); |
282 | } else { | |
283 | 0 | numberFormat.applyPattern(pattern); |
284 | } | |
285 | } else { | |
286 | 6 | log.debug("No pattern provided, using default."); |
287 | } | |
288 | ||
289 | 6 | return numberFormat; |
290 | } | |
291 | } |