KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > typeconverter > StringConverter


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.typeconverter;
4
5 import java.sql.Clob JavaDoc;
6 import java.sql.SQLException JavaDoc;
7
8 /**
9  * Converts given object to String.
10  */

11 public class StringConverter implements TypeConverter {
12
13     public static String JavaDoc valueOf(Object JavaDoc value) {
14         if (value == null) {
15             return null;
16         }
17         Class JavaDoc type = value.getClass();
18         if (type == byte[].class) {
19             byte[] valueArray = (byte[]) value;
20             return new String JavaDoc(valueArray, 0, valueArray.length);
21         }
22         if (value instanceof Clob JavaDoc) {
23             Clob JavaDoc clob = (Clob JavaDoc) value;
24             try {
25                 long lenght = clob.length();
26                 if (lenght > Integer.MAX_VALUE) {
27                     throw new TypeConversionException("Clob is too big.");
28                 }
29                 return clob.getSubString(1, (int) lenght);
30             } catch (SQLException JavaDoc sex) {
31                 throw new TypeConversionException(sex);
32             }
33         }
34         return value.toString();
35     }
36
37     public Object JavaDoc convert(Object JavaDoc value) {
38         return valueOf(value);
39     }
40     
41 }
42
Popular Tags