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");
80 while (stream
.next()) {
83 throw new NullPointerException(
84 "InputStream empty, normal termination");
90 boolean zip
= stream
.startsWiths("ZIP:");
91 boolean b64
= stream
.startsWiths("B64:");
94 stream
.skip("XXX:".length());
95 InputStream decoded
= StringUtils
.unbase64(stream
.open(),
114 * Read a single (whole) line of serialised data into this {@link Importer}
115 * and accumulate it with the already present data.
120 * @return TRUE if we are just done with one object or sub-object
122 * @throws NoSuchFieldException
123 * if the serialised data contains information about a field
124 * which does actually not exist in the class we know of
125 * @throws NoSuchMethodException
126 * if a class described in the serialised data cannot be created
127 * because it is not compatible with this code
128 * @throws ClassNotFoundException
129 * if a class described in the serialised data cannot be found
130 * @throws IOException
131 * if the content cannot be read (for instance, corrupt data)
133 private boolean processLine(InputStream in
) throws NoSuchFieldException
,
134 NoSuchMethodException
, ClassNotFoundException
, IOException
{
136 // Defer to latest child if any
138 if (child
.processLine(in
)) {
139 if (currentFieldName
!= null) {
140 setField(currentFieldName
, child
.getValue());
141 currentFieldName
= null;
149 // TODO use the stream, Luke
150 String line
= IOUtils
.readSmallStream(in
);
152 if (line
.isEmpty()) {
156 if (line
.equals("{")) { // START: new child if needed
158 child
= new Importer(map
);
160 } else if (line
.equals("}")) { // STOP: report self to parent
162 } else if (line
.startsWith("REF ")) { // REF: create/link self
163 String
[] tab
= line
.substring("REF ".length()).split("@");
164 String type
= tab
[0];
165 tab
= tab
[1].split(":");
168 link
= map
.containsKey(ref
);
172 if (line
.endsWith(":")) {
175 me
= SerialUtils
.createObject(type
);
176 } catch (NoSuchMethodException e
) {
177 System
.out
.println("LINE: <" + line
+ ">");
182 int pos
= line
.indexOf(":");
183 String encodedValue
= line
.substring(pos
+ 1);
184 me
= SerialUtils
.decode(encodedValue
);
188 } else { // FIELD: new field *or* direct simple value
189 if (line
.endsWith(":")) {
190 // field value is compound
191 currentFieldName
= line
.substring(0, line
.length() - 1);
192 } else if (line
.startsWith(":") || !line
.contains(":")
193 || line
.startsWith("\"") || CustomSerializer
.isCustom(line
)) {
194 // not a field value but a direct value
195 me
= SerialUtils
.decode(line
);
197 // field value is direct
198 int pos
= line
.indexOf(":");
199 String fieldName
= line
.substring(0, pos
);
200 String encodedValue
= line
.substring(pos
+ 1);
202 value
= SerialUtils
.decode(encodedValue
);
204 // To support simple types directly:
208 setField(fieldName
, value
);
216 private void setField(String name
, Object value
)
217 throws NoSuchFieldException
{
220 Field field
= me
.getClass().getDeclaredField(name
);
222 field
.setAccessible(true);
223 field
.set(me
, value
);
224 } catch (NoSuchFieldException e
) {
225 throw new NoSuchFieldException(String
.format(
226 "Field \"%s\" was not found in object of type \"%s\".",
227 name
, me
.getClass().getCanonicalName()));
228 } catch (Exception e
) {
229 throw new NoSuchFieldException(String
.format(
230 "Internal error when setting \"%s.%s\": %s", me
.getClass()
231 .getCanonicalName(), name
, e
.getMessage()));
236 * Return the current deserialised value.
238 * @return the current value
240 public Object
getValue() {