Spring – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.IO; |
||
3 | using System.Text; |
||
4 | using System.Threading.Tasks; |
||
5 | using System.Xml; |
||
6 | using System.Xml.Schema; |
||
7 | using System.Xml.Serialization; |
||
8 | |||
9 | namespace Spring.Serialization.Combos |
||
10 | { |
||
11 | public static class ComboSerialization |
||
12 | { |
||
13 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
||
14 | |||
15 | private static XmlSerializer CombosSerializer { get; } = new XmlSerializer(typeof(SpringCombos.Combos)); |
||
16 | |||
17 | private static XmlReaderSettings XmlReaderSettings { get; } = new XmlReaderSettings |
||
18 | { |
||
19 | Async = true, |
||
20 | ValidationType = ValidationType.Schema, |
||
21 | IgnoreWhitespace = true, |
||
22 | ValidationFlags = |
||
23 | XmlSchemaValidationFlags |
||
24 | .ProcessInlineSchema | |
||
25 | XmlSchemaValidationFlags |
||
26 | .ProcessSchemaLocation | |
||
27 | XmlSchemaValidationFlags |
||
28 | .ReportValidationWarnings, |
||
29 | DtdProcessing = DtdProcessing.Parse |
||
30 | }; |
||
31 | |||
32 | private static XmlWriterSettings XmlWriterSettings { get; } = new XmlWriterSettings |
||
33 | { |
||
34 | Async = true, |
||
35 | Indent = true, |
||
36 | IndentChars = " ", |
||
37 | OmitXmlDeclaration = false |
||
38 | }; |
||
39 | |||
40 | #endregion |
||
41 | |||
42 | #region Event Handlers |
||
43 | |||
44 | private static void XmlReaderSettings_ValidationEventHandler(object sender, ValidationEventArgs e) |
||
45 | { |
||
46 | //TODO: do something... |
||
47 | switch (e.Severity) |
||
48 | { |
||
49 | case XmlSeverityType.Error: |
||
50 | /*this.Execute(() => |
||
51 | { |
||
52 | toolStripStatusLabel1.Text = $"{Strings.Validation_error_reading_combos}: {e.Message}"; |
||
53 | }); |
||
54 | */ |
||
55 | break; |
||
56 | |||
57 | case XmlSeverityType.Warning: |
||
58 | /*this.Execute(() => |
||
59 | { |
||
60 | toolStripStatusLabel1.Text = |
||
61 | $"{Strings.Validation_warning_reading_combos}: {e.Message}"; |
||
62 | }); |
||
63 | */ |
||
64 | break; |
||
65 | } |
||
66 | } |
||
67 | |||
68 | #endregion |
||
69 | |||
70 | #region Public Methods |
||
71 | |||
72 | /// <summary> |
||
73 | /// Deserialize text to Spring combos. |
||
74 | /// </summary> |
||
75 | /// <param name="text">the text to deserialize</param> |
||
76 | /// <returns>a deserialization success or fail object</returns> |
||
77 | public static async Task<SerializationState> Deserialize(string text) |
||
78 | { |
||
79 | XmlReaderSettings.ValidationEventHandler += XmlReaderSettings_ValidationEventHandler; |
||
80 | |||
81 | try |
||
82 | { |
||
83 | using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(text))) |
||
84 | { |
||
85 | using (var xmlReader = XmlReader.Create(memoryStream, XmlReaderSettings)) |
||
86 | { |
||
87 | var stringBuilder = new StringBuilder(); |
||
88 | |||
89 | using (var stringWriter = new StringWriter(stringBuilder)) |
||
90 | { |
||
91 | while (await xmlReader.ReadAsync()) |
||
92 | { |
||
93 | await stringWriter.WriteAsync(await xmlReader.ReadOuterXmlAsync()); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | using (var stringReader = new StringReader(stringBuilder.ToString())) |
||
98 | { |
||
99 | var combos = (SpringCombos.Combos) CombosSerializer.Deserialize(stringReader); |
||
100 | |||
101 | using (var writeMemoryStream = new MemoryStream()) |
||
102 | { |
||
103 | using (var xmlWriter = |
||
104 | XmlWriter.Create(writeMemoryStream, XmlWriterSettings)) |
||
105 | { |
||
106 | await xmlWriter.WriteDocTypeAsync("SpringCombos", |
||
107 | null, |
||
108 | null, |
||
109 | "<!ATTLIST SpringCombos xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>"); |
||
110 | |||
111 | CombosSerializer.Serialize(xmlWriter, combos); |
||
112 | |||
113 | writeMemoryStream.Position = 0L; |
||
114 | |||
115 | using (var readMemoryStream = new MemoryStream()) |
||
116 | { |
||
117 | await writeMemoryStream.CopyToAsync(readMemoryStream); |
||
118 | |||
119 | readMemoryStream.Position = 0L; |
||
120 | |||
121 | var contents = Encoding.UTF8.GetString(readMemoryStream.ToArray()); |
||
122 | |||
123 | return new SerializationSuccess<SpringCombos.Combos> |
||
124 | { |
||
125 | Result = combos, |
||
126 | Text = contents |
||
127 | }; |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | catch (Exception ex) |
||
136 | { |
||
137 | return new SerializationFailure { Exception = ex }; |
||
138 | } |
||
139 | finally |
||
140 | { |
||
141 | XmlReaderSettings.ValidationEventHandler -= XmlReaderSettings_ValidationEventHandler; |
||
142 | } |
||
143 | } |
||
144 | |||
145 | public static async Task<SerializationState> Deserialize(FileInfo fileInfo, int index) |
||
146 | { |
||
147 | var result = await Deserialize(fileInfo); |
||
148 | |||
149 | switch (result) |
||
150 | { |
||
151 | case SerializationSuccess<SpringCombos.Combos> serializationSuccess: |
||
152 | foreach (var combo in serializationSuccess.Result.Combo) |
||
153 | { |
||
154 | combo.Index = index++; |
||
155 | } |
||
156 | |||
157 | break; |
||
158 | } |
||
159 | |||
160 | return result; |
||
161 | } |
||
162 | |||
163 | /// <summary> |
||
164 | /// Serialize Spring combos. |
||
165 | /// </summary> |
||
166 | /// <param name="combos">the spring combos to serialize</param> |
||
167 | /// <returns>a serialization success or fail object</returns> |
||
168 | public static async Task<SerializationState> Serialize(this SpringCombos.Combos combos) |
||
169 | { |
||
170 | try |
||
171 | { |
||
172 | using (var writeMemoryStream = new MemoryStream()) |
||
173 | { |
||
174 | using (var xmlWriter = |
||
175 | XmlWriter.Create(writeMemoryStream, XmlWriterSettings)) |
||
176 | { |
||
177 | await xmlWriter.WriteDocTypeAsync("SpringCombos", |
||
178 | null, |
||
179 | null, |
||
180 | "<!ATTLIST SpringCombos xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>"); |
||
181 | |||
182 | CombosSerializer.Serialize(xmlWriter, combos); |
||
183 | |||
184 | writeMemoryStream.Position = 0L; |
||
185 | |||
186 | using (var readMemoryStream = new MemoryStream()) |
||
187 | { |
||
188 | await writeMemoryStream.CopyToAsync(readMemoryStream); |
||
189 | |||
190 | readMemoryStream.Position = 0L; |
||
191 | |||
192 | var contents = Encoding.UTF8.GetString(readMemoryStream.ToArray()); |
||
193 | |||
194 | return new SerializationSuccess<SpringCombos.Combos> |
||
195 | { Result = combos, Text = contents }; |
||
196 | } |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | catch (Exception ex) |
||
201 | { |
||
202 | return new SerializationFailure { Exception = ex }; |
||
203 | } |
||
204 | } |
||
205 | |||
206 | /// <summary> |
||
207 | /// Serialize Spring combos to a file. |
||
208 | /// </summary> |
||
209 | /// <param name="combos">the combos to serialize</param> |
||
210 | /// <param name="fileName">path to a file</param> |
||
211 | /// <returns>a serialization success or fail object</returns> |
||
212 | public static async Task<SerializationState> Serialize(this SpringCombos.Combos combos, string fileName) |
||
213 | { |
||
214 | try |
||
215 | { |
||
216 | using (var writeMemoryStream = new MemoryStream()) |
||
217 | { |
||
218 | using (var xmlWriter = |
||
219 | XmlWriter.Create(writeMemoryStream, XmlWriterSettings)) |
||
220 | { |
||
221 | await xmlWriter.WriteDocTypeAsync("SpringCombos", |
||
222 | null, |
||
223 | null, |
||
224 | "<!ATTLIST SpringCombos xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>"); |
||
225 | |||
226 | CombosSerializer.Serialize(xmlWriter, combos); |
||
227 | |||
228 | using (var fileStream = new FileStream(fileName, FileMode.Create)) |
||
229 | { |
||
230 | writeMemoryStream.Position = 0L; |
||
231 | |||
232 | await writeMemoryStream.CopyToAsync(fileStream); |
||
233 | |||
234 | var contents = Encoding.UTF8.GetString(writeMemoryStream.ToArray()); |
||
235 | |||
236 | return new SerializationSuccess<SpringCombos.Combos> |
||
237 | { |
||
238 | Result = combos, |
||
239 | Text = contents |
||
240 | }; |
||
241 | } |
||
242 | } |
||
243 | } |
||
244 | } |
||
245 | catch (Exception ex) |
||
246 | { |
||
247 | return new SerializationFailure { Exception = ex }; |
||
248 | } |
||
249 | } |
||
250 | |||
251 | #endregion |
||
252 | |||
253 | #region Private Methods |
||
254 | |||
255 | /// <summary> |
||
256 | /// Deserialize a file to Spring combos. |
||
257 | /// </summary> |
||
258 | /// <param name="fileInfo">the file info</param> |
||
259 | /// <returns>a deserialization success or fail object</returns> |
||
260 | private static async Task<SerializationState> Deserialize(FileSystemInfo fileInfo) |
||
261 | { |
||
262 | XmlReaderSettings.ValidationEventHandler += XmlReaderSettings_ValidationEventHandler; |
||
263 | |||
264 | try |
||
265 | { |
||
266 | using (var fileStream = new FileStream(fileInfo.FullName, FileMode.Open)) |
||
267 | { |
||
268 | using (var xmlReader = XmlReader.Create(fileStream, XmlReaderSettings)) |
||
269 | { |
||
270 | var stringBuilder = new StringBuilder(); |
||
271 | |||
272 | using (var stringWriter = new StringWriter(stringBuilder)) |
||
273 | { |
||
274 | while (await xmlReader.ReadAsync()) |
||
275 | { |
||
276 | await stringWriter.WriteAsync(await xmlReader.ReadOuterXmlAsync()); |
||
277 | } |
||
278 | } |
||
279 | |||
280 | using (var stringReader = new StringReader(stringBuilder.ToString())) |
||
281 | { |
||
282 | var combos = (SpringCombos.Combos) CombosSerializer.Deserialize(stringReader) ?? |
||
283 | new SpringCombos.Combos(); |
||
284 | |||
285 | using (var writeMemoryStream = new MemoryStream()) |
||
286 | { |
||
287 | using (var xmlWriter = |
||
288 | XmlWriter.Create(writeMemoryStream, XmlWriterSettings)) |
||
289 | { |
||
290 | await xmlWriter.WriteDocTypeAsync("SpringCombos", |
||
291 | null, |
||
292 | null, |
||
293 | "<!ATTLIST SpringCombos xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>"); |
||
294 | |||
295 | CombosSerializer.Serialize(xmlWriter, combos); |
||
296 | |||
297 | writeMemoryStream.Position = 0L; |
||
298 | |||
299 | using (var readMemoryStream = new MemoryStream()) |
||
300 | { |
||
301 | await writeMemoryStream.CopyToAsync(readMemoryStream); |
||
302 | |||
303 | readMemoryStream.Position = 0L; |
||
304 | |||
305 | var contents = Encoding.UTF8.GetString(readMemoryStream.ToArray()); |
||
306 | |||
307 | return new SerializationSuccess<SpringCombos.Combos> |
||
308 | { |
||
309 | Result = combos, |
||
310 | Text = contents |
||
311 | }; |
||
312 | } |
||
313 | } |
||
314 | } |
||
315 | } |
||
316 | } |
||
317 | } |
||
318 | } |
||
319 | catch (Exception ex) |
||
320 | { |
||
321 | return new SerializationFailure { Exception = ex }; |
||
322 | } |
||
323 | finally |
||
324 | { |
||
325 | XmlReaderSettings.ValidationEventHandler -= XmlReaderSettings_ValidationEventHandler; |
||
326 | } |
||
327 | } |
||
328 | |||
329 | #endregion |
||
330 | } |
||
331 | } |