Commit | Line | Data |
---|---|---|
db31c358 NR |
1 | package be.nikiroo.utils.serial; |
2 | ||
564bbbdb | 3 | import java.io.ByteArrayInputStream; |
e570f7eb | 4 | import java.io.IOException; |
68532958 | 5 | import java.io.InputStream; |
db31c358 | 6 | import java.io.NotSerializableException; |
68532958 NR |
7 | import java.io.OutputStream; |
8 | import java.io.UnsupportedEncodingException; | |
ce0974c4 | 9 | import java.lang.reflect.Array; |
8c8da42a | 10 | import java.lang.reflect.Constructor; |
db31c358 | 11 | import java.lang.reflect.Field; |
e570f7eb | 12 | import java.lang.reflect.Modifier; |
f4053377 | 13 | import java.net.URL; |
72648e75 | 14 | import java.util.ArrayList; |
db31c358 | 15 | import java.util.HashMap; |
72648e75 | 16 | import java.util.List; |
db31c358 | 17 | import java.util.Map; |
ce0974c4 | 18 | import java.util.UnknownFormatConversionException; |
db31c358 | 19 | |
dc41a952 | 20 | import be.nikiroo.utils.IOUtils; |
80500544 | 21 | import be.nikiroo.utils.Image; |
3b4319db | 22 | import be.nikiroo.utils.StringUtils; |
52fb9a56 NR |
23 | import be.nikiroo.utils.streams.NextableInputStream; |
24 | import be.nikiroo.utils.streams.NextableInputStreamStep; | |
e570f7eb | 25 | |
db31c358 | 26 | /** |
8c8da42a | 27 | * Small class to help with serialisation. |
db31c358 NR |
28 | * <p> |
29 | * Note that we do not support inner classes (but we do support nested classes) | |
30 | * and all objects require an empty constructor to be deserialised. | |
5bc55b51 NR |
31 | * <p> |
32 | * It is possible to add support to custom types (both the encoder and the | |
33 | * decoder will require the custom classes) -- see {@link CustomSerializer}. | |
34 | * <p> | |
35 | * Default supported types are: | |
36 | * <ul> | |
37 | * <li>NULL (as a null value)</li> | |
38 | * <li>String</li> | |
39 | * <li>Boolean</li> | |
40 | * <li>Byte</li> | |
41 | * <li>Character</li> | |
42 | * <li>Short</li> | |
43 | * <li>Long</li> | |
44 | * <li>Float</li> | |
45 | * <li>Double</li> | |
46 | * <li>Integer</li> | |
47 | * <li>Enum (any enum whose name and value is known by the caller)</li> | |
48 | * <li>java.awt.image.BufferedImage (as a {@link CustomSerializer})</li> | |
49 | * <li>An array of the above (as a {@link CustomSerializer})</li> | |
f4053377 | 50 | * <li>URL</li> |
5bc55b51 | 51 | * </ul> |
db31c358 NR |
52 | * |
53 | * @author niki | |
54 | */ | |
aad14586 | 55 | public class SerialUtils { |
db31c358 NR |
56 | private static Map<String, CustomSerializer> customTypes; |
57 | ||
58 | static { | |
59 | customTypes = new HashMap<String, CustomSerializer>(); | |
ce0974c4 NR |
60 | |
61 | // Array types: | |
62 | customTypes.put("[]", new CustomSerializer() { | |
63 | @Override | |
143a64f4 NR |
64 | protected void toStream(OutputStream out, Object value) |
65 | throws IOException { | |
3b4319db NR |
66 | |
67 | // TODO: we use \n to separate, and b64 to un-\n | |
68 | // -- but we could use \\n ? | |
ce0974c4 NR |
69 | String type = value.getClass().getCanonicalName(); |
70 | type = type.substring(0, type.length() - 2); // remove the [] | |
71 | ||
dc41a952 | 72 | write(out, type); |
ce0974c4 NR |
73 | try { |
74 | for (int i = 0; true; i++) { | |
75 | Object item = Array.get(value, i); | |
23cf894d | 76 | |
ce0974c4 | 77 | // encode it normally if direct value |
23cf894d | 78 | write(out, "\r"); |
143a64f4 | 79 | if (!SerialUtils.encode(out, item)) { |
ce0974c4 | 80 | try { |
4d319565 NR |
81 | // TODO: bad escaping? |
82 | write(out, "B64:"); | |
83 | OutputStream bout = StringUtils.base64(out, | |
84 | false, false); | |
85 | new Exporter(bout).append(item); | |
ce0974c4 NR |
86 | } catch (NotSerializableException e) { |
87 | throw new UnknownFormatConversionException(e | |
88 | .getMessage()); | |
89 | } | |
90 | } | |
ce0974c4 NR |
91 | } |
92 | } catch (ArrayIndexOutOfBoundsException e) { | |
93 | // Done. | |
94 | } | |
ce0974c4 NR |
95 | } |
96 | ||
143a64f4 NR |
97 | @Override |
98 | protected Object fromStream(InputStream in) throws IOException { | |
dc41a952 | 99 | NextableInputStream stream = new NextableInputStream(in, |
4d319565 | 100 | new NextableInputStreamStep('\r')); |
ce0974c4 NR |
101 | |
102 | try { | |
dc41a952 NR |
103 | List<Object> list = new ArrayList<Object>(); |
104 | stream.next(); | |
105 | String type = IOUtils.readSmallStream(stream); | |
106 | ||
107 | while (stream.next()) { | |
108 | Object value = new Importer().read(stream).getValue(); | |
109 | list.add(value); | |
110 | } | |
111 | ||
ce0974c4 | 112 | Object array = Array.newInstance( |
dc41a952 NR |
113 | SerialUtils.getClass(type), list.size()); |
114 | for (int i = 0; i < list.size(); i++) { | |
115 | Array.set(array, i, list.get(i)); | |
ce0974c4 NR |
116 | } |
117 | ||
118 | return array; | |
119 | } catch (Exception e) { | |
452f38c8 NR |
120 | if (e instanceof IOException) { |
121 | throw (IOException) e; | |
122 | } | |
123 | throw new IOException(e.getMessage()); | |
ce0974c4 NR |
124 | } |
125 | } | |
dc41a952 NR |
126 | |
127 | @Override | |
128 | protected String getType() { | |
129 | return "[]"; | |
130 | } | |
ce0974c4 | 131 | }); |
e570f7eb | 132 | |
f4053377 NR |
133 | // URL: |
134 | customTypes.put("java.net.URL", new CustomSerializer() { | |
135 | @Override | |
dc41a952 NR |
136 | protected void toStream(OutputStream out, Object value) |
137 | throws IOException { | |
138 | String val = ""; | |
f4053377 | 139 | if (value != null) { |
dc41a952 | 140 | val = ((URL) value).toString(); |
f4053377 | 141 | } |
dc41a952 NR |
142 | |
143 | out.write(val.getBytes("UTF-8")); | |
f4053377 NR |
144 | } |
145 | ||
146 | @Override | |
dc41a952 NR |
147 | protected Object fromStream(InputStream in) throws IOException { |
148 | String val = IOUtils.readSmallStream(in); | |
149 | if (!val.isEmpty()) { | |
150 | return new URL(val); | |
f4053377 | 151 | } |
dc41a952 | 152 | |
f4053377 NR |
153 | return null; |
154 | } | |
155 | ||
156 | @Override | |
157 | protected String getType() { | |
158 | return "java.net.URL"; | |
159 | } | |
160 | }); | |
161 | ||
e570f7eb | 162 | // Images (this is currently the only supported image type by default) |
80500544 | 163 | customTypes.put("be.nikiroo.utils.Image", new CustomSerializer() { |
e570f7eb | 164 | @Override |
dc41a952 NR |
165 | protected void toStream(OutputStream out, Object value) |
166 | throws IOException { | |
167 | Image img = (Image) value; | |
168 | OutputStream encoded = StringUtils.base64(out, false, false); | |
169 | try { | |
170 | InputStream in = img.newInputStream(); | |
171 | try { | |
172 | IOUtils.write(in, encoded); | |
173 | } finally { | |
174 | in.close(); | |
175 | } | |
176 | } finally { | |
177 | encoded.close(); | |
178 | } | |
e570f7eb NR |
179 | } |
180 | ||
181 | @Override | |
182 | protected String getType() { | |
80500544 | 183 | return "be.nikiroo.utils.Image"; |
e570f7eb NR |
184 | } |
185 | ||
186 | @Override | |
dc41a952 | 187 | protected Object fromStream(InputStream in) throws IOException { |
e570f7eb | 188 | try { |
dc41a952 | 189 | return new Image(in); |
e570f7eb NR |
190 | } catch (IOException e) { |
191 | throw new UnknownFormatConversionException(e.getMessage()); | |
192 | } | |
193 | } | |
194 | }); | |
db31c358 | 195 | } |
8c8da42a | 196 | |
aad14586 NR |
197 | /** |
198 | * Create an empty object of the given type. | |
199 | * | |
200 | * @param type | |
201 | * the object type (its class name) | |
202 | * | |
203 | * @return the new object | |
204 | * | |
8c8da42a NR |
205 | * @throws ClassNotFoundException |
206 | * if the class cannot be found | |
207 | * @throws NoSuchMethodException | |
208 | * if the given class is not compatible with this code | |
aad14586 | 209 | */ |
8c8da42a NR |
210 | public static Object createObject(String type) |
211 | throws ClassNotFoundException, NoSuchMethodException { | |
aad14586 | 212 | |
72648e75 | 213 | String desc = null; |
aad14586 NR |
214 | try { |
215 | Class<?> clazz = getClass(type); | |
aad14586 | 216 | String className = clazz.getName(); |
72648e75 NR |
217 | List<Object> args = new ArrayList<Object>(); |
218 | List<Class<?>> classes = new ArrayList<Class<?>>(); | |
aad14586 NR |
219 | Constructor<?> ctor = null; |
220 | if (className.contains("$")) { | |
72648e75 NR |
221 | for (String parentName = className.substring(0, |
222 | className.lastIndexOf('$'));; parentName = parentName | |
949445ee | 223 | .substring(0, parentName.lastIndexOf('$'))) { |
72648e75 NR |
224 | Object parent = createObject(parentName); |
225 | args.add(parent); | |
226 | classes.add(parent.getClass()); | |
949445ee | 227 | |
72648e75 NR |
228 | if (!parentName.contains("$")) { |
229 | break; | |
230 | } | |
231 | } | |
232 | ||
233 | // Better error description in case there is no empty | |
234 | // constructor: | |
235 | desc = ""; | |
236 | String end = ""; | |
237 | for (Class<?> parent = clazz; parent != null | |
238 | && !parent.equals(Object.class); parent = parent | |
949445ee | 239 | .getSuperclass()) { |
72648e75 NR |
240 | if (!desc.isEmpty()) { |
241 | desc += " [:"; | |
242 | end += "]"; | |
243 | } | |
244 | desc += parent; | |
245 | } | |
246 | desc += end; | |
247 | // | |
248 | ||
cd26ee07 NR |
249 | try { |
250 | ctor = clazz.getDeclaredConstructor(classes | |
251 | .toArray(new Class[] {})); | |
252 | } catch (NoSuchMethodException nsme) { | |
564bbbdb | 253 | // TODO: it seems we do not always need a parameter for each |
cd26ee07 NR |
254 | // level, so we currently try "ALL" levels or "FIRST" level |
255 | // only -> we should check the actual rule and use it | |
256 | ctor = clazz.getDeclaredConstructor(classes.get(0)); | |
257 | Object firstParent = args.get(0); | |
258 | args.clear(); | |
259 | args.add(firstParent); | |
260 | } | |
72648e75 | 261 | desc = null; |
aad14586 | 262 | } else { |
aad14586 NR |
263 | ctor = clazz.getDeclaredConstructor(); |
264 | } | |
265 | ||
266 | ctor.setAccessible(true); | |
72648e75 | 267 | return ctor.newInstance(args.toArray()); |
8c8da42a NR |
268 | } catch (ClassNotFoundException e) { |
269 | throw e; | |
aad14586 | 270 | } catch (NoSuchMethodException e) { |
72648e75 | 271 | if (desc != null) { |
949445ee NR |
272 | throw new NoSuchMethodException("Empty constructor not found: " |
273 | + desc); | |
72648e75 | 274 | } |
8c8da42a NR |
275 | throw e; |
276 | } catch (Exception e) { | |
277 | throw new NoSuchMethodException("Cannot instantiate: " + type); | |
aad14586 | 278 | } |
aad14586 | 279 | } |
db31c358 | 280 | |
8c8da42a NR |
281 | /** |
282 | * Insert a custom serialiser that will take precedence over the default one | |
283 | * or the target class. | |
284 | * | |
285 | * @param serializer | |
286 | * the custom serialiser | |
287 | */ | |
db31c358 NR |
288 | static public void addCustomSerializer(CustomSerializer serializer) { |
289 | customTypes.put(serializer.getType(), serializer); | |
290 | } | |
291 | ||
8c8da42a | 292 | /** |
68532958 | 293 | * Serialise the given object into this {@link OutputStream}. |
8c8da42a NR |
294 | * <p> |
295 | * <b>Important: </b>If the operation fails (with a | |
296 | * {@link NotSerializableException}), the {@link StringBuilder} will be | |
297 | * corrupted (will contain bad, most probably not importable data). | |
298 | * | |
68532958 NR |
299 | * @param out |
300 | * the output {@link OutputStream} to serialise to | |
8c8da42a NR |
301 | * @param o |
302 | * the object to serialise | |
303 | * @param map | |
304 | * the map of already serialised objects (if the given object or | |
305 | * one of its descendant is already present in it, only an ID | |
306 | * will be serialised) | |
307 | * | |
308 | * @throws NotSerializableException | |
309 | * if the object cannot be serialised (in this case, the | |
310 | * {@link StringBuilder} can contain bad, most probably not | |
311 | * importable data) | |
68532958 NR |
312 | * @throws IOException |
313 | * in case of I/O errors | |
8c8da42a | 314 | */ |
68532958 NR |
315 | static void append(OutputStream out, Object o, Map<Integer, Object> map) |
316 | throws NotSerializableException, IOException { | |
db31c358 NR |
317 | |
318 | Field[] fields = new Field[] {}; | |
319 | String type = ""; | |
320 | String id = "NULL"; | |
321 | ||
322 | if (o != null) { | |
323 | int hash = System.identityHashCode(o); | |
324 | fields = o.getClass().getDeclaredFields(); | |
325 | type = o.getClass().getCanonicalName(); | |
326 | if (type == null) { | |
72648e75 NR |
327 | // Anonymous inner classes support |
328 | type = o.getClass().getName(); | |
db31c358 NR |
329 | } |
330 | id = Integer.toString(hash); | |
331 | if (map.containsKey(hash)) { | |
332 | fields = new Field[] {}; | |
333 | } else { | |
334 | map.put(hash, o); | |
335 | } | |
336 | } | |
337 | ||
68532958 NR |
338 | write(out, "{\nREF "); |
339 | write(out, type); | |
340 | write(out, "@"); | |
341 | write(out, id); | |
342 | write(out, ":"); | |
343 | ||
344 | if (!encode(out, o)) { // check if direct value | |
ce0974c4 NR |
345 | try { |
346 | for (Field field : fields) { | |
347 | field.setAccessible(true); | |
db31c358 | 348 | |
e570f7eb NR |
349 | if (field.getName().startsWith("this$") |
350 | || field.isSynthetic() | |
351 | || (field.getModifiers() & Modifier.STATIC) == Modifier.STATIC) { | |
ce0974c4 | 352 | // Do not keep this links of nested classes |
e570f7eb NR |
353 | // Do not keep synthetic fields |
354 | // Do not keep final fields | |
ce0974c4 NR |
355 | continue; |
356 | } | |
db31c358 | 357 | |
68532958 NR |
358 | write(out, "\n"); |
359 | write(out, field.getName()); | |
360 | write(out, ":"); | |
db31c358 | 361 | |
68532958 | 362 | Object value = field.get(o); |
db31c358 | 363 | |
68532958 NR |
364 | if (!encode(out, value)) { |
365 | write(out, "\n"); | |
366 | append(out, value, map); | |
ce0974c4 | 367 | } |
db31c358 | 368 | } |
ce0974c4 NR |
369 | } catch (IllegalArgumentException e) { |
370 | e.printStackTrace(); // should not happen (see | |
371 | // setAccessible) | |
372 | } catch (IllegalAccessException e) { | |
373 | e.printStackTrace(); // should not happen (see | |
374 | // setAccessible) | |
db31c358 | 375 | } |
95c5b4f9 NR |
376 | |
377 | write(out, "\n}"); | |
db31c358 | 378 | } |
db31c358 NR |
379 | } |
380 | ||
5bc55b51 | 381 | /** |
68532958 NR |
382 | * Encode the object into the given {@link OutputStream} if possible and if |
383 | * supported. | |
949445ee NR |
384 | * <p> |
385 | * A supported object in this context means an object we can directly | |
386 | * encode, like an Integer or a String. Custom objects and arrays are also | |
387 | * considered supported, but <b>compound objects are not supported here</b>. | |
388 | * <p> | |
389 | * For compound objects, you should use {@link Exporter}. | |
5bc55b51 | 390 | * |
68532958 NR |
391 | * @param out |
392 | * the {@link OutputStream} to append to | |
5bc55b51 NR |
393 | * @param value |
394 | * the object to encode (can be NULL, which will be encoded) | |
395 | * | |
68532958 NR |
396 | * @return TRUE if success, FALSE if not (the content of the |
397 | * {@link OutputStream} won't be changed in case of failure) | |
398 | * | |
399 | * @throws IOException | |
400 | * in case of I/O error | |
5bc55b51 | 401 | */ |
68532958 | 402 | static boolean encode(OutputStream out, Object value) throws IOException { |
db31c358 | 403 | if (value == null) { |
68532958 | 404 | write(out, "NULL"); |
72648e75 NR |
405 | } else if (value.getClass().getSimpleName().endsWith("[]")) { |
406 | // Simple name does support [] suffix and do not return NULL for | |
407 | // inner anonymous classes | |
3b4319db | 408 | customTypes.get("[]").encode(out, value); |
db31c358 | 409 | } else if (customTypes.containsKey(value.getClass().getCanonicalName())) { |
3b4319db | 410 | customTypes.get(value.getClass().getCanonicalName())// |
68532958 | 411 | .encode(out, value); |
db31c358 | 412 | } else if (value instanceof String) { |
68532958 | 413 | encodeString(out, (String) value); |
db31c358 | 414 | } else if (value instanceof Boolean) { |
68532958 | 415 | write(out, value); |
db31c358 | 416 | } else if (value instanceof Byte) { |
68532958 NR |
417 | write(out, value); |
418 | write(out, "b"); | |
db31c358 | 419 | } else if (value instanceof Character) { |
68532958 NR |
420 | encodeString(out, "" + value); |
421 | write(out, "c"); | |
db31c358 | 422 | } else if (value instanceof Short) { |
68532958 NR |
423 | write(out, value); |
424 | write(out, "s"); | |
db31c358 | 425 | } else if (value instanceof Integer) { |
68532958 | 426 | write(out, value); |
db31c358 | 427 | } else if (value instanceof Long) { |
68532958 NR |
428 | write(out, value); |
429 | write(out, "L"); | |
db31c358 | 430 | } else if (value instanceof Float) { |
68532958 NR |
431 | write(out, value); |
432 | write(out, "F"); | |
db31c358 | 433 | } else if (value instanceof Double) { |
68532958 NR |
434 | write(out, value); |
435 | write(out, "d"); | |
e570f7eb NR |
436 | } else if (value instanceof Enum) { |
437 | String type = value.getClass().getCanonicalName(); | |
68532958 NR |
438 | write(out, type); |
439 | write(out, "."); | |
440 | write(out, ((Enum<?>) value).name()); | |
441 | write(out, ";"); | |
db31c358 NR |
442 | } else { |
443 | return false; | |
444 | } | |
445 | ||
446 | return true; | |
447 | } | |
448 | ||
5bc55b51 | 449 | /** |
949445ee NR |
450 | * Decode the data into an equivalent supported source object. |
451 | * <p> | |
452 | * A supported object in this context means an object we can directly | |
453 | * encode, like an Integer or a String. Custom objects and arrays are also | |
454 | * considered supported, but <b>compound objects are not supported here</b>. | |
455 | * <p> | |
456 | * For compound objects, you should use {@link Importer}. | |
5bc55b51 NR |
457 | * |
458 | * @param encodedValue | |
459 | * the encoded data, cannot be NULL | |
460 | * | |
461 | * @return the object (can be NULL for NULL encoded values) | |
462 | * | |
452f38c8 | 463 | * @throws IOException |
5bc55b51 NR |
464 | * if the content cannot be converted |
465 | */ | |
452f38c8 | 466 | static Object decode(String encodedValue) throws IOException { |
5bc55b51 | 467 | try { |
452f38c8 NR |
468 | String cut = ""; |
469 | if (encodedValue.length() > 1) { | |
470 | cut = encodedValue.substring(0, encodedValue.length() - 1); | |
471 | } | |
472 | ||
5bc55b51 NR |
473 | if (CustomSerializer.isCustom(encodedValue)) { |
474 | // custom:TYPE_NAME:"content is String-encoded" | |
475 | String type = CustomSerializer.typeOf(encodedValue); | |
476 | if (customTypes.containsKey(type)) { | |
564bbbdb NR |
477 | // TODO: we should start with a stream |
478 | InputStream streamEncodedValue = new ByteArrayInputStream( | |
479 | encodedValue.getBytes("UTF-8")); | |
480 | try { | |
481 | return customTypes.get(type).decode(streamEncodedValue); | |
482 | } finally { | |
483 | streamEncodedValue.close(); | |
484 | } | |
5bc55b51 | 485 | } |
452f38c8 | 486 | throw new IOException("Unknown custom type: " + type); |
5bc55b51 NR |
487 | } else if (encodedValue.equals("NULL") |
488 | || encodedValue.equals("null")) { | |
489 | return null; | |
490 | } else if (encodedValue.endsWith("\"")) { | |
491 | return decodeString(encodedValue); | |
492 | } else if (encodedValue.equals("true")) { | |
493 | return true; | |
494 | } else if (encodedValue.equals("false")) { | |
495 | return false; | |
496 | } else if (encodedValue.endsWith("b")) { | |
497 | return Byte.parseByte(cut); | |
498 | } else if (encodedValue.endsWith("c")) { | |
499 | return decodeString(cut).charAt(0); | |
500 | } else if (encodedValue.endsWith("s")) { | |
501 | return Short.parseShort(cut); | |
502 | } else if (encodedValue.endsWith("L")) { | |
503 | return Long.parseLong(cut); | |
504 | } else if (encodedValue.endsWith("F")) { | |
505 | return Float.parseFloat(cut); | |
506 | } else if (encodedValue.endsWith("d")) { | |
507 | return Double.parseDouble(cut); | |
508 | } else if (encodedValue.endsWith(";")) { | |
509 | return decodeEnum(encodedValue); | |
510 | } else { | |
511 | return Integer.parseInt(encodedValue); | |
db31c358 | 512 | } |
5bc55b51 | 513 | } catch (Exception e) { |
452f38c8 NR |
514 | if (e instanceof IOException) { |
515 | throw (IOException) e; | |
5bc55b51 | 516 | } |
452f38c8 | 517 | throw new IOException(e.getMessage()); |
db31c358 NR |
518 | } |
519 | } | |
8c8da42a | 520 | |
68532958 NR |
521 | /** |
522 | * Write the given {@link String} into the given {@link OutputStream} in | |
523 | * UTF-8. | |
524 | * | |
525 | * @param out | |
526 | * the {@link OutputStream} | |
527 | * @param data | |
528 | * the data to write, cannot be NULL | |
529 | * | |
530 | * @throws IOException | |
531 | * in case of I/O error | |
532 | */ | |
533 | static void write(OutputStream out, Object data) throws IOException { | |
534 | try { | |
535 | out.write(data.toString().getBytes("UTF-8")); | |
536 | } catch (UnsupportedEncodingException e) { | |
537 | // A conforming JVM is required to support UTF-8 | |
538 | e.printStackTrace(); | |
539 | } | |
540 | } | |
541 | ||
8c8da42a NR |
542 | /** |
543 | * Return the corresponding class or throw an {@link Exception} if it | |
544 | * cannot. | |
545 | * | |
546 | * @param type | |
547 | * the class name to look for | |
548 | * | |
549 | * @return the class (will never be NULL) | |
550 | * | |
551 | * @throws ClassNotFoundException | |
552 | * if the class cannot be found | |
553 | * @throws NoSuchMethodException | |
554 | * if the class cannot be created (usually because it or its | |
555 | * enclosing class doesn't have an empty constructor) | |
556 | */ | |
557 | static private Class<?> getClass(String type) | |
558 | throws ClassNotFoundException, NoSuchMethodException { | |
aad14586 NR |
559 | Class<?> clazz = null; |
560 | try { | |
561 | clazz = Class.forName(type); | |
562 | } catch (ClassNotFoundException e) { | |
563 | int pos = type.length(); | |
564 | pos = type.lastIndexOf(".", pos); | |
565 | if (pos >= 0) { | |
566 | String parentType = type.substring(0, pos); | |
567 | String nestedType = type.substring(pos + 1); | |
568 | Class<?> javaParent = null; | |
569 | try { | |
570 | javaParent = getClass(parentType); | |
571 | parentType = javaParent.getName(); | |
572 | clazz = Class.forName(parentType + "$" + nestedType); | |
573 | } catch (Exception ee) { | |
574 | } | |
db31c358 | 575 | |
aad14586 NR |
576 | if (javaParent == null) { |
577 | throw new NoSuchMethodException( | |
578 | "Class not found: " | |
579 | + type | |
580 | + " (the enclosing class cannot be created: maybe it doesn't have an empty constructor?)"); | |
581 | } | |
582 | } | |
583 | } | |
584 | ||
8c8da42a NR |
585 | if (clazz == null) { |
586 | throw new ClassNotFoundException("Class not found: " + type); | |
587 | } | |
588 | ||
aad14586 NR |
589 | return clazz; |
590 | } | |
8c8da42a | 591 | |
e570f7eb | 592 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
68532958 | 593 | static private Enum<?> decodeEnum(String escaped) { |
e570f7eb NR |
594 | // escaped: be.xxx.EnumType.VALUE; |
595 | int pos = escaped.lastIndexOf("."); | |
596 | String type = escaped.substring(0, pos); | |
597 | String name = escaped.substring(pos + 1, escaped.length() - 1); | |
598 | ||
599 | try { | |
600 | return Enum.valueOf((Class<Enum>) getClass(type), name); | |
601 | } catch (Exception e) { | |
e570f7eb NR |
602 | throw new UnknownFormatConversionException("Unknown enum: <" + type |
603 | + "> " + name); | |
604 | } | |
605 | } | |
606 | ||
db31c358 | 607 | // aa bb -> "aa\tbb" |
68532958 | 608 | static void encodeString(OutputStream out, String raw) throws IOException { |
3b4319db | 609 | // TODO: not. efficient. |
68532958 NR |
610 | out.write('\"'); |
611 | // TODO !! utf-8 required | |
db31c358 | 612 | for (char car : raw.toCharArray()) { |
68532958 NR |
613 | encodeString(out, car); |
614 | } | |
615 | out.write('\"'); | |
616 | } | |
617 | ||
618 | // aa bb -> "aa\tbb" | |
619 | static void encodeString(OutputStream out, InputStream raw) | |
620 | throws IOException { | |
621 | out.write('\"'); | |
564bbbdb | 622 | byte buffer[] = new byte[4096]; |
68532958 NR |
623 | for (int len = 0; (len = raw.read(buffer)) > 0;) { |
624 | for (int i = 0; i < len; i++) { | |
625 | // TODO: not 100% correct, look up howto for UTF-8 | |
626 | encodeString(out, (char) buffer[i]); | |
db31c358 NR |
627 | } |
628 | } | |
68532958 NR |
629 | out.write('\"'); |
630 | } | |
631 | ||
632 | // for encode string, NOT to encode a char by itself! | |
633 | static void encodeString(OutputStream out, char raw) throws IOException { | |
634 | switch (raw) { | |
635 | case '\\': | |
636 | out.write('\\'); | |
637 | out.write('\\'); | |
638 | break; | |
639 | case '\r': | |
640 | out.write('\\'); | |
641 | out.write('r'); | |
642 | break; | |
643 | case '\n': | |
644 | out.write('\\'); | |
645 | out.write('n'); | |
646 | break; | |
647 | case '"': | |
648 | out.write('\\'); | |
649 | out.write('\"'); | |
650 | break; | |
651 | default: | |
652 | out.write(raw); | |
653 | break; | |
654 | } | |
db31c358 NR |
655 | } |
656 | ||
657 | // "aa\tbb" -> aa bb | |
68532958 | 658 | static String decodeString(String escaped) { |
db31c358 NR |
659 | StringBuilder builder = new StringBuilder(); |
660 | ||
661 | boolean escaping = false; | |
662 | for (char car : escaped.toCharArray()) { | |
663 | if (!escaping) { | |
664 | if (car == '\\') { | |
665 | escaping = true; | |
666 | } else { | |
667 | builder.append(car); | |
668 | } | |
669 | } else { | |
670 | switch (car) { | |
671 | case '\\': | |
672 | builder.append('\\'); | |
673 | break; | |
674 | case 'r': | |
675 | builder.append('\r'); | |
676 | break; | |
677 | case 'n': | |
678 | builder.append('\n'); | |
679 | break; | |
680 | case '"': | |
681 | builder.append('"'); | |
682 | break; | |
683 | } | |
684 | escaping = false; | |
685 | } | |
686 | } | |
687 | ||
0988831f | 688 | return builder.substring(1, builder.length() - 1); |
db31c358 NR |
689 | } |
690 | } |