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
;
8 import java
.util
.zip
.GZIPInputStream
;
10 import be
.nikiroo
.utils
.IOUtils
;
11 import be
.nikiroo
.utils
.streams
.Base64InputStream
;
12 import be
.nikiroo
.utils
.streams
.BufferedInputStream
;
13 import be
.nikiroo
.utils
.streams
.NextableInputStream
;
14 import be
.nikiroo
.utils
.streams
.NextableInputStreamStep
;
17 * A simple class that can accept the output of {@link Exporter} to recreate
18 * objects as they were sent to said exporter.
20 * This class requires the objects (and their potential enclosing objects) to
21 * have an empty constructor, and does not support inner classes (it does
22 * support nested classes, though).
26 public class Importer
{
29 private Importer child
;
30 private Map
<String
, Object
> map
;
32 private String currentFieldName
;
35 * Create a new {@link Importer}.
38 map
= new HashMap
<String
, Object
>();
39 map
.put("NULL", null);
42 private Importer(Map
<String
, Object
> map
) {
47 * Read some data into this {@link Importer}: it can be the full serialised
48 * content, or a number of lines of it (any given line <b>MUST</b> be
49 * complete though) and accumulate it with the already present data.
54 * @return itself so it can be chained
56 * @throws NoSuchFieldException
57 * if the serialised data contains information about a field
58 * which does actually not exist in the class we know of
59 * @throws NoSuchMethodException
60 * if a class described in the serialised data cannot be created
61 * because it is not compatible with this code
62 * @throws ClassNotFoundException
63 * if a class described in the serialised data cannot be found
65 * if the content cannot be read (for instance, corrupt data)
66 * @throws NullPointerException
67 * if the stream is empty
69 public Importer
read(InputStream in
) throws NoSuchFieldException
,
70 NoSuchMethodException
, ClassNotFoundException
, IOException
,
71 NullPointerException
{
73 NextableInputStream stream
= new NextableInputStream(in
,
74 new NextableInputStreamStep('\n'));
78 throw new NullPointerException("InputStream is null");
82 while (stream
.next()) {
85 throw new NullPointerException(
86 "InputStream empty, normal termination");
92 boolean zip
= stream
.startsWith("ZIP:");
93 boolean b64
= stream
.startsWith("B64:");
96 stream
.skip("XXX:".length());
98 InputStream decoded
= stream
.open();
100 decoded
= new GZIPInputStream(decoded
);
102 decoded
= new Base64InputStream(decoded
, false);
121 * Read a single (whole) line of serialised data into this {@link Importer}
122 * and accumulate it with the already present data.
127 * @return TRUE if we are just done with one object or sub-object
129 * @throws NoSuchFieldException
130 * if the serialised data contains information about a field
131 * which does actually not exist in the class we know of
132 * @throws NoSuchMethodException
133 * if a class described in the serialised data cannot be created
134 * because it is not compatible with this code
135 * @throws ClassNotFoundException
136 * if a class described in the serialised data cannot be found
137 * @throws IOException
138 * if the content cannot be read (for instance, corrupt data)
140 private boolean processLine(BufferedInputStream in
)
141 throws NoSuchFieldException
, NoSuchMethodException
,
142 ClassNotFoundException
, IOException
{
144 // Defer to latest child if any
146 if (child
.processLine(in
)) {
147 if (currentFieldName
!= null) {
148 setField(currentFieldName
, child
.getValue());
149 currentFieldName
= null;
158 if (in
.is("{")) { // START: new child if needed
160 child
= new Importer(map
);
164 } else if (in
.is("}")) { // STOP: report self to parent
170 if (CustomSerializer
.isCustom(in
)) {
171 // not a field value but a direct value
172 me
= SerialUtils
.decode(in
);
177 if (in
.startsWith("REF ")) { // REF: create/link self
178 // here, line is REF type@999:xxx
181 NextableInputStream stream
= new NextableInputStream(in
,
182 new NextableInputStreamStep(':'));
186 stream
.skip("REF ".length());
187 String header
= IOUtils
.readSmallStream(stream
);
189 String
[] tab
= header
.split("@");
190 if (tab
.length
!= 2) {
191 throw new IOException("Bad import header line: " + header
);
193 String type
= tab
[0];
198 link
= map
.containsKey(ref
);
205 me
= SerialUtils
.createObject(type
);
208 me
= SerialUtils
.decode(stream
);
219 if (SerialUtils
.isDirectValue(in
)) {
220 // not a field value but a direct value
221 me
= SerialUtils
.decode(in
);
225 if (in
.startsWith("^")) {
228 NextableInputStream nameThenContent
= new NextableInputStream(in
,
229 new NextableInputStreamStep(':'));
232 nameThenContent
.next();
233 String fieldName
= IOUtils
.readSmallStream(nameThenContent
);
235 if (nameThenContent
.nextAll() && !nameThenContent
.eof()) {
236 // field value is direct or custom
238 value
= SerialUtils
.decode(nameThenContent
);
240 // To support simple types directly:
244 setField(fieldName
, value
);
247 // field value is compound
248 currentFieldName
= fieldName
;
251 nameThenContent
.close(false);
257 String line
= IOUtils
.readSmallStream(in
);
258 throw new IOException("Line cannot be processed: <" + line
+ ">");
261 private void setField(String name
, Object value
)
262 throws NoSuchFieldException
{
265 Field field
= me
.getClass().getDeclaredField(name
);
267 field
.setAccessible(true);
268 field
.set(me
, value
);
269 } catch (NoSuchFieldException e
) {
270 throw new NoSuchFieldException(String
.format(
271 "Field \"%s\" was not found in object of type \"%s\".",
272 name
, me
.getClass().getCanonicalName()));
273 } catch (Exception e
) {
274 throw new NoSuchFieldException(String
.format(
275 "Internal error when setting \"%s.%s\": %s", me
.getClass()
276 .getCanonicalName(), name
, e
.getMessage()));
281 * Return the current deserialised value.
283 * @return the current value
285 public Object
getValue() {