6718fb8ecf623b8bd66350be1d1f51685836b2cf
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / Importer.java
1 package be.nikiroo.utils.serial;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.lang.reflect.Field;
6 import java.util.HashMap;
7 import java.util.Map;
8 import java.util.zip.GZIPInputStream;
9
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;
15
16 /**
17 * A simple class that can accept the output of {@link Exporter} to recreate
18 * objects as they were sent to said exporter.
19 * <p>
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).
23 *
24 * @author niki
25 */
26 public class Importer {
27 private Boolean link;
28 private Object me;
29 private Importer child;
30 private Map<String, Object> map;
31
32 private String currentFieldName;
33
34 /**
35 * Create a new {@link Importer}.
36 */
37 public Importer() {
38 map = new HashMap<String, Object>();
39 map.put("NULL", null);
40 }
41
42 private Importer(Map<String, Object> map) {
43 this.map = map;
44 }
45
46 /**
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.
50 *
51 * @param in
52 * the data to parse
53 *
54 * @return itself so it can be chained
55 *
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
64 * @throws IOException
65 * if the content cannot be read (for instance, corrupt data)
66 * @throws NullPointerException
67 * if the stream is empty
68 */
69 public Importer read(InputStream in) throws NoSuchFieldException,
70 NoSuchMethodException, ClassNotFoundException, IOException,
71 NullPointerException {
72
73 NextableInputStream stream = new NextableInputStream(in,
74 new NextableInputStreamStep('\n'));
75
76 try {
77 if (in == null) {
78 throw new NullPointerException("InputStream is null");
79 }
80
81 boolean first = true;
82 while (stream.next()) {
83 if (stream.eof()) {
84 if (first) {
85 throw new NullPointerException(
86 "InputStream empty, normal termination");
87 }
88 return this;
89 }
90 first = false;
91
92 boolean zip = stream.startsWith("ZIP:");
93 boolean b64 = stream.startsWith("B64:");
94
95 if (zip || b64) {
96 stream.skip("XXX:".length());
97
98 InputStream decoded = stream.open();
99 if (zip) {
100 decoded = new GZIPInputStream(decoded);
101 }
102 decoded = new Base64InputStream(decoded, false);
103
104 try {
105 read(decoded);
106 } finally {
107 decoded.close();
108 }
109 } else {
110 processLine(stream);
111 }
112 }
113 } finally {
114 stream.close(false);
115 }
116
117 return this;
118 }
119
120 /**
121 * Read a single (whole) line of serialised data into this {@link Importer}
122 * and accumulate it with the already present data.
123 *
124 * @param in
125 * the line to parse
126 *
127 * @return TRUE if we are just done with one object or sub-object
128 *
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)
139 */
140 private boolean processLine(BufferedInputStream in)
141 throws NoSuchFieldException, NoSuchMethodException,
142 ClassNotFoundException, IOException {
143
144 // Defer to latest child if any
145 if (child != null) {
146 if (child.processLine(in)) {
147 if (currentFieldName != null) {
148 setField(currentFieldName, child.getValue());
149 currentFieldName = null;
150 }
151 child = null;
152 }
153
154 return false;
155 }
156
157 // Start/Stop object
158 if (in.is("{")) { // START: new child if needed
159 if (link != null) {
160 child = new Importer(map);
161 }
162 in.end();
163 return false;
164 } else if (in.is("}")) { // STOP: report self to parent
165 in.end();
166 return true;
167 }
168
169 // Custom objects
170 if (CustomSerializer.isCustom(in)) {
171 // not a field value but a direct value
172 String line = IOUtils.readSmallStream(in);
173 me = SerialUtils.decode(line);
174 return false;
175 }
176
177 // TODO use the stream, Luke
178 // .. at least for REF
179 String line = IOUtils.readSmallStream(in);
180
181 if (line.startsWith("REF ")) { // REF: create/link self
182 // TODO: here, line is REF type@999:xxx
183 // xxx is optional
184 // note: use .end() when containsKey(ref)
185 String[] tab = line.substring("REF ".length()).split("@");
186 String type = tab[0];
187 tab = tab[1].split(":");
188 String ref = tab[0];
189
190 link = map.containsKey(ref);
191 if (link) {
192 me = map.get(ref);
193 } else {
194 if (line.endsWith(":")) {
195 // construct
196 me = SerialUtils.createObject(type);
197 } else {
198 // direct value
199 int pos = line.indexOf(":");
200 String encodedValue = line.substring(pos + 1);
201 me = SerialUtils.decode(encodedValue);
202 }
203 map.put(ref, me);
204 }
205 } else { // FIELD: new field *or* direct simple value
206 if (line.endsWith(":")) {
207 // field value is compound
208 currentFieldName = line.substring(0, line.length() - 1);
209 } else if (line.startsWith(":") || !line.contains(":")
210 || line.startsWith("\"")) {
211 // not a field value but a direct value
212 me = SerialUtils.decode(line);
213 } else {
214 // field value is direct
215 int pos = line.indexOf(":");
216 String fieldName = line.substring(0, pos);
217 String encodedValue = line.substring(pos + 1);
218 Object value = null;
219 value = SerialUtils.decode(encodedValue);
220
221 // To support simple types directly:
222 if (me == null) {
223 me = value;
224 } else {
225 setField(fieldName, value);
226 }
227 }
228 }
229
230 return false;
231 }
232
233 private void setField(String name, Object value)
234 throws NoSuchFieldException {
235
236 try {
237 Field field = me.getClass().getDeclaredField(name);
238
239 field.setAccessible(true);
240 field.set(me, value);
241 } catch (NoSuchFieldException e) {
242 throw new NoSuchFieldException(String.format(
243 "Field \"%s\" was not found in object of type \"%s\".",
244 name, me.getClass().getCanonicalName()));
245 } catch (Exception e) {
246 throw new NoSuchFieldException(String.format(
247 "Internal error when setting \"%s.%s\": %s", me.getClass()
248 .getCanonicalName(), name, e.getMessage()));
249 }
250 }
251
252 /**
253 * Return the current deserialised value.
254 *
255 * @return the current value
256 */
257 public Object getValue() {
258 return me;
259 }
260 }