1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package net.sourceforge.addam.impexp; |
20 | |
|
21 | |
import org.apache.commons.codec.binary.Base64; |
22 | |
import org.apache.tools.ant.BuildException; |
23 | |
|
24 | |
import java.lang.reflect.Constructor; |
25 | |
import java.lang.reflect.Method; |
26 | |
import java.util.HashMap; |
27 | |
import java.util.Map; |
28 | |
import java.sql.Blob; |
29 | |
import java.sql.SQLException; |
30 | |
import java.io.InputStream; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 14 | public abstract class DataCodec { |
40 | |
|
41 | |
abstract public String encode(Object o); |
42 | |
|
43 | |
abstract public Object decode(String s); |
44 | |
|
45 | 4 | static final Map decoders = new HashMap(); |
46 | |
|
47 | |
static DataCodec getCodec(String className) throws NoSuchCodec { |
48 | 12 | DataCodec decoder = (DataCodec) decoders.get(className); |
49 | 12 | final Class STRINGPARAM[] = {java.lang.String.class}; |
50 | 12 | if (decoder == null) { |
51 | |
|
52 | 12 | if (className.equals("oracle.sql.TIMESTAMP")) { |
53 | 0 | className = "java.sql.Timestamp"; |
54 | |
} |
55 | 12 | if (className.equals("byte[]")) { |
56 | 0 | return new ByteArrayCodec(); |
57 | 12 | } else if (className.equals("oracle.sql.BLOB")) { |
58 | 0 | return new BlobCodec(); |
59 | 12 | } else if (className.equals("java.sql.Blob")) { |
60 | 0 | return new BlobCodec(); |
61 | |
} else { |
62 | 12 | Class theClass = null; |
63 | 12 | Constructor constructor = null; |
64 | 12 | Method valueOfMethod = null; |
65 | |
|
66 | |
try { |
67 | 12 | theClass = Class.forName(className); |
68 | 0 | } catch (ClassNotFoundException e) { |
69 | 0 | throw new NoSuchCodec(className); |
70 | 6 | } |
71 | |
|
72 | |
try { |
73 | 12 | constructor = theClass.getConstructor(STRINGPARAM); |
74 | 12 | } catch (NoSuchMethodException e) { |
75 | |
|
76 | 0 | } |
77 | |
|
78 | 12 | if (constructor == null) { |
79 | |
try { |
80 | 12 | valueOfMethod = theClass.getMethod("valueOf", STRINGPARAM); |
81 | 0 | } catch (NoSuchMethodException e) { |
82 | 0 | throw new NoSuchCodec(className); |
83 | 6 | } |
84 | 12 | decoder = new StandardCodec(valueOfMethod); |
85 | 6 | } else { |
86 | 0 | decoder = new StandardCodec(constructor); |
87 | |
} |
88 | |
} |
89 | |
} |
90 | 12 | return decoder; |
91 | |
} |
92 | |
|
93 | |
|
94 | 0 | static class BlobCodec extends DataCodec { |
95 | |
|
96 | |
public String encode(Object o) { |
97 | 0 | Blob b = (Blob)o; |
98 | |
String result; |
99 | |
try { |
100 | 0 | if (b.length() > (1024*1024)) { |
101 | 0 | throw new BuildException("Blob is larger than 1 meg"); |
102 | |
} |
103 | 0 | byte byteBuf[] = new byte[(int)b.length()]; |
104 | 0 | InputStream s = b.getBinaryStream(); |
105 | 0 | s.read(byteBuf); |
106 | 0 | byte[] encodedBytes = Base64.encodeBase64(byteBuf); |
107 | 0 | result = new String(encodedBytes); |
108 | 0 | } catch (Exception e) { |
109 | 0 | throw new BuildException(e); |
110 | 0 | } |
111 | 0 | return result; |
112 | |
} |
113 | |
|
114 | |
public Object decode(String s) { |
115 | 0 | byte[] decodedBytes = Base64.decodeBase64(s.getBytes()); |
116 | 0 | return decodedBytes; |
117 | |
} |
118 | |
} |
119 | |
|
120 | |
|
121 | 0 | static class ByteArrayCodec extends DataCodec { |
122 | |
|
123 | |
public String encode(Object o) { |
124 | 0 | byte[] encodedBytes = Base64.encodeBase64((byte[]) o); |
125 | 0 | return new String(encodedBytes); |
126 | |
} |
127 | |
|
128 | |
public Object decode(String s) { |
129 | 0 | byte[] decodedBytes = Base64.decodeBase64(s.getBytes()); |
130 | 0 | return decodedBytes; |
131 | |
} |
132 | |
} |
133 | |
|
134 | |
|
135 | |
static class StandardCodec extends DataCodec { |
136 | |
|
137 | |
final Constructor constructor; |
138 | |
final Method valueOfMethod; |
139 | |
|
140 | 0 | StandardCodec(Constructor constructor) { |
141 | 0 | this.constructor = constructor; |
142 | 0 | this.valueOfMethod = null; |
143 | 0 | } |
144 | |
|
145 | 12 | StandardCodec(Method valueOfMethod) { |
146 | 12 | this.valueOfMethod = valueOfMethod; |
147 | 12 | this.constructor = null; |
148 | 12 | } |
149 | |
|
150 | |
public String encode(Object o) { |
151 | 8 | return o == null ? "" : o.toString(); |
152 | |
} |
153 | |
|
154 | |
public Object decode(String s) { |
155 | 12 | Object result = null; |
156 | |
|
157 | |
|
158 | 12 | if (s != null && s.length() > 0) { |
159 | 8 | Object args[] = {s}; |
160 | |
try { |
161 | 8 | if (constructor != null) { |
162 | |
|
163 | 0 | result = constructor.newInstance(args); |
164 | 4 | } else if (valueOfMethod != null) { |
165 | |
|
166 | 8 | result = valueOfMethod.invoke(null, args); |
167 | 2 | } else { |
168 | |
|
169 | 0 | result = s; |
170 | |
} |
171 | 4 | } catch (Exception e) { |
172 | 4 | e.printStackTrace(); |
173 | 4 | throw new BuildException(e); |
174 | 2 | } |
175 | |
} |
176 | |
|
177 | 8 | return result; |
178 | |
} |
179 | |
|
180 | |
|
181 | |
} |
182 | |
|
183 | 6 | static class NoSuchCodec extends Exception { |
184 | |
NoSuchCodec(String className) { |
185 | 0 | super("no decoder for class " + className + " could be found"); |
186 | 0 | } |
187 | |
} |
188 | |
} |