1 package be
.nikiroo
.utils
.serial
;
3 import java
.io
.IOException
;
4 import java
.lang
.reflect
.Field
;
5 import java
.util
.HashMap
;
7 import java
.util
.Scanner
;
9 import be
.nikiroo
.utils
.StringUtils
;
12 * A simple class that can accept the output of {@link Exporter} to recreate
13 * objects as they were sent to said exporter.
15 * This class requires the objects (and their potential enclosing objects) to
16 * have an empty constructor, and does not support inner classes (it does
17 * support nested classes, though).
21 public class Importer
{
24 private Importer child
;
25 private Map
<String
, Object
> map
;
27 private String currentFieldName
;
30 * Create a new {@link Importer}.
33 map
= new HashMap
<String
, Object
>();
34 map
.put("NULL", null);
37 private Importer(Map
<String
, Object
> map
) {
42 * Read some data into this {@link Importer}: it can be the full serialised
43 * content, or a number of lines of it (any given line <b>MUST</b> be
44 * complete though) and accumulate it with the already present data.
49 * @return itself so it can be chained
51 * @throws NoSuchFieldException
52 * if the serialised data contains information about a field
53 * which does actually not exist in the class we know of
54 * @throws NoSuchMethodException
55 * if a class described in the serialised data cannot be created
56 * because it is not compatible with this code
57 * @throws ClassNotFoundException
58 * if a class described in the serialised data cannot be found
60 public Importer
read(String data
) throws NoSuchFieldException
,
61 NoSuchMethodException
, ClassNotFoundException
{
64 Scanner scan
= new Scanner(data
);
65 scan
.useDelimiter("\n");
66 while (scan
.hasNext()) {
67 String line
= scan
.next();
69 if (line
.startsWith("ZIP:")) {
70 line
= StringUtils
.unzip64(line
.substring("ZIP:".length()));
77 } catch (IOException e
) {
78 throw new NoSuchMethodException(
79 "Internal error when decoding ZIP content: input may be corrupt");
86 * Read a single (whole) line of serialised data into this {@link Importer}
87 * and accumulate it with the already present data.
92 * @return TRUE if we are just done with one object or sub-object
94 * @throws NoSuchFieldException
95 * if the serialised data contains information about a field
96 * which does actually not exist in the class we know of
97 * @throws NoSuchMethodException
98 * if a class described in the serialised data cannot be created
99 * because it is not compatible with this code
100 * @throws ClassNotFoundException
101 * if a class described in the serialised data cannot be found
103 private boolean processLine(String line
) throws NoSuchFieldException
,
104 NoSuchMethodException
, ClassNotFoundException
{
105 // Defer to latest child if any
107 if (child
.processLine(line
)) {
108 if (currentFieldName
!= null) {
109 setField(currentFieldName
, child
.getValue());
110 currentFieldName
= null;
118 if (line
.equals("{")) { // START: new child if needed
120 child
= new Importer(map
);
122 } else if (line
.equals("}")) { // STOP: report self to parent
124 } else if (line
.startsWith("REF ")) { // REF: create/link self
125 String
[] tab
= line
.substring("REF ".length()).split("@");
126 String type
= tab
[0];
127 tab
= tab
[1].split(":");
130 link
= map
.containsKey(ref
);
134 if (line
.endsWith(":")) {
136 me
= SerialUtils
.createObject(type
);
139 int pos
= line
.indexOf(":");
140 String encodedValue
= line
.substring(pos
+ 1);
141 me
= SerialUtils
.decode(encodedValue
);
145 } else { // FIELD: new field *or* direct simple value
146 if (line
.endsWith(":")) {
147 // field value is compound
148 currentFieldName
= line
.substring(0, line
.length() - 1);
149 } else if (line
.startsWith(":") || !line
.contains(":")) {
150 // not a field value but a direct value
151 me
= SerialUtils
.decode(line
);
153 // field value is direct
154 int pos
= line
.indexOf(":");
155 String fieldName
= line
.substring(0, pos
);
156 String encodedValue
= line
.substring(pos
+ 1);
158 value
= SerialUtils
.decode(encodedValue
);
160 // To support simple types directly:
164 setField(fieldName
, value
);
172 private void setField(String name
, Object value
)
173 throws NoSuchFieldException
{
176 Field field
= me
.getClass().getDeclaredField(name
);
178 field
.setAccessible(true);
179 field
.set(me
, value
);
180 } catch (NoSuchFieldException e
) {
181 throw new NoSuchFieldException(String
.format(
182 "Field \"%s\" was not found in object of type \"%s\".",
183 name
, me
.getClass().getCanonicalName()));
184 } catch (Exception e
) {
185 throw new NoSuchFieldException(String
.format(
186 "Internal error when setting \"%s.%s\": %s", me
.getClass()
187 .getCanonicalName(), name
, e
.getMessage()));
192 * Return the current deserialised value.
194 * @return the current value
196 public Object
getValue() {