/
XmlWriter.cs
250 lines (239 loc) · 9.34 KB
/
XmlWriter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using Xemio.GameLibrary.Common;
using Xemio.GameLibrary.Math;
namespace Xemio.GameLibrary.Content.Formats.Xml
{
public class XmlWriter : IFormatWriter
{
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="XmlWriter"/> class.
/// </summary>
/// <param name="stream">The stream.</param>
public XmlWriter(Stream stream)
{
this.Stream = stream;
this._currentElement = new XElement("xml");
this._elements = new Stack<XElement>();
}
#endregion
#region Fields
private XElement _currentElement;
private readonly Stack<XElement> _elements;
#endregion
#region Implementation of IFormatWriter
/// <summary>
/// Gets the stream.
/// </summary>
public Stream Stream { get; private set; }
/// <summary>
/// Begins the specified section.
/// </summary>
/// <param name="tag">The tag.</param>
public IDisposable Section(string tag)
{
this._elements.Push(this._currentElement);
this._currentElement = new XElement(tag);
return new ActionDisposable(() =>
{
var element = this._currentElement;
this._currentElement = this._elements.Pop();
this._currentElement.Add(element);
});
}
/// <summary>
/// Writes the specified unsigned integer value.
/// </summary>
/// <param name="tag">The tag that is used for better readability in plain text formats.</param>
/// <param name="value">The value.</param>
public void WriteUnsignedInteger(string tag, uint value)
{
this._currentElement.Add(new XElement(tag, value));
}
/// <summary>
/// Writes the specified unsigned short value.
/// </summary>
/// <param name="tag">The tag that is used for better readability in plain text formats.</param>
/// <param name="value">The value.</param>
public void WriteUnsignedShort(string tag, ushort value)
{
this._currentElement.Add(new XElement(tag, value));
}
/// <summary>
/// Writes the specified unsigned long value.
/// </summary>
/// <param name="tag">The tag that is used for better readability in plain text formats.</param>
/// <param name="value">The value.</param>
public void WriteUnsignedLong(string tag, ulong value)
{
this._currentElement.Add(new XElement(tag, value));
}
/// <summary>
/// Writes the specified integer value.
/// </summary>
/// <param name="tag">The tag that is used for better readability in plain text formats.</param>
/// <param name="value">The value.</param>
public void WriteInteger(string tag, int value)
{
this._currentElement.Add(new XElement(tag, value));
}
/// <summary>
/// Writes the specified short value.
/// </summary>
/// <param name="tag">The tag that is used for better readability in plain text formats.</param>
/// <param name="value">The value.</param>
public void WriteShort(string tag, short value)
{
this._currentElement.Add(new XElement(tag, value));
}
/// <summary>
/// Writes the specified long value.
/// </summary>
/// <param name="tag">The tag that is used for better readability in plain text formats.</param>
/// <param name="value">The value.</param>
public void WriteLong(string tag, long value)
{
this._currentElement.Add(new XElement(tag, value));
}
/// <summary>
/// Writes the specified float value.
/// </summary>
/// <param name="tag">The tag that is used for better readability in plain text formats.</param>
/// <param name="value">The value.</param>
public void WriteFloat(string tag, float value)
{
this._currentElement.Add(new XElement(tag, value));
}
/// <summary>
/// Writes the specified double value.
/// </summary>
/// <param name="tag">The tag that is used for better readability in plain text formats.</param>
/// <param name="value">The value.</param>
public void WriteDouble(string tag, double value)
{
this._currentElement.Add(new XElement(tag, value));
}
/// <summary>
/// Writes the specified decimal value.
/// </summary>
/// <param name="tag">The tag that is used for better readability in plain text formats.</param>
/// <param name="value">The value.</param>
public void WriteDecimal(string tag, decimal value)
{
this._currentElement.Add(new XElement(tag, value));
}
/// <summary>
/// Writes the specified boolean value.
/// </summary>
/// <param name="tag">The tag that is used for better readability in plain text formats.</param>
/// <param name="value">The value.</param>
public void WriteBoolean(string tag, bool value)
{
this._currentElement.Add(new XElement(tag, value));
}
/// <summary>
/// Writes the specified byte value.
/// </summary>
/// <param name="tag">The tag that is used for better readability in plain text formats.</param>
/// <param name="value">The value.</param>
public void WriteByte(string tag, byte value)
{
this._currentElement.Add(new XElement(tag, value));
}
/// <summary>
/// Writes the specified byte array value.
/// </summary>
/// <param name="tag">The tag that is used for better readability in plain text formats.</param>
/// <param name="value">The value.</param>
public void WriteBytes(string tag, byte[] value)
{
this._currentElement.Add(new XElement(tag, string.Join(",", value)));
}
/// <summary>
/// Writes the specified character value.
/// </summary>
/// <param name="tag">The tag that is used for better readability in plain text formats.</param>
/// <param name="value">The value.</param>
public void WriteCharacter(string tag, char value)
{
this._currentElement.Add(new XElement(tag, value.ToString()));
}
/// <summary>
/// Writes the specified string value.
/// </summary>
/// <param name="tag">The tag that is used for better readability in plain text formats.</param>
/// <param name="value">The value.</param>
public void WriteString(string tag, string value)
{
this._currentElement.Add(new XElement(tag, value));
}
/// <summary>
/// Writes the specified vector value.
/// </summary>
/// <param name="tag">The tag that is used for better readability in plain text formats.</param>
/// <param name="value">The value.</param>
public void WriteVector2(string tag, Vector2 value)
{
using (this.Section(tag))
{
this.WriteFloat("X", value.X);
this.WriteFloat("Y", value.Y);
}
}
/// <summary>
/// Writes the specified rectangle value.
/// </summary>
/// <param name="tag">The tag that is used for better readability in plain text formats.</param>
/// <param name="value">The value.</param>
public void WriteRectangle(string tag, Rectangle value)
{
using (this.Section(tag))
{
this.WriteFloat("X", value.X);
this.WriteFloat("Y", value.Y);
this.WriteFloat("Width", value.Width);
this.WriteFloat("Height", value.Height);
}
}
#endregion
#region Methods
/// <summary>
/// Checks and throws an invalid operation exception.
/// </summary>
private void CheckAndThrowDisposeException()
{
if (this._elements.Count > 0)
{
var sectionBuilder = new StringBuilder();
sectionBuilder.Append("[");
while (this._elements.Count > 0)
{
sectionBuilder.Append(this._elements.Pop().Name.LocalName);
if (this._elements.Count > 1)
sectionBuilder.Append(", ");
}
sectionBuilder.Append("]");
throw new InvalidOperationException(
"There are unclosed sections, make sure you dispose all calls to writer.Section(tag). " +
"Remaining sections: " + sectionBuilder);
}
}
#endregion
#region Implementation of IDisposable
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
this.CheckAndThrowDisposeException();
this._currentElement.Save(this.Stream);
}
#endregion
}
}