d21f1dd341c9085af11a08015a05c5fb04fc5c14
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 map
= new HashMap
<String
, Object
>();
31 map
.put("NULL", null);
34 private Importer(Map
<String
, Object
> map
) {
39 * Read some data into this {@link Importer}: it can be the full serialised
40 * content, or a number of lines of it (any given line <b>MUST</b> be
41 * complete though) and accumulate it with the already present data.
46 * @return itself so it can be chained
48 * @throws NoSuchFieldException
49 * if the serialised data contains information about a field
50 * which does actually not exist in the class we know of
51 * @throws NoSuchMethodException
52 * if a class described in the serialised data cannot be created
53 * because it is not compatible with this code
54 * @throws ClassNotFoundException
55 * if a class described in the serialised data cannot be found
57 public Importer
read(String data
) throws NoSuchFieldException
,
58 NoSuchMethodException
, ClassNotFoundException
{
61 Scanner scan
= new Scanner(data
);
62 scan
.useDelimiter("\n");
63 while (scan
.hasNext()) {
64 String line
= scan
.next();
66 if (line
.startsWith("ZIP:")) {
67 line
= StringUtils
.unzip64(line
.substring("ZIP:".length()));
74 } catch (IOException e
) {
75 throw new NoSuchMethodException(
76 "Internal error when decoding ZIP content: input may be corrupt");
83 * Read a single (whole) line of serialised data into this {@link Importer}
84 * and accumulate it with the already present data.
89 * @return TRUE if we are just done with one object or sub-object
91 * @throws NoSuchFieldException
92 * if the serialised data contains information about a field
93 * which does actually not exist in the class we know of
94 * @throws NoSuchMethodException
95 * if a class described in the serialised data cannot be created
96 * because it is not compatible with this code
97 * @throws ClassNotFoundException
98 * if a class described in the serialised data cannot be found
100 private boolean processLine(String line
) throws NoSuchFieldException
,
101 NoSuchMethodException
, ClassNotFoundException
{
102 // Defer to latest child if any
104 if (child
.processLine(line
)) {
105 if (currentFieldName
!= null) {
106 setField(currentFieldName
, child
.getValue());
107 currentFieldName
= null;
115 if (line
.equals("{")) { // START: new child if needed
117 child
= new Importer(map
);
119 } else if (line
.equals("}")) { // STOP: report self to parent
121 } else if (line
.startsWith("REF ")) { // REF: create/link self
122 String
[] tab
= line
.substring("REF ".length()).split("@");
123 String type
= tab
[0];
124 tab
= tab
[1].split(":");
127 link
= map
.containsKey(ref
);
131 if (line
.endsWith(":")) {
133 me
= SerialUtils
.createObject(type
);
136 int pos
= line
.indexOf(":");
137 String encodedValue
= line
.substring(pos
+ 1);
138 me
= SerialUtils
.decode(encodedValue
);
142 } else { // FIELD: new field *or* direct simple value
143 if (line
.endsWith(":")) {
144 // field value is compound
145 currentFieldName
= line
.substring(0, line
.length() - 1);
146 } else if (line
.startsWith(":") || !line
.contains(":")) {
147 // not a field value but a direct value
148 me
= SerialUtils
.decode(line
);
150 // field value is direct
151 int pos
= line
.indexOf(":");
152 String fieldName
= line
.substring(0, pos
);
153 String encodedValue
= line
.substring(pos
+ 1);
155 value
= SerialUtils
.decode(encodedValue
);
157 // To support simple types directly:
161 setField(fieldName
, value
);
169 private void setField(String name
, Object value
)
170 throws NoSuchFieldException
{
173 Field field
= me
.getClass().getDeclaredField(name
);
175 field
.setAccessible(true);
176 field
.set(me
, value
);
177 } catch (NoSuchFieldException e
) {
178 throw new NoSuchFieldException(String
.format(
179 "Field \"%s\" was not found in object of type \"%s\".",
180 name
, me
.getClass().getCanonicalName()));
181 } catch (Exception e
) {
182 throw new NoSuchFieldException(String
.format(
183 "Internal error when setting \"%s.%s\": %s", me
.getClass()
184 .getCanonicalName(), name
, e
.getMessage()));
189 * Return the current deserialised value.
191 * @return the current value
193 public Object
getValue() {