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