1 package be
.nikiroo
.utils
.serial
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.lang
.reflect
.Field
;
6 import java
.util
.HashMap
;
9 import be
.nikiroo
.utils
.IOUtils
;
10 import be
.nikiroo
.utils
.StringUtils
;
11 import be
.nikiroo
.utils
.streams
.NextableInputStream
;
12 import be
.nikiroo
.utils
.streams
.NextableInputStreamStep
;
15 * A simple class that can accept the output of {@link Exporter} to recreate
16 * objects as they were sent to said exporter.
18 * This class requires the objects (and their potential enclosing objects) to
19 * have an empty constructor, and does not support inner classes (it does
20 * support nested classes, though).
24 public class Importer
{
27 private Importer child
;
28 private Map
<String
, Object
> map
;
30 private String currentFieldName
;
33 * Create a new {@link Importer}.
36 map
= new HashMap
<String
, Object
>();
37 map
.put("NULL", null);
40 private Importer(Map
<String
, Object
> map
) {
45 * Read some data into this {@link Importer}: it can be the full serialised
46 * content, or a number of lines of it (any given line <b>MUST</b> be
47 * complete though) and accumulate it with the already present data.
52 * @return itself so it can be chained
54 * @throws NoSuchFieldException
55 * if the serialised data contains information about a field
56 * which does actually not exist in the class we know of
57 * @throws NoSuchMethodException
58 * if a class described in the serialised data cannot be created
59 * because it is not compatible with this code
60 * @throws ClassNotFoundException
61 * if a class described in the serialised data cannot be found
63 * if the content cannot be read (for instance, corrupt data)
64 * @throws NullPointerException
65 * if the stream is empty
67 public Importer
read(InputStream in
) throws NoSuchFieldException
,
68 NoSuchMethodException
, ClassNotFoundException
, IOException
,
69 NullPointerException
{
71 NextableInputStream stream
= new NextableInputStream(in
,
72 new NextableInputStreamStep('\n'));
76 throw new NullPointerException("InputStream is null");
79 throw new NullPointerException("InputStream is empty");
82 while (stream
.next()) {
83 boolean zip
= stream
.startsWiths("ZIP:");
84 boolean b64
= stream
.startsWiths("B64:");
87 stream
.skip("XXX:".length());
88 InputStream decoded
= StringUtils
.unbase64(stream
.open(),
107 * Read a single (whole) line of serialised data into this {@link Importer}
108 * and accumulate it with the already present data.
113 * @return TRUE if we are just done with one object or sub-object
115 * @throws NoSuchFieldException
116 * if the serialised data contains information about a field
117 * which does actually not exist in the class we know of
118 * @throws NoSuchMethodException
119 * if a class described in the serialised data cannot be created
120 * because it is not compatible with this code
121 * @throws ClassNotFoundException
122 * if a class described in the serialised data cannot be found
123 * @throws IOException
124 * if the content cannot be read (for instance, corrupt data)
126 private boolean processLine(InputStream in
) throws NoSuchFieldException
,
127 NoSuchMethodException
, ClassNotFoundException
, IOException
{
129 // Defer to latest child if any
131 if (child
.processLine(in
)) {
132 if (currentFieldName
!= null) {
133 setField(currentFieldName
, child
.getValue());
134 currentFieldName
= null;
142 // TODO use the stream, Luke
143 String line
= IOUtils
.readSmallStream(in
);
145 if (line
.equals("{")) { // START: new child if needed
147 child
= new Importer(map
);
149 } else if (line
.equals("}")) { // STOP: report self to parent
151 } else if (line
.startsWith("REF ")) { // REF: create/link self
152 String
[] tab
= line
.substring("REF ".length()).split("@");
153 String type
= tab
[0];
154 tab
= tab
[1].split(":");
157 link
= map
.containsKey(ref
);
161 if (line
.endsWith(":")) {
163 me
= SerialUtils
.createObject(type
);
166 int pos
= line
.indexOf(":");
167 String encodedValue
= line
.substring(pos
+ 1);
168 me
= SerialUtils
.decode(encodedValue
);
172 } else { // FIELD: new field *or* direct simple value
173 if (line
.endsWith(":")) {
174 // field value is compound
175 currentFieldName
= line
.substring(0, line
.length() - 1);
176 } else if (line
.startsWith(":") || !line
.contains(":")
177 || line
.startsWith("\"") || CustomSerializer
.isCustom(line
)) {
178 // not a field value but a direct value
179 me
= SerialUtils
.decode(line
);
181 // field value is direct
182 int pos
= line
.indexOf(":");
183 String fieldName
= line
.substring(0, pos
);
184 String encodedValue
= line
.substring(pos
+ 1);
186 value
= SerialUtils
.decode(encodedValue
);
188 // To support simple types directly:
192 setField(fieldName
, value
);
200 private void setField(String name
, Object value
)
201 throws NoSuchFieldException
{
204 Field field
= me
.getClass().getDeclaredField(name
);
206 field
.setAccessible(true);
207 field
.set(me
, value
);
208 } catch (NoSuchFieldException e
) {
209 throw new NoSuchFieldException(String
.format(
210 "Field \"%s\" was not found in object of type \"%s\".",
211 name
, me
.getClass().getCanonicalName()));
212 } catch (Exception e
) {
213 throw new NoSuchFieldException(String
.format(
214 "Internal error when setting \"%s.%s\": %s", me
.getClass()
215 .getCanonicalName(), name
, e
.getMessage()));
220 * Return the current deserialised value.
222 * @return the current value
224 public Object
getValue() {