Commit | Line | Data |
---|---|---|
db31c358 NR |
1 | package be.nikiroo.utils.serial; |
2 | ||
8c8da42a | 3 | import java.io.IOException; |
dc41a952 | 4 | import java.io.InputStream; |
8c8da42a | 5 | import java.lang.reflect.Field; |
db31c358 NR |
6 | import java.util.HashMap; |
7 | import java.util.Map; | |
db31c358 | 8 | |
dc41a952 | 9 | import be.nikiroo.utils.IOUtils; |
3b4319db | 10 | import be.nikiroo.utils.StringUtils; |
52fb9a56 NR |
11 | import be.nikiroo.utils.streams.NextableInputStream; |
12 | import be.nikiroo.utils.streams.NextableInputStreamStep; | |
db31c358 NR |
13 | |
14 | /** | |
15 | * A simple class that can accept the output of {@link Exporter} to recreate | |
16 | * objects as they were sent to said exporter. | |
17 | * <p> | |
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). | |
21 | * | |
22 | * @author niki | |
23 | */ | |
24 | public class Importer { | |
25 | private Boolean link; | |
26 | private Object me; | |
27 | private Importer child; | |
28 | private Map<String, Object> map; | |
29 | ||
30 | private String currentFieldName; | |
31 | ||
f157aed8 NR |
32 | /** |
33 | * Create a new {@link Importer}. | |
34 | */ | |
db31c358 NR |
35 | public Importer() { |
36 | map = new HashMap<String, Object>(); | |
37 | map.put("NULL", null); | |
38 | } | |
39 | ||
40 | private Importer(Map<String, Object> map) { | |
41 | this.map = map; | |
42 | } | |
43 | ||
8c8da42a NR |
44 | /** |
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. | |
48 | * | |
3b4319db | 49 | * @param in |
8c8da42a NR |
50 | * the data to parse |
51 | * | |
52 | * @return itself so it can be chained | |
53 | * | |
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 | |
452f38c8 NR |
62 | * @throws IOException |
63 | * if the content cannot be read (for instance, corrupt data) | |
dc41a952 NR |
64 | * @throws NullPointerException |
65 | * if the stream is empty | |
8c8da42a | 66 | */ |
dc41a952 NR |
67 | public Importer read(InputStream in) throws NoSuchFieldException, |
68 | NoSuchMethodException, ClassNotFoundException, IOException, | |
69 | NullPointerException { | |
70 | ||
dc41a952 NR |
71 | NextableInputStream stream = new NextableInputStream(in, |
72 | new NextableInputStreamStep('\n')); | |
73 | ||
564bbbdb | 74 | try { |
46d2b6a4 NR |
75 | if (in == null) { |
76 | throw new NullPointerException("InputStream is null"); | |
77 | } | |
b6200792 | 78 | |
23cf894d | 79 | boolean first = true; |
564bbbdb | 80 | while (stream.next()) { |
23cf894d NR |
81 | if (stream.eof()) { |
82 | if (first) { | |
83 | throw new NullPointerException( | |
84 | "InputStream empty, normal termination"); | |
85 | } | |
86 | return this; | |
87 | } | |
88 | first = false; | |
89 | ||
564bbbdb NR |
90 | boolean zip = stream.startsWiths("ZIP:"); |
91 | boolean b64 = stream.startsWiths("B64:"); | |
92 | ||
93 | if (zip || b64) { | |
4d319565 | 94 | stream.skip("XXX:".length()); |
564bbbdb NR |
95 | InputStream decoded = StringUtils.unbase64(stream.open(), |
96 | zip); | |
97 | try { | |
98 | read(decoded); | |
99 | } finally { | |
100 | decoded.close(); | |
101 | } | |
102 | } else { | |
103 | processLine(stream); | |
8c8da42a | 104 | } |
db31c358 | 105 | } |
564bbbdb NR |
106 | } finally { |
107 | stream.close(false); | |
db31c358 | 108 | } |
8c8da42a | 109 | |
db31c358 NR |
110 | return this; |
111 | } | |
112 | ||
8c8da42a NR |
113 | /** |
114 | * Read a single (whole) line of serialised data into this {@link Importer} | |
115 | * and accumulate it with the already present data. | |
116 | * | |
3b4319db | 117 | * @param in |
8c8da42a NR |
118 | * the line to parse |
119 | * | |
120 | * @return TRUE if we are just done with one object or sub-object | |
121 | * | |
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 | |
452f38c8 NR |
130 | * @throws IOException |
131 | * if the content cannot be read (for instance, corrupt data) | |
8c8da42a | 132 | */ |
dc41a952 | 133 | private boolean processLine(InputStream in) throws NoSuchFieldException, |
452f38c8 | 134 | NoSuchMethodException, ClassNotFoundException, IOException { |
dc41a952 | 135 | |
db31c358 NR |
136 | // Defer to latest child if any |
137 | if (child != null) { | |
dc41a952 | 138 | if (child.processLine(in)) { |
db31c358 NR |
139 | if (currentFieldName != null) { |
140 | setField(currentFieldName, child.getValue()); | |
141 | currentFieldName = null; | |
142 | } | |
143 | child = null; | |
144 | } | |
145 | ||
146 | return false; | |
147 | } | |
148 | ||
dc41a952 NR |
149 | // TODO use the stream, Luke |
150 | String line = IOUtils.readSmallStream(in); | |
151 | ||
23cf894d NR |
152 | if (line.isEmpty()) { |
153 | return false; | |
154 | } | |
155 | ||
db31c358 NR |
156 | if (line.equals("{")) { // START: new child if needed |
157 | if (link != null) { | |
158 | child = new Importer(map); | |
159 | } | |
160 | } else if (line.equals("}")) { // STOP: report self to parent | |
161 | return true; | |
162 | } else if (line.startsWith("REF ")) { // REF: create/link self | |
ce0974c4 NR |
163 | String[] tab = line.substring("REF ".length()).split("@"); |
164 | String type = tab[0]; | |
165 | tab = tab[1].split(":"); | |
166 | String ref = tab[0]; | |
167 | ||
db31c358 NR |
168 | link = map.containsKey(ref); |
169 | if (link) { | |
170 | me = map.get(ref); | |
171 | } else { | |
ce0974c4 NR |
172 | if (line.endsWith(":")) { |
173 | // construct | |
23cf894d NR |
174 | try { |
175 | me = SerialUtils.createObject(type); | |
176 | } catch (NoSuchMethodException e) { | |
177 | System.out.println("LINE: <" + line + ">"); | |
178 | throw e; | |
179 | } | |
ce0974c4 NR |
180 | } else { |
181 | // direct value | |
182 | int pos = line.indexOf(":"); | |
183 | String encodedValue = line.substring(pos + 1); | |
184 | me = SerialUtils.decode(encodedValue); | |
185 | } | |
db31c358 NR |
186 | map.put(ref, me); |
187 | } | |
ce0974c4 | 188 | } else { // FIELD: new field *or* direct simple value |
db31c358 NR |
189 | if (line.endsWith(":")) { |
190 | // field value is compound | |
191 | currentFieldName = line.substring(0, line.length() - 1); | |
f4053377 NR |
192 | } else if (line.startsWith(":") || !line.contains(":") |
193 | || line.startsWith("\"") || CustomSerializer.isCustom(line)) { | |
ce0974c4 NR |
194 | // not a field value but a direct value |
195 | me = SerialUtils.decode(line); | |
db31c358 NR |
196 | } else { |
197 | // field value is direct | |
198 | int pos = line.indexOf(":"); | |
199 | String fieldName = line.substring(0, pos); | |
200 | String encodedValue = line.substring(pos + 1); | |
201 | Object value = null; | |
202 | value = SerialUtils.decode(encodedValue); | |
203 | ||
204 | // To support simple types directly: | |
205 | if (me == null) { | |
206 | me = value; | |
207 | } else { | |
208 | setField(fieldName, value); | |
209 | } | |
210 | } | |
211 | } | |
212 | ||
213 | return false; | |
214 | } | |
215 | ||
db31c358 | 216 | private void setField(String name, Object value) |
8c8da42a | 217 | throws NoSuchFieldException { |
db31c358 NR |
218 | |
219 | try { | |
220 | Field field = me.getClass().getDeclaredField(name); | |
221 | ||
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())); | |
8c8da42a NR |
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())); | |
db31c358 NR |
232 | } |
233 | } | |
234 | ||
8c8da42a NR |
235 | /** |
236 | * Return the current deserialised value. | |
237 | * | |
238 | * @return the current value | |
239 | */ | |
db31c358 NR |
240 | public Object getValue() { |
241 | return me; | |
242 | } | |
243 | } |