OLD | NEW |
1 part of serialization; | 1 part of serialization; |
2 | 2 |
3 /** | 3 /** |
4 * An abstract class for serialization formats. Subclasses define how data | 4 * An abstract class for serialization formats. Subclasses define how data |
5 * is read or written to a particular output mechanism. | 5 * is read or written to a particular output mechanism. |
6 */ | 6 */ |
7 abstract class Format { | 7 abstract class Format { |
8 /** | 8 /** |
9 * Return true if this format stores primitives in their own area and uses | 9 * Return true if this format stores primitives in their own area and uses |
10 * references to them (e.g. [SimpleFlatFormat]) and false if primitives | 10 * references to them (e.g. [SimpleFlatFormat]) and false if primitives |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 if (reader.selfDescribing && !(data.containsKey(DATA))) { | 180 if (reader.selfDescribing && !(data.containsKey(DATA))) { |
181 throw new SerializationException("Missing $DATA entry, " | 181 throw new SerializationException("Missing $DATA entry, " |
182 "may mean this was written and read with different values " | 182 "may mean this was written and read with different values " |
183 "of selfDescribing."); | 183 "of selfDescribing."); |
184 } | 184 } |
185 // If we are self-describing, we should have separate rule and data | 185 // If we are self-describing, we should have separate rule and data |
186 // sections. If not, we assume that we have just the data at the top level. | 186 // sections. If not, we assume that we have just the data at the top level. |
187 var rules = reader.selfDescribing ? data[RULES] : null; | 187 var rules = reader.selfDescribing ? data[RULES] : null; |
188 var actualData = reader.selfDescribing ? data[DATA] : data; | 188 var actualData = reader.selfDescribing ? data[DATA] : data; |
189 reader.readRules(rules); | 189 reader.readRules(rules); |
190 var ruleData = new List(reader.rules.length).map((x) => []).toList(); | 190 var ruleData = new List.generate(reader.rules.length, (_) => []); |
191 var top = recursivelyFixUp(actualData, reader, ruleData); | 191 var top = recursivelyFixUp(actualData, reader, ruleData); |
192 result["data"] = ruleData; | 192 result["data"] = ruleData; |
193 result["roots"] = [top]; | 193 result["roots"] = [top]; |
194 return result; | 194 return result; |
195 } | 195 } |
196 | 196 |
197 /** | 197 /** |
198 * Convert nested references in [data] into [Reference] objects. | 198 * Convert nested references in [data] into [Reference] objects. |
199 */ | 199 */ |
200 recursivelyFixUp(input, Reader r, List result) { | 200 recursivelyFixUp(input, Reader r, List result) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 List generateOutput(Writer w) { | 250 List generateOutput(Writer w) { |
251 var result = new List(3); | 251 var result = new List(3); |
252 var flatData = []; | 252 var flatData = []; |
253 for (var eachRule in w.rules) { | 253 for (var eachRule in w.rules) { |
254 var ruleData = w.states[eachRule.number]; | 254 var ruleData = w.states[eachRule.number]; |
255 flatData.add(ruleData.length); | 255 flatData.add(ruleData.length); |
256 writeStateInto(eachRule, ruleData, flatData); | 256 writeStateInto(eachRule, ruleData, flatData); |
257 } | 257 } |
258 result[0] = w.serializedRules(); | 258 result[0] = w.serializedRules(); |
259 result[1] = flatData; | 259 result[1] = flatData; |
260 result[2] = new List(); | 260 result[2] = []; |
261 w._rootReferences().forEach((x) => x.writeToList(result[2])); | 261 w._rootReferences().forEach((x) => x.writeToList(result[2])); |
262 return result; | 262 return result; |
263 } | 263 } |
264 | 264 |
265 /** | 265 /** |
266 * Writes the data from [rule] into the [target] list. | 266 * Writes the data from [rule] into the [target] list. |
267 */ | 267 */ |
268 void writeStateInto(SerializationRule rule, List ruleData, List target) { | 268 void writeStateInto(SerializationRule rule, List ruleData, List target) { |
269 if (!ruleData.isEmpty) { | 269 if (!ruleData.isEmpty) { |
270 var sample = ruleData.first; | 270 var sample = ruleData.first; |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 return new Reference(r, a, b); | 462 return new Reference(r, a, b); |
463 } | 463 } |
464 } | 464 } |
465 | 465 |
466 /** Return the next element from the input. */ | 466 /** Return the next element from the input. */ |
467 _next(Iterator input) { | 467 _next(Iterator input) { |
468 input.moveNext(); | 468 input.moveNext(); |
469 return input.current; | 469 return input.current; |
470 } | 470 } |
471 } | 471 } |
OLD | NEW |