1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.commons.convert; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.io.InputStream; |
23 | |
import java.io.Reader; |
24 | |
import java.nio.ByteBuffer; |
25 | |
import java.nio.charset.Charset; |
26 | |
import java.sql.Blob; |
27 | |
import java.sql.Clob; |
28 | |
import java.text.DecimalFormat; |
29 | |
import java.text.SimpleDateFormat; |
30 | |
import java.util.Locale; |
31 | |
import java.util.UUID; |
32 | |
import java.util.regex.Pattern; |
33 | |
|
34 | |
|
35 | 1 | public class MiscConverters implements ConverterLoader { |
36 | |
|
37 | |
public void loadConverters() { |
38 | 1 | Converters.loadContainedConverters(MiscConverters.class); |
39 | 1 | Converters.registerConverter(new GenericToStringConverter<Locale>(Locale.class)); |
40 | 1 | Converters.registerConverter(new GenericToStringConverter<UUID>(UUID.class)); |
41 | 1 | Converters.registerConverter(new GenericToStringConverter<Pattern>(Pattern.class)); |
42 | 1 | } |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | public static class BlobToByteArray extends AbstractConverter<Blob, byte[]> { |
49 | |
public BlobToByteArray() { |
50 | 1 | super(Blob.class, byte[].class); |
51 | 1 | } |
52 | |
|
53 | |
public byte[] convert(Blob obj) throws ConversionException { |
54 | 0 | InputStream inStream = null; |
55 | |
try { |
56 | 0 | inStream = obj.getBinaryStream(); |
57 | 0 | int blobLength = (int) obj.length(); |
58 | 0 | byte[] byteBuffer = new byte[blobLength]; |
59 | 0 | int offset = 0; |
60 | 0 | int bytesRead = inStream.read(byteBuffer, offset, blobLength); |
61 | 0 | while (bytesRead > 0) { |
62 | 0 | offset += bytesRead; |
63 | 0 | bytesRead = inStream.read(byteBuffer, offset, blobLength); |
64 | |
} |
65 | 0 | return byteBuffer; |
66 | 0 | } catch (Exception e) { |
67 | 0 | throw new ConversionException(e); |
68 | |
} |
69 | |
finally { |
70 | 0 | if (inStream != null) { |
71 | |
try { |
72 | 0 | inStream.close(); |
73 | 0 | } catch (IOException e) {} |
74 | |
} |
75 | |
} |
76 | |
} |
77 | |
} |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | 0 | public static class ByteArrayToByteBuffer extends AbstractConverter<byte[], ByteBuffer> { |
84 | |
public ByteArrayToByteBuffer() { |
85 | 1 | super(byte[].class, ByteBuffer.class); |
86 | 1 | } |
87 | |
|
88 | |
public ByteBuffer convert(byte[] obj) throws ConversionException { |
89 | |
try { |
90 | 0 | return ByteBuffer.wrap(obj); |
91 | 0 | } catch (Exception e) { |
92 | 0 | throw new ConversionException(e); |
93 | |
} |
94 | |
} |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | 0 | public static class ByteBufferToByteArray extends AbstractConverter<ByteBuffer, byte[]> { |
102 | |
public ByteBufferToByteArray() { |
103 | 1 | super(ByteBuffer.class, byte[].class); |
104 | 1 | } |
105 | |
|
106 | |
public byte[] convert(ByteBuffer obj) throws ConversionException { |
107 | |
try { |
108 | 0 | return obj.hasArray() ? obj.array() : null; |
109 | 0 | } catch (Exception e) { |
110 | 0 | throw new ConversionException(e); |
111 | |
} |
112 | |
} |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | 0 | public static class CharsetToString extends AbstractConverter<Charset, String> { |
120 | |
public CharsetToString() { |
121 | 1 | super(Charset.class, String.class); |
122 | 1 | } |
123 | |
|
124 | |
public String convert(Charset obj) throws ConversionException { |
125 | 0 | return obj.name(); |
126 | |
} |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | 0 | public static class ClobToString extends AbstractConverter<Clob, String> { |
134 | |
public ClobToString() { |
135 | 1 | super(Clob.class, String.class); |
136 | 1 | } |
137 | |
|
138 | |
public String convert(Clob obj) throws ConversionException { |
139 | 0 | Reader clobReader = null; |
140 | |
try { |
141 | 0 | clobReader = obj.getCharacterStream(); |
142 | 0 | int clobLength = (int) obj.length(); |
143 | 0 | char[] charBuffer = new char[clobLength]; |
144 | 0 | int offset = 0; |
145 | 0 | int charsRead = clobReader.read(charBuffer, offset, clobLength); |
146 | 0 | while (charsRead > 0) { |
147 | 0 | offset += charsRead; |
148 | 0 | charsRead = clobReader.read(charBuffer, offset, clobLength); |
149 | |
} |
150 | 0 | return new String(charBuffer); |
151 | 0 | } catch (Exception e) { |
152 | 0 | throw new ConversionException(e); |
153 | |
} |
154 | |
finally { |
155 | 0 | if (clobReader != null) { |
156 | |
try { |
157 | 0 | clobReader.close(); |
158 | 0 | } catch (IOException e) {} |
159 | |
} |
160 | |
} |
161 | |
} |
162 | |
} |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | 0 | public static class DecimalFormatToString extends AbstractConverter<DecimalFormat, String> { |
169 | |
public DecimalFormatToString() { |
170 | 1 | super(DecimalFormat.class, String.class); |
171 | 1 | } |
172 | |
|
173 | |
public String convert(DecimalFormat obj) throws ConversionException { |
174 | 0 | return obj.toPattern(); |
175 | |
} |
176 | |
} |
177 | |
|
178 | 0 | public static class EnumToString extends AbstractConverter<Enum<?>, String> { |
179 | |
public EnumToString() { |
180 | 1 | super(Enum.class, String.class); |
181 | 1 | } |
182 | |
|
183 | |
public boolean canConvert(Class<?> sourceClass, Class<?> targetClass) { |
184 | 60 | return Enum.class.isAssignableFrom(sourceClass) && String.class.isAssignableFrom(targetClass); |
185 | |
} |
186 | |
|
187 | |
public String convert(Enum<?> obj) throws ConversionException { |
188 | 0 | return obj.toString(); |
189 | |
} |
190 | |
|
191 | |
public Class<? super Enum<?>> getSourceClass() { |
192 | 1 | return null; |
193 | |
} |
194 | |
} |
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
public static class NotAConverter { |
201 | 0 | protected NotAConverter() { |
202 | 0 | throw new Error("Should not be loaded"); |
203 | |
} |
204 | |
} |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | 0 | public static class SimpleDateFormatToString extends AbstractConverter<SimpleDateFormat, String> { |
211 | |
public SimpleDateFormatToString() { |
212 | 1 | super(SimpleDateFormat.class, String.class); |
213 | 1 | } |
214 | |
|
215 | |
public String convert(SimpleDateFormat obj) throws ConversionException { |
216 | 0 | return obj.toPattern(); |
217 | |
} |
218 | |
} |
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | 0 | public static class StringToCharset extends AbstractConverter<String, Charset> { |
225 | |
public StringToCharset() { |
226 | 1 | super(String.class, Charset.class); |
227 | 1 | } |
228 | |
|
229 | |
public Charset convert(String obj) throws ConversionException { |
230 | 0 | return Charset.forName(obj); |
231 | |
} |
232 | |
} |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | 0 | public static class StringToDecimalFormat extends AbstractConverter<String, DecimalFormat> { |
239 | |
public StringToDecimalFormat() { |
240 | 1 | super(String.class, DecimalFormat.class); |
241 | 1 | } |
242 | |
|
243 | |
public DecimalFormat convert(String obj) throws ConversionException { |
244 | 0 | return new DecimalFormat(obj); |
245 | |
} |
246 | |
} |
247 | |
|
248 | 0 | private static class StringToEnum<E extends Enum<E>> extends AbstractConverter<String, E> { |
249 | |
public StringToEnum() { |
250 | 0 | super(String.class, Enum.class); |
251 | 0 | } |
252 | |
|
253 | |
public boolean canConvert(Class<?> sourceClass, Class<?> targetClass) { |
254 | 0 | return String.class.isAssignableFrom(sourceClass) && Enum.class.isAssignableFrom(targetClass); |
255 | |
} |
256 | |
|
257 | |
public E convert(String obj) throws ConversionException { |
258 | 0 | throw new UnsupportedOperationException(); |
259 | |
} |
260 | |
|
261 | |
public Class<? super Enum> getTargetClass() { |
262 | 0 | return null; |
263 | |
} |
264 | |
} |
265 | |
|
266 | 1 | public static class StringToEnumConverterCreator implements ConverterCreator, ConverterLoader { |
267 | |
public <S, T> Converter<S, T> createConverter(Class<S> sourceClass, Class<T> targetClass) { |
268 | 14 | if (String.class == sourceClass && Enum.class.isAssignableFrom(targetClass)) { |
269 | 0 | return Util.cast(new StringToEnum()); |
270 | |
} else { |
271 | 14 | return null; |
272 | |
} |
273 | |
} |
274 | |
|
275 | |
public void loadConverters() { |
276 | 1 | Converters.registerCreator(this); |
277 | 1 | } |
278 | |
} |
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | 0 | public static class StringToLocale extends AbstractConverter<String, Locale> { |
285 | |
public StringToLocale() { |
286 | 1 | super(String.class, Locale.class); |
287 | 1 | } |
288 | |
|
289 | |
public Locale convert(String obj) throws ConversionException { |
290 | 0 | return new Locale(obj); |
291 | |
} |
292 | |
} |
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | 0 | public static class StringToRegexPattern extends AbstractConverter<String, Pattern> { |
299 | |
public StringToRegexPattern() { |
300 | 1 | super(String.class, Pattern.class); |
301 | 1 | } |
302 | |
|
303 | |
public Pattern convert(String obj) throws ConversionException { |
304 | 0 | return Pattern.compile(obj); |
305 | |
} |
306 | |
} |
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | 0 | public static class StringToSimpleDateFormat extends AbstractConverter<String, SimpleDateFormat> { |
313 | |
public StringToSimpleDateFormat() { |
314 | 1 | super(String.class, SimpleDateFormat.class); |
315 | 1 | } |
316 | |
|
317 | |
public SimpleDateFormat convert(String obj) throws ConversionException { |
318 | 0 | return new SimpleDateFormat(obj); |
319 | |
} |
320 | |
} |
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | 0 | public static class StringToUUID extends AbstractConverter<String, UUID> { |
327 | |
public StringToUUID() { |
328 | 1 | super(String.class, UUID.class); |
329 | 1 | } |
330 | |
|
331 | |
public UUID convert(String obj) throws ConversionException { |
332 | 0 | return UUID.fromString(obj); |
333 | |
} |
334 | |
} |
335 | |
} |