Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BaseLocaleConverter |
|
| 2.5;2.5 |
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; | |
19 | ||
20 | import org.apache.commons.beanutils.ConversionException; | |
21 | import org.apache.commons.logging.Log; | |
22 | import org.apache.commons.logging.LogFactory; | |
23 | ||
24 | import java.text.ParseException; | |
25 | import java.util.Locale; | |
26 | ||
27 | ||
28 | /** | |
29 | * <p>The base class for all standart type locale-sensitive converters. | |
30 | * It has {@link LocaleConverter} and {@link org.apache.commons.beanutils.Converter} implementations, | |
31 | * that convert an incoming locale-sensitive Object into an object of correspond type, | |
32 | * optionally using a default value or throwing a {@link ConversionException} | |
33 | * if a conversion error occurs.</p> | |
34 | * | |
35 | * @author Yauheny Mikulski | |
36 | */ | |
37 | ||
38 | public abstract class BaseLocaleConverter implements LocaleConverter { | |
39 | ||
40 | // ----------------------------------------------------- Instance Variables | |
41 | ||
42 | /** All logging goes through this logger */ | |
43 | 512 | private Log log = LogFactory.getLog(BaseLocaleConverter.class); |
44 | ||
45 | /** The default value specified to our Constructor, if any. */ | |
46 | 512 | private Object defaultValue = null; |
47 | ||
48 | /** Should we return the default value on conversion errors? */ | |
49 | 512 | protected boolean useDefault = false; |
50 | ||
51 | /** The locale specified to our Constructor, by default - system locale. */ | |
52 | 512 | protected Locale locale = Locale.getDefault(); |
53 | ||
54 | /** The default pattern specified to our Constructor, if any. */ | |
55 | 512 | protected String pattern = null; |
56 | ||
57 | /** The flag indicating whether the given pattern string is localized or not. */ | |
58 | 512 | protected boolean locPattern = false; |
59 | ||
60 | // ----------------------------------------------------------- Constructors | |
61 | ||
62 | /** | |
63 | * Create a {@link LocaleConverter} that will throw a {@link ConversionException} | |
64 | * if a conversion error occurs. | |
65 | * An unlocalized pattern is used for the convertion. | |
66 | * | |
67 | * @param locale The locale | |
68 | * @param pattern The convertion pattern | |
69 | */ | |
70 | protected BaseLocaleConverter(Locale locale, String pattern) { | |
71 | ||
72 | 0 | this(null, locale, pattern, false, false); |
73 | 0 | } |
74 | ||
75 | /** | |
76 | * Create a {@link LocaleConverter} that will throw a {@link ConversionException} | |
77 | * if a conversion error occurs. | |
78 | * | |
79 | * @param locale The locale | |
80 | * @param pattern The convertion pattern | |
81 | * @param locPattern Indicate whether the pattern is localized or not | |
82 | */ | |
83 | protected BaseLocaleConverter(Locale locale, String pattern, boolean locPattern) { | |
84 | ||
85 | 476 | this(null, locale, pattern, false, locPattern); |
86 | 476 | } |
87 | ||
88 | /** | |
89 | * Create a {@link LocaleConverter} that will return the specified default value | |
90 | * if a conversion error occurs. | |
91 | * An unlocalized pattern is used for the convertion. | |
92 | * | |
93 | * @param defaultValue The default value to be returned | |
94 | * @param locale The locale | |
95 | * @param pattern The convertion pattern | |
96 | */ | |
97 | protected BaseLocaleConverter(Object defaultValue, Locale locale, String pattern) { | |
98 | ||
99 | 0 | this(defaultValue, locale, pattern, false); |
100 | 0 | } |
101 | ||
102 | /** | |
103 | * Create a {@link LocaleConverter} that will return the specified default value | |
104 | * if a conversion error occurs. | |
105 | * | |
106 | * @param defaultValue The default value to be returned | |
107 | * @param locale The locale | |
108 | * @param pattern The convertion pattern | |
109 | * @param locPattern Indicate whether the pattern is localized or not | |
110 | */ | |
111 | protected BaseLocaleConverter(Object defaultValue, Locale locale, String pattern, boolean locPattern) { | |
112 | ||
113 | 36 | this(defaultValue, locale, pattern, true, locPattern); |
114 | 36 | } |
115 | ||
116 | /** | |
117 | * Create a {@link LocaleConverter} that will return the specified default value | |
118 | * or throw a {@link ConversionException} if a conversion error occurs. | |
119 | * | |
120 | * @param defaultValue The default value to be returned | |
121 | * @param locale The locale | |
122 | * @param pattern The convertion pattern | |
123 | * @param useDefault Indicate whether the default value is used or not | |
124 | * @param locPattern Indicate whether the pattern is localized or not | |
125 | */ | |
126 | private BaseLocaleConverter(Object defaultValue, Locale locale, | |
127 | 512 | String pattern, boolean useDefault, boolean locPattern) { |
128 | ||
129 | 512 | if (useDefault) { |
130 | 36 | this.defaultValue = defaultValue; |
131 | 36 | this.useDefault = true; |
132 | } | |
133 | ||
134 | 512 | if (locale != null) { |
135 | 512 | this.locale = locale; |
136 | } | |
137 | ||
138 | 512 | this.pattern = pattern; |
139 | 512 | this.locPattern = locPattern; |
140 | 512 | } |
141 | ||
142 | // --------------------------------------------------------- Methods | |
143 | ||
144 | /** | |
145 | * Convert the specified locale-sensitive input object into an output object of the | |
146 | * specified type. | |
147 | * | |
148 | * @param value The input object to be converted | |
149 | * @param pattern The pattern is used for the convertion | |
150 | * @return The converted value | |
151 | * | |
152 | * @exception ParseException if conversion cannot be performed | |
153 | * successfully | |
154 | */ | |
155 | ||
156 | abstract protected Object parse(Object value, String pattern) throws ParseException; | |
157 | ||
158 | ||
159 | /** | |
160 | * Convert the specified locale-sensitive input object into an output object. | |
161 | * The default pattern is used for the convertion. | |
162 | * | |
163 | * @param value The input object to be converted | |
164 | * @return The converted value | |
165 | * | |
166 | * @exception ConversionException if conversion cannot be performed | |
167 | * successfully | |
168 | */ | |
169 | public Object convert(Object value) { | |
170 | 296 | return convert(value, null); |
171 | } | |
172 | ||
173 | /** | |
174 | * Convert the specified locale-sensitive input object into an output object. | |
175 | * | |
176 | * @param value The input object to be converted | |
177 | * @param pattern The pattern is used for the convertion | |
178 | * @return The converted value | |
179 | * | |
180 | * @exception ConversionException if conversion cannot be performed | |
181 | * successfully | |
182 | */ | |
183 | public Object convert(Object value, String pattern) { | |
184 | 398 | return convert(null, value, pattern); |
185 | } | |
186 | ||
187 | /** | |
188 | * Convert the specified locale-sensitive input object into an output object of the | |
189 | * specified type. The default pattern is used for the convertion. | |
190 | * | |
191 | * @param type Data type to which this value should be converted | |
192 | * @param value The input object to be converted | |
193 | * @return The converted value | |
194 | * | |
195 | * @exception ConversionException if conversion cannot be performed | |
196 | * successfully | |
197 | */ | |
198 | public Object convert(Class type, Object value) { | |
199 | 1 | return convert(type, value, null); |
200 | } | |
201 | ||
202 | /** | |
203 | * Convert the specified locale-sensitive input object into an output object of the | |
204 | * specified type. | |
205 | * | |
206 | * @param type Data is type to which this value should be converted | |
207 | * @param value is the input object to be converted | |
208 | * @param pattern is the pattern is used for the conversion; if null is | |
209 | * passed then the default pattern associated with the converter object | |
210 | * will be used. | |
211 | * @return The converted value | |
212 | * | |
213 | * @exception ConversionException if conversion cannot be performed | |
214 | * successfully | |
215 | */ | |
216 | public Object convert(Class type, Object value, String pattern) { | |
217 | 450 | if (value == null) { |
218 | 91 | if (useDefault) { |
219 | 36 | return (defaultValue); |
220 | } else { | |
221 | // symmetric beanutils function allows null | |
222 | // so do not: throw new ConversionException("No value specified"); | |
223 | 55 | log.debug("Null value specified for conversion, returing null"); |
224 | 55 | return null; |
225 | } | |
226 | } | |
227 | ||
228 | try { | |
229 | 359 | if (pattern != null) { |
230 | 114 | return parse(value, pattern); |
231 | } else { | |
232 | 245 | return parse(value, this.pattern); |
233 | } | |
234 | 115 | } catch (Exception e) { |
235 | 115 | if (useDefault) { |
236 | 42 | return (defaultValue); |
237 | } else { | |
238 | 73 | if (e instanceof ConversionException) { |
239 | 13 | throw (ConversionException)e; |
240 | } | |
241 | 60 | throw new ConversionException(e); |
242 | } | |
243 | } | |
244 | } | |
245 | } |