1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package nl.openedge.baritus.converters;
33
34 import java.text.ParseException;
35 import java.util.Locale;
36
37 /***
38 * base class for localized converters
39 * @author Eelco Hillenius
40 */
41 public abstract class BaseLocaleConverter implements LocaleConverter, Formatter
42 {
43
44
45
46 /*** The locale specified to our Constructor, by default - system locale. */
47 protected Locale locale = Locale.getDefault();
48
49 /*** The default pattern specified to our Constructor, if any. */
50 protected String pattern = null;
51
52 /*** The flag indicating whether the given pattern string is localized or not. */
53 protected boolean locPattern = false;
54
55
56
57 /***
58 * Create a {@link LocaleConverter} that will throw a {@link ConversionException}
59 * if a conversion error occurs.
60 * An unlocalized pattern is used for the convertion.
61 *
62 * @param locale The locale
63 * @param pattern The convertion pattern
64 */
65 protected BaseLocaleConverter(Locale locale, String pattern)
66 {
67 this(locale, pattern, false);
68 }
69
70
71 /***
72 * Create a {@link LocaleConverter} that will throw a {@link ConversionException}
73 * if an conversion error occurs.
74 *
75 * @param locale The locale
76 * @param pattern The convertion pattern
77 * @param locPattern Indicate whether the pattern is localized or not
78 */
79 protected BaseLocaleConverter(
80 Locale locale,
81 String pattern,
82 boolean locPattern)
83 {
84
85 if (locale != null)
86 {
87 this.locale = locale;
88 }
89
90 this.pattern = pattern;
91 this.locPattern = locPattern;
92 }
93
94
95
96 /***
97 * Convert the specified locale-sensitive input object into an output object of the
98 * specified type.
99 *
100 * @param value The input object to be converted
101 * @param pattern The pattern is used for the convertion
102 *
103 * @exception ConversionException if conversion cannot be performed
104 * successfully
105 */
106
107 abstract protected Object parse(Object value, String pattern)
108 throws ParseException;
109
110 /***
111 * Convert the specified locale-sensitive input object into an output object.
112 * The default pattern is used for the convertion.
113 *
114 * @param value The input object to be converted
115 *
116 * @exception ConversionException if conversion cannot be performed
117 * successfully
118 */
119 public Object convert(Object value)
120
121 {
122 return convert(value, null);
123 }
124
125 /***
126 * Convert the specified locale-sensitive input object into an output object.
127 *
128 * @param value The input object to be converted
129 * @param pattern The pattern is used for the convertion
130 *
131 * @exception ConversionException if conversion cannot be performed
132 * successfully
133 */
134 public Object convert(Object value, String pattern)
135 {
136 return convert(null, value, pattern);
137 }
138
139 /***
140 * Convert the specified locale-sensitive input object into an output object of the
141 * specified type. The default pattern is used for the convertion.
142 *
143 * @param type Data type to which this value should be converted
144 * @param value The input object to be converted
145 *
146 * @exception ConversionException if conversion cannot be performed
147 * successfully
148 */
149 public Object convert(Class type, Object value)
150 {
151 return convert(type, value, null);
152 }
153
154 /***
155 // * Convert the specified locale-sensitive input object into an output object of the
156 // * specified type.
157 // *
158 // * @param type Data type to which this value should be converted
159 // * @param value The input object to be converted
160 // * @param pattern The pattern is used for the convertion
161 // *
162 // * @exception ConversionException if conversion cannot be performed successfully
163 // */
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188 /***
189 * get the locale
190 * @return Locale
191 */
192 public Locale getLocale()
193 {
194 return locale;
195 }
196
197 /***
198 * get the pattern
199 * @return String
200 */
201 public String getPattern()
202 {
203 return pattern;
204 }
205
206 /***
207 * set the locale
208 * @param locale
209 */
210 public void setLocale(Locale locale)
211 {
212 this.locale = locale;
213 }
214
215 /***
216 * set the pattern
217 * @param string
218 */
219 public void setPattern(String string)
220 {
221 pattern = string;
222 }
223
224 }