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 * if the content cannot be read (for instance, corrupt data)
62 public Importer
read(String data
) throws NoSuchFieldException
,
63 NoSuchMethodException
, ClassNotFoundException
, IOException
{
65 Scanner scan
= new Scanner(data
);
67 scan
.useDelimiter("\n");
68 while (scan
.hasNext()) {
69 String line
= scan
.next();
71 if (line
.startsWith("ZIP:")) {
73 line
= StringUtils
.unzip64(line
.substring("ZIP:"
75 } catch (IOException e
) {
76 throw new IOException(
77 "Internal error when decoding ZIP content: input may be corrupt");
92 * Read a single (whole) line of serialised data into this {@link Importer}
93 * and accumulate it with the already present data.
98 * @return TRUE if we are just done with one object or sub-object
100 * @throws NoSuchFieldException
101 * if the serialised data contains information about a field
102 * which does actually not exist in the class we know of
103 * @throws NoSuchMethodException
104 * if a class described in the serialised data cannot be created
105 * because it is not compatible with this code
106 * @throws ClassNotFoundException
107 * if a class described in the serialised data cannot be found
108 * @throws IOException
109 * if the content cannot be read (for instance, corrupt data)
111 private boolean processLine(String line
) throws NoSuchFieldException
,
112 NoSuchMethodException
, ClassNotFoundException
, IOException
{
113 // Defer to latest child if any
115 if (child
.processLine(line
)) {
116 if (currentFieldName
!= null) {
117 setField(currentFieldName
, child
.getValue());
118 currentFieldName
= null;
126 if (line
.equals("{")) { // START: new child if needed
128 child
= new Importer(map
);
130 } else if (line
.equals("}")) { // STOP: report self to parent
132 } else if (line
.startsWith("REF ")) { // REF: create/link self
133 String
[] tab
= line
.substring("REF ".length()).split("@");
134 String type
= tab
[0];
135 tab
= tab
[1].split(":");
138 link
= map
.containsKey(ref
);
142 if (line
.endsWith(":")) {
144 me
= SerialUtils
.createObject(type
);
147 int pos
= line
.indexOf(":");
148 String encodedValue
= line
.substring(pos
+ 1);
149 me
= SerialUtils
.decode(encodedValue
);
153 } else { // FIELD: new field *or* direct simple value
154 if (line
.endsWith(":")) {
155 // field value is compound
156 currentFieldName
= line
.substring(0, line
.length() - 1);
157 } else if (line
.startsWith(":") || !line
.contains(":")) {
158 // not a field value but a direct value
159 me
= SerialUtils
.decode(line
);
161 // field value is direct
162 int pos
= line
.indexOf(":");
163 String fieldName
= line
.substring(0, pos
);
164 String encodedValue
= line
.substring(pos
+ 1);
166 value
= SerialUtils
.decode(encodedValue
);
168 // To support simple types directly:
172 setField(fieldName
, value
);
180 private void setField(String name
, Object value
)
181 throws NoSuchFieldException
{
184 Field field
= me
.getClass().getDeclaredField(name
);
186 field
.setAccessible(true);
187 field
.set(me
, value
);
188 } catch (NoSuchFieldException e
) {
189 throw new NoSuchFieldException(String
.format(
190 "Field \"%s\" was not found in object of type \"%s\".",
191 name
, me
.getClass().getCanonicalName()));
192 } catch (Exception e
) {
193 throw new NoSuchFieldException(String
.format(
194 "Internal error when setting \"%s.%s\": %s", me
.getClass()
195 .getCanonicalName(), name
, e
.getMessage()));
200 * Return the current deserialised value.
202 * @return the current value
204 public Object
getValue() {