KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > util > type > TypeUtils


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.util.type;
19
20 import org.apache.beehive.netui.util.internal.InternalStringBuilder;
21
22 import java.io.InputStream JavaDoc;
23 import java.math.BigDecimal JavaDoc;
24 import java.math.BigInteger JavaDoc;
25 import java.text.DateFormat JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.LinkedHashMap JavaDoc;
31 import java.util.Locale JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 import org.apache.beehive.netui.util.Bundle;
36 import org.apache.beehive.netui.util.config.ConfigUtil;
37 import org.apache.beehive.netui.util.config.bean.NetuiConfigDocument.NetuiConfig;
38 import org.apache.beehive.netui.util.config.bean.TypeConverters;
39 import org.apache.beehive.netui.util.logging.Logger;
40
41 /**
42  *
43  *
44  */

45 public final class TypeUtils {
46
47     private static final Logger LOGGER = Logger.getInstance(TypeUtils.class);
48     private static final String JavaDoc TYPE_CONVERTER_PROPERTIES = "/properties/netui-typeconverter.properties";
49     private static final String JavaDoc EMPTY_STRING = "";
50     private static final HashMap JavaDoc/*<Class, BaseTypeConverter>*/ TYPE_CONVERTERS = new HashMap JavaDoc/*<Class, BaseTypeConverter>*/();
51     private static String JavaDoc TO_STRING = null;
52
53     static {
54         // initialize the default type converters
55
initialize();
56
57         Map JavaDoc/*<String, String>*/ map = readFromConfig();
58         if(map != null) {
59             load(map);
60             map.clear();
61         }
62
63         map = readFromProperties();
64         if(map != null)
65             load(map);
66
67         LOGGER.info(registeredConvertersToString());
68     }
69
70     /* do not construct */
71     private TypeUtils() {}
72
73     /**
74      * Convert an object from a String to the given type.
75      *
76      * @deprecated
77      * @param value the String to convert
78      * @param type the type to which to convert the String
79      * @return the Object result of converting the String to the type.
80      * @throws TypeConverterNotFoundException if a TypeConverter for the target type can not be found.
81      */

82     public static final Object JavaDoc convertToObject(String JavaDoc value, Class JavaDoc type) {
83         return convertToObject(value, type, null);
84     }
85
86     /**
87      * Convert an object from a String to the given type using the specified {@link java.util.Locale}.
88      * <p/>
89      * The locale is optionally used depending on the implementation of the {@link TypeConverter} that is used.
90      *
91      * @param value the String to convert
92      * @param type the type to which to convert the String
93      * @param locale the locale to use during conversion
94      * @return the Object result of converting the String to the type.
95      * @throws TypeConverterNotFoundException if a TypeConverter for the target type can not be found.
96      */

97     public static final Object JavaDoc convertToObject(String JavaDoc value, Class JavaDoc type, Locale JavaDoc locale) {
98         BaseTypeConverter converter = lookupTypeConverter(type);
99         assert converter != null;
100         return converter.convertToObject(type, value, locale);
101     }
102
103     public static final byte convertToByte(String JavaDoc value) {
104         return ((Byte JavaDoc)convertToObject(value, byte.class, null)).byteValue();
105     }
106
107     public static final boolean convertToBoolean(String JavaDoc value) {
108         return ((Boolean JavaDoc)convertToObject(value, boolean.class, null)).booleanValue();
109     }
110
111     public static final char convertToChar(String JavaDoc value) {
112         return ((Character JavaDoc)convertToObject(value, char.class, null)).charValue();
113     }
114
115     public static final double convertToDouble(String JavaDoc value) {
116         return ((Double JavaDoc)convertToObject(value, double.class, null)).doubleValue();
117     }
118
119     public static final float convertToFloat(String JavaDoc value) {
120         return ((Float JavaDoc)convertToObject(value, float.class, null)).floatValue();
121     }
122
123     public static final int convertToInt(String JavaDoc value) {
124         return ((Integer JavaDoc)convertToObject(value, int.class, null)).intValue();
125     }
126
127     public static final long convertToLong(String JavaDoc value) {
128         return ((Long JavaDoc)convertToObject(value, long.class, null)).longValue();
129     }
130
131     public static final short convertToShort(String JavaDoc value) {
132         return ((Short JavaDoc)convertToObject(value, short.class, null)).shortValue();
133     }
134
135     public static final Byte JavaDoc convertToByteObject(String JavaDoc value) {
136         return (Byte JavaDoc)convertToObject(value, Byte JavaDoc.class, null);
137     }
138
139     public static final Boolean JavaDoc convertToBooleanObject(String JavaDoc value) {
140         return (Boolean JavaDoc)convertToObject(value, Boolean JavaDoc.class, null);
141     }
142
143     public static final Character JavaDoc convertToCharacterObject(String JavaDoc value) {
144         return (Character JavaDoc)convertToObject(value, Character JavaDoc.class, null);
145     }
146
147     public static final Double JavaDoc convertToDoubleObject(String JavaDoc value) {
148         return (Double JavaDoc)convertToObject(value, Double JavaDoc.class, null);
149     }
150
151     public static final Float JavaDoc convertToFloatObject(String JavaDoc value) {
152         return (Float JavaDoc)convertToObject(value, Float JavaDoc.class, null);
153     }
154
155     public static final Integer JavaDoc convertToIntegerObject(String JavaDoc value) {
156         return (Integer JavaDoc)convertToObject(value, Integer JavaDoc.class, null);
157     }
158
159     public static final Long JavaDoc convertToLongObject(String JavaDoc value) {
160         return (Long JavaDoc)convertToObject(value, Long JavaDoc.class, null);
161     }
162
163     public static final Short JavaDoc convertToShortObject(String JavaDoc value) {
164         return (Short JavaDoc)convertToObject(value, Short JavaDoc.class, null);
165     }
166
167     /**
168      * Internal method used to lookup a {@link BaseTypeConverter}.
169      *
170      * @param type the target conversion type
171      * @return a {@link BaseTypeConverter} to use for conversion
172      * @throws TypeConverterNotFoundException if a TypeConverter for the target type can not be found.
173      */

174     private static final BaseTypeConverter lookupTypeConverter(Class JavaDoc type) {
175         BaseTypeConverter converter = (BaseTypeConverter)TYPE_CONVERTERS.get(type);
176         if(converter == null) {
177             String JavaDoc msg = "Could not find a TypeConverter for converting a String to an object of type \"" +
178                 (type != null ? type.getName() : null) + "\"";
179             TypeConverterNotFoundException tcn = new TypeConverterNotFoundException(msg);
180
181             if(type == null)
182                 msg = Bundle.getErrorString("TypeUtils_nullType");
183             else
184                 msg = Bundle.getErrorString("TypeUtils_noConverterForType", new Object JavaDoc[]{type.getName()});
185             tcn.setLocalizedMessage(msg);
186             LOGGER.error(msg);
187
188             throw tcn;
189         }
190
191         return converter;
192     }
193
194     private static String JavaDoc registeredConvertersToString() {
195         if(TO_STRING != null)
196             return TO_STRING;
197
198         InternalStringBuilder sb = new InternalStringBuilder(256);
199         sb.append(TypeUtils.class.getName() + " regestered converters (class name, TypeConverter implementation):\n");
200         sb.append(":::::\n");
201         Iterator JavaDoc iterator = TYPE_CONVERTERS.keySet().iterator();
202         while(iterator.hasNext()) {
203             Class JavaDoc key = (Class JavaDoc)iterator.next();
204             String JavaDoc keyName = key.getName();
205             String JavaDoc value = (TYPE_CONVERTERS.get(key) != null ? TYPE_CONVERTERS.get(key).getClass().getName() : "null");
206             sb.append(keyName);
207             sb.append(", ");
208             sb.append(value);
209             sb.append("\n");
210         }
211         sb.append(":::::\n");
212         TO_STRING = sb.toString();
213
214         return TO_STRING;
215     }
216
217     private static Map JavaDoc/*<String, String>*/ readFromProperties() {
218         Properties JavaDoc props = null;
219         InputStream JavaDoc is = null;
220         try {
221             is = (TypeUtils.class).getClassLoader().getResourceAsStream(TYPE_CONVERTER_PROPERTIES);
222
223             LOGGER.debug("found type converter InputStream at " + TYPE_CONVERTER_PROPERTIES + " " + (is != null ? "true" : "false"));
224
225             if(is == null)
226                 return null;
227
228             props = new Properties JavaDoc();
229             props.load(is);
230         } catch(Exception JavaDoc e) {
231             LOGGER.warn("Error occurred reading type converter properties file", e);
232         } finally {
233             try {
234                 if(is != null) is.close();
235             } catch(Exception JavaDoc ignore) {
236             }
237         }
238
239         LinkedHashMap JavaDoc/*<String, String>*/ map = new LinkedHashMap JavaDoc/*<String, String>*/();
240         Enumeration JavaDoc e = props.propertyNames();
241         while(e.hasMoreElements()) {
242             String JavaDoc key = (String JavaDoc)e.nextElement();
243             map.put(key, props.getProperty(key));
244         }
245
246         return map;
247     }
248
249     private static final Map JavaDoc/*<String, String>*/ readFromConfig() {
250         NetuiConfig config = ConfigUtil.getConfig();
251         if(config == null)
252             return null;
253
254         TypeConverters converters = config.getTypeConverters();
255         if(converters == null)
256             return null;
257
258         org.apache.beehive.netui.util.config.bean.TypeConverters.TypeConverter[] converterArray =
259             converters.getTypeConverterArray();
260         if(converterArray == null)
261             return null;
262
263         LinkedHashMap JavaDoc/*<String, String>*/ map = new LinkedHashMap JavaDoc/*<String, String>*/();
264         for(int i = 0; i < converterArray.length; i++) {
265             map.put(converterArray[i].getType(), converterArray[i].getConverterClass());
266         }
267
268         return map;
269     }
270
271     private static void load(Map JavaDoc/*<String, String>*/ map) {
272         // load the properties and continue to populate the map
273
for ( Iterator JavaDoc i = map.keySet().iterator(); i.hasNext(); )
274         {
275             String JavaDoc key = (String JavaDoc) i.next();
276             String JavaDoc className = (String JavaDoc) map.get(key);
277
278             if((key == null || key.equals(EMPTY_STRING)) || (className == null || className.equals(EMPTY_STRING))) {
279                 LOGGER.warn("Could not create a TypeConverter for type \"" + key + "\" and TypeConverter \"" + className + "\"");
280                 continue;
281             }
282
283             Class JavaDoc targetClazz = null;
284
285             /* attempt to load the "convert-to" class */
286             try {
287                 targetClazz = Class.forName(key);
288             } catch(ClassNotFoundException JavaDoc cnf) {
289                 LOGGER.warn("Could not create a TypeConverter for type \"" + key + "\" because the \"convert-to\" type could not be found.");
290                 continue;
291             }
292
293             Class JavaDoc tcClazz = null;
294             BaseTypeConverter tc = null;
295             // try to find the TypeConverter implementation
296
try {
297                 tcClazz = Class.forName(className);
298                 Object JavaDoc obj = tcClazz.newInstance();
299
300                 // this supports existing TypeConverter implementations
301
// but allows TypeUtils make calls against the BaseTypeConverter
302
// API, which supports Locale-based conversion
303
if(obj instanceof TypeConverter)
304                     tc = new DelegatingTypeConverter((TypeConverter)obj);
305                 else if(obj instanceof BaseTypeConverter)
306                     tc = (BaseTypeConverter)obj;
307                 else throw new IllegalStateException JavaDoc("Attempt to load illegal type converter type: " + tcClazz);
308             } catch(ClassNotFoundException JavaDoc cnf) {
309                 LOGGER.warn("Could not create a TypeConverter for type \"" + key + "\" because the TypeConverter implementation class \"" +
310                             (tcClazz != null ? tcClazz.getName() : null) + "\" could not be found.");
311
312                 continue;
313             } catch(Exception JavaDoc e) {
314                 if(LOGGER.isWarnEnabled())
315                     LOGGER.warn("Could not create a TypeConverter for type \"" + key + "\" because the implementation class \"" +
316                         (tcClazz != null ? tcClazz.getName() : null) + "\" could not be instantiated.");
317                 continue;
318             }
319             
320             /* found two type converters for the same class -- warn */
321             if(TYPE_CONVERTERS.containsKey(targetClazz))
322                 if(LOGGER.isWarnEnabled())
323                     LOGGER.warn("Overwriting a previously defined TypeConverter named \"" + targetClazz +
324                         "\" with a new TypeConverter implementation of type \"" + className + "\"");
325
326             if(LOGGER.isInfoEnabled())
327                 LOGGER.info("Adding a type converter; target type=\"" + targetClazz.getName() +
328                     "\" TypeConverter implementation=\"" + tc.getClass().getName() + "\"");
329
330             TYPE_CONVERTERS.put(targetClazz, tc);
331         }
332     }
333
334     /**
335      * Create converters that take an Object representing a value and convert
336      * that value, based on its type, to a String. Includes all primitive types,
337      * primitive wrappers, String, and BigDecimal. Types like BigDecimal are included
338      * because JDBC uses these complex types to map SQL types to Java objects.
339      */

340     private static void initialize() {
341         TYPE_CONVERTERS.put(byte.class, new BaseTypeConverter() {
342             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
343                 return (value == null || value.equals(EMPTY_STRING) ? new Byte JavaDoc((byte)0) : new Byte JavaDoc(value.trim()));
344             }
345
346         });
347         TYPE_CONVERTERS.put(Byte JavaDoc.class, new BaseTypeConverter() {
348             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
349                 if(value == null || value.equals(EMPTY_STRING))
350                     return null;
351                 else
352                     return TypeUtils.convertToObject(value, byte.class, null);
353             }
354         });
355
356         TYPE_CONVERTERS.put(boolean.class, new BaseTypeConverter() {
357             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
358                 if(value == null || value.equals(EMPTY_STRING))
359                     return Boolean.FALSE;
360
361                 value = value.toLowerCase().trim();
362                 if(value.equals("on") || value.equals("true"))
363                     return Boolean.TRUE;
364                 else
365                     return Boolean.FALSE;
366             }
367         });
368         TYPE_CONVERTERS.put(Boolean JavaDoc.class, new BaseTypeConverter() {
369             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
370                 if(value == null || value.equals(EMPTY_STRING))
371                     return null;
372                 else
373                     return TypeUtils.convertToObject(value, boolean.class, null);
374             }
375         });
376
377         TYPE_CONVERTERS.put(char.class, new BaseTypeConverter() {
378             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
379                 if(value == null || value.equals(EMPTY_STRING))
380                     return new Character JavaDoc('\u0000');
381                 else
382                     return new Character JavaDoc(value.charAt(0));
383             }
384         });
385         TYPE_CONVERTERS.put(Character JavaDoc.class, new BaseTypeConverter() {
386             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
387                 if(value == null || value.equals(EMPTY_STRING))
388                     return null;
389                 else
390                     return TypeUtils.convertToObject(value, char.class, null);
391             }
392         });
393
394         TYPE_CONVERTERS.put(double.class, new BaseTypeConverter() {
395             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
396                 if(value == null || value.equals(EMPTY_STRING))
397                     return new Double JavaDoc(0.0);
398                 else
399                     return new Double JavaDoc(value.trim());
400             }
401         });
402         TYPE_CONVERTERS.put(Double JavaDoc.class, new BaseTypeConverter() {
403             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
404                 if(value == null || value.equals(EMPTY_STRING))
405                     return null;
406                 else
407                     return TypeUtils.convertToObject(value, double.class, null);
408             }
409         });
410
411         TYPE_CONVERTERS.put(float.class, new BaseTypeConverter() {
412             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
413                 if(value == null || value.equals(EMPTY_STRING))
414                     return new Float JavaDoc(0.0);
415                 else
416                     return new Float JavaDoc(value.trim());
417             }
418         });
419         TYPE_CONVERTERS.put(Float JavaDoc.class, new BaseTypeConverter() {
420             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
421                 if(value == null || value.equals(EMPTY_STRING))
422                     return null;
423                 else
424                     return TypeUtils.convertToObject(value, float.class, null);
425             }
426         });
427
428         TYPE_CONVERTERS.put(int.class, new BaseTypeConverter() {
429             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
430                 if(value == null || value.equals(EMPTY_STRING))
431                     return new Integer JavaDoc(0);
432                 else
433                     return new Integer JavaDoc(value.trim());
434             }
435         });
436         TYPE_CONVERTERS.put(Integer JavaDoc.class, new BaseTypeConverter() {
437             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
438                 if(value == null || value.equals(EMPTY_STRING))
439                     return null;
440                 else
441                     return TypeUtils.convertToObject(value, int.class, null);
442             }
443         });
444
445         TYPE_CONVERTERS.put(long.class, new BaseTypeConverter() {
446             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
447                 if(value == null || value.equals(EMPTY_STRING))
448                     return new Long JavaDoc(0);
449                 else
450                     return new Long JavaDoc(value.trim());
451             }
452         });
453         TYPE_CONVERTERS.put(Long JavaDoc.class, new BaseTypeConverter() {
454             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
455                 if(value == null || value.equals(EMPTY_STRING))
456                     return null;
457                 else
458                     return TypeUtils.convertToObject(value, long.class, null);
459             }
460         });
461
462         TYPE_CONVERTERS.put(short.class, new BaseTypeConverter() {
463             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
464                 if(value == null || value.equals(EMPTY_STRING))
465                     return new Short JavaDoc((short)0);
466                 else
467                     return new Short JavaDoc(value.trim());
468             }
469         });
470         TYPE_CONVERTERS.put(Short JavaDoc.class, new BaseTypeConverter() {
471             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
472                 if(value == null || value.equals(EMPTY_STRING))
473                     return null;
474                 else
475                     return TypeUtils.convertToObject(value, short.class, null);
476             }
477         });
478
479         TYPE_CONVERTERS.put(String JavaDoc.class, new BaseTypeConverter() {
480             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
481                 if(value == null)
482                     return null;
483                 else
484                     return value;
485             }
486         });
487
488         TYPE_CONVERTERS.put(java.math.BigDecimal JavaDoc.class, new BaseTypeConverter() {
489             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
490                 if(value == null || value.equals(EMPTY_STRING))
491                     return null;
492                 else
493                     return new BigDecimal JavaDoc(value.trim());
494             }
495         });
496
497         TYPE_CONVERTERS.put(java.math.BigInteger JavaDoc.class, new BaseTypeConverter() {
498             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
499                 if(value == null || value.equals(EMPTY_STRING))
500                     return null;
501                 else
502                     return new BigInteger JavaDoc(value.trim());
503             }
504         });
505
506         TYPE_CONVERTERS.put(byte[].class, new BaseTypeConverter() {
507             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
508                 if(value == null || value.equals(EMPTY_STRING))
509                     return null;
510                 else
511                     return value.getBytes();
512             }
513         });
514
515         TYPE_CONVERTERS.put(Byte JavaDoc[].class, new BaseTypeConverter() {
516             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
517                 if(value == null || value.equals(EMPTY_STRING))
518                     return null;
519                 else {
520                     byte[] bytes = value.getBytes();
521                     Byte JavaDoc[] wBytes = new Byte JavaDoc[bytes.length];
522
523                     for(int i = 0; i < bytes.length; i++)
524                         wBytes[i] = new Byte JavaDoc(bytes[i]);
525
526                     return wBytes;
527                 }
528             }
529         });
530
531         TYPE_CONVERTERS.put(Date JavaDoc.class, new BaseTypeConverter() {
532             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
533                 if(value == null || value.equals(EMPTY_STRING))
534                     return null;
535
536                 try {
537                     if(locale == null)
538                         locale = Locale.getDefault();
539
540                     DateFormat JavaDoc df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
541                     return df.parse(value);
542                 } catch(java.text.ParseException JavaDoc pe) {
543                     String JavaDoc msg = "Caugnt an error converting a String to a DateFormat.SHORT formatted Date";
544                     LOGGER.warn(msg, pe);
545
546                     TypeConversionException tce = new TypeConversionException(msg, pe);
547                     tce.setLocalizedMessage(Bundle.getString("TypeUtils_javaUtilDateConvertError", new Object JavaDoc[]{pe.getMessage()}));
548                     throw tce;
549                 }
550             }
551         });
552
553         /* http://java.sun.com/j2se/1.4.1/docs/api/java/sql/Date.html */
554         TYPE_CONVERTERS.put(java.sql.Date JavaDoc.class, new BaseTypeConverter() {
555             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
556                 if(value == null || value.equals(EMPTY_STRING)) return null;
557
558                 try {
559                     return java.sql.Date.valueOf(value);
560                 } catch(Exception JavaDoc e) {
561                     String JavaDoc msg = "Caught an error converting a String to a java.sql.Date";
562                     LOGGER.error(msg, e);
563
564                     TypeConversionException tce = new TypeConversionException(msg, e);
565                     tce.setLocalizedMessage(Bundle.getString("TypeUtils_javaSqlDateConvertError", new Object JavaDoc[]{e.getMessage()}));
566                     throw tce;
567                 }
568             }
569         });
570
571         /* http://java.sun.com/j2se/1.4.1/docs/api/java/sql/Timestamp.html */
572         TYPE_CONVERTERS.put(java.sql.Timestamp JavaDoc.class, new BaseTypeConverter() {
573             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
574                 if(value == null || value.equals(EMPTY_STRING))
575                     return null;
576
577                 try {
578                     return java.sql.Timestamp.valueOf(value);
579                 } catch(Exception JavaDoc e) {
580                     String JavaDoc msg = "Caught an error converting a String to a java.sql.Timestamp";
581                     LOGGER.error(msg, e);
582
583                     TypeConversionException tce = new TypeConversionException(msg, e);
584                     tce.setLocalizedMessage(Bundle.getString("TypeUtils_javaSqlTimestampConvertError", new Object JavaDoc[]{e.getMessage()}));
585                     throw tce;
586                 }
587             }
588         });
589
590         /* http://java.sun.com/j2se/1.4.1/docs/api/java/sql/Time.html */
591         TYPE_CONVERTERS.put(java.sql.Time JavaDoc.class, new BaseTypeConverter() {
592             public Object JavaDoc convertToObject(Class JavaDoc type, String JavaDoc value, Locale JavaDoc locale) {
593                 if(value == null || value.equals(EMPTY_STRING))
594                     return null;
595
596                 try {
597                     return java.sql.Time.valueOf(value);
598                 } catch(Exception JavaDoc e) {
599                     String JavaDoc msg = "Caught an error converting a String to a java.sql.Time";
600                     LOGGER.error(msg, e);
601
602                     TypeConversionException tce = new TypeConversionException(msg, e);
603                     tce.setLocalizedMessage(Bundle.getString("TypeUtils_javaSqlTimeConvertError", new Object JavaDoc[]{e.getMessage()}));
604                     throw tce;
605                 }
606             }
607         });
608     }
609 }
610
Popular Tags