1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.beanutils.locale;
18
19 import org.apache.commons.beanutils.ConversionException;
20 import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
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
41
42 /** All logging goes through this logger */
43 private static Log log = LogFactory.getLog(BaseLocaleConverter.class);
44
45 /** The default value specified to our Constructor, if any. */
46 private Object defaultValue = null;
47
48 /** Should we return the default value on conversion errors? */
49 protected boolean useDefault = false;
50
51 /** The locale specified to our Constructor, by default - system locale. */
52 protected Locale locale = Locale.getDefault();
53
54 /** The default pattern specified to our Constructor, if any. */
55 protected String pattern = null;
56
57 /** The flag indicating whether the given pattern string is localized or not. */
58 protected boolean locPattern = false;
59
60
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 this(null, locale, pattern, false, false);
73 }
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 this(null, locale, pattern, false, locPattern);
86 }
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 this(defaultValue, locale, pattern, false);
100 }
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 this(defaultValue, locale, pattern, true, locPattern);
114 }
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 String pattern, boolean useDefault, boolean locPattern) {
128
129 if (useDefault) {
130 this.defaultValue = defaultValue;
131 this.useDefault = true;
132 }
133
134 if (locale != null) {
135 this.locale = locale;
136 }
137
138 this.pattern = pattern;
139 this.locPattern = locPattern;
140 }
141
142
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 *
151 * @exception ConversionException if conversion cannot be performed
152 * successfully
153 */
154
155 abstract protected Object parse(Object value, String pattern) throws ParseException;
156
157
158 /**
159 * Convert the specified locale-sensitive input object into an output object.
160 * The default pattern is used for the convertion.
161 *
162 * @param value The input object to be converted
163 *
164 * @exception ConversionException if conversion cannot be performed
165 * successfully
166 */
167 public Object convert(Object value) {
168 return convert(value, null);
169 }
170
171 /**
172 * Convert the specified locale-sensitive input object into an output object.
173 *
174 * @param value The input object to be converted
175 * @param pattern The pattern is used for the convertion
176 *
177 * @exception ConversionException if conversion cannot be performed
178 * successfully
179 */
180 public Object convert(Object value, String pattern) {
181 return convert(null, value, pattern);
182 }
183
184 /**
185 * Convert the specified locale-sensitive input object into an output object of the
186 * specified type. The default pattern is used for the convertion.
187 *
188 * @param type Data type to which this value should be converted
189 * @param value The input object to be converted
190 *
191 * @exception ConversionException if conversion cannot be performed
192 * successfully
193 */
194 public Object convert(Class type, Object value) {
195 return convert(type, value, null);
196 }
197
198 /**
199 * Convert the specified locale-sensitive input object into an output object of the
200 * specified type.
201 *
202 * @param type Data type to which this value should be converted
203 * @param value The input object to be converted
204 * @param pattern The pattern is used for the convertion
205 *
206 * @exception ConversionException if conversion cannot be performed
207 * successfully
208 */
209 public Object convert(Class type, Object value, String pattern) {
210 if (value == null) {
211 if (useDefault) {
212 return (defaultValue);
213 } else {
214
215
216 log.debug("Null value specified for conversion, returing null");
217 return null;
218 }
219 }
220
221 try {
222 if (pattern != null) {
223 return parse(value, pattern);
224 } else {
225 return parse(value, this.pattern);
226 }
227 } catch (Exception e) {
228 if (useDefault) {
229 return (defaultValue);
230 } else {
231 throw new ConversionException(e);
232 }
233 }
234 }
235 }