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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Xml;
using Google.ProtocolBuffers.Descriptors;
namespace Google.ProtocolBuffers.Serialization
{
/// <summary>
/// Writes a proto buffer to an XML document or fragment. .NET 3.5 users may also
/// use this class to produce Json by setting the options to support Json and providing
/// an XmlWriter obtained from <see cref="System.Runtime.Serialization.Json.JsonReaderWriterFactory"/>.
/// </summary>
public class XmlFormatWriter : AbstractTextWriter
{
private static readonly Encoding DefaultEncoding = new UTF8Encoding(false);
public const string DefaultRootElementName = "root";
private const int NestedArrayFlag = 0x0001;
private readonly XmlWriter _output;
private string _rootElementName;
private int _messageOpenCount;
private static XmlWriterSettings DefaultSettings(Encoding encoding)
{
return new XmlWriterSettings()
{
CheckCharacters = false,
NewLineHandling = NewLineHandling.Entitize,
OmitXmlDeclaration = true,
Encoding = encoding,
};
}
/// <summary>
/// Constructs the XmlFormatWriter to write to the given TextWriter
/// </summary>
public static XmlFormatWriter CreateInstance(TextWriter output)
{
return new XmlFormatWriter(XmlWriter.Create(output, DefaultSettings(output.Encoding)));
}
/// <summary>
/// Constructs the XmlFormatWriter to write to the given stream
/// </summary>
public static XmlFormatWriter CreateInstance(Stream output)
{
return new XmlFormatWriter(XmlWriter.Create(output, DefaultSettings(DefaultEncoding)));
}
/// <summary>
/// Constructs the XmlFormatWriter to write to the given stream
/// </summary>
public static XmlFormatWriter CreateInstance(Stream output, Encoding encoding)
{
return new XmlFormatWriter(XmlWriter.Create(output, DefaultSettings(encoding)));
}
/// <summary>
/// Constructs the XmlFormatWriter to write to the given XmlWriter
/// </summary>
public static XmlFormatWriter CreateInstance(XmlWriter output)
{
return new XmlFormatWriter(output);
}
protected XmlFormatWriter(XmlWriter output)
{
_output = output;
_messageOpenCount = 0;
_rootElementName = DefaultRootElementName;
}
/// <summary>
/// Closes the underlying XmlTextWriter
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
while(_messageOpenCount > 0)
WriteMessageEnd();
_output.Close();
}
base.Dispose(disposing);
}
/// <summary>
/// Gets or sets the default element name to use when using the Merge<TBuilder>()
/// </summary>
public string RootElementName
{
get { return _rootElementName; }
set
{
ThrowHelper.ThrowIfNull(value, "RootElementName");
_rootElementName = value;
}
}
/// <summary>
/// Gets or sets the options to use while generating the XML
/// </summary>
public XmlWriterOptions Options { get; set; }
/// <summary>
/// Sets the options to use while generating the XML
/// </summary>
public XmlFormatWriter SetOptions(XmlWriterOptions options)
{
Options = options;
return this;
}
private bool TestOption(XmlWriterOptions option)
{
return (Options & option) != 0;
}
/// <summary>
/// Completes any pending write operations
/// </summary>
public override void Flush()
{
_output.Flush();
base.Flush();
}
/// <summary>
/// Used to write the root-message preamble, in xml this is open element for RootElementName,
/// by default "<root>". After this call you can call IMessageLite.MergeTo(...) and
/// complete the message with a call to WriteMessageEnd().
/// </summary>
public override void WriteMessageStart()
{
StartMessage(_rootElementName);
}
/// <summary>
/// Used to write the root-message preamble, in xml this is open element for elementName.
/// After this call you can call IMessageLite.MergeTo(...) and complete the message with
/// a call to WriteMessageEnd().
/// </summary>
public void StartMessage(string elementName)
{
if (TestOption(XmlWriterOptions.OutputJsonTypes))
{
_output.WriteStartElement("root"); // json requires this is the root-element
_output.WriteAttributeString("type", "object");
}
else
{
_output.WriteStartElement(elementName);
}
_messageOpenCount++;
}
/// <summary>
/// Used to complete a root-message previously started with a call to WriteMessageStart()
/// </summary>
public override void WriteMessageEnd()
{
if (_messageOpenCount <= 0)
throw new InvalidOperationException();
_output.WriteEndElement();
_output.Flush();
_messageOpenCount--;
}
/// <summary>
/// Writes a message as an element using the name defined in <see cref="RootElementName"/>
/// </summary>
public override void WriteMessage(IMessageLite message)
{
WriteMessage(_rootElementName, message);
}
/// <summary>
/// Writes a message as an element with the given name
/// </summary>
public void WriteMessage(string elementName, IMessageLite message)
{
StartMessage(elementName);
message.WriteTo(this);
WriteMessageEnd();
}
/// <summary>
/// Writes a message
/// </summary>
protected override void WriteMessageOrGroup(string field, IMessageLite message)
{
_output.WriteStartElement(field);
if (TestOption(XmlWriterOptions.OutputJsonTypes))
{
_output.WriteAttributeString("type", "object");
}
message.WriteTo(this);
_output.WriteEndElement();
}
/// <summary>
/// Writes a String value
/// </summary>
protected override void WriteAsText(string field, string textValue, object typedValue)
{
_output.WriteStartElement(field);
if (TestOption(XmlWriterOptions.OutputJsonTypes))
{
if (typedValue is int || typedValue is uint || typedValue is long || typedValue is ulong ||
typedValue is double || typedValue is float)
{
_output.WriteAttributeString("type", "number");
}
else if (typedValue is bool)
{
_output.WriteAttributeString("type", "boolean");
}
}
_output.WriteString(textValue);
//Empty strings should not be written as empty elements '<item/>', rather as '<item></item>'
if (_output.WriteState == WriteState.Element)
{
_output.WriteRaw("");
}
_output.WriteEndElement();
}
/// <summary>
/// Writes an array of field values
/// </summary>
protected override void WriteArray(FieldType fieldType, string field, IEnumerable items)
{
//see if it's empty
IEnumerator eitems = items.GetEnumerator();
try
{
if (!eitems.MoveNext())
{
return;
}
}
finally
{
if (eitems is IDisposable)
{
((IDisposable) eitems).Dispose();
}
}
if (TestOption(XmlWriterOptions.OutputNestedArrays | XmlWriterOptions.OutputJsonTypes))
{
_output.WriteStartElement(field);
if (TestOption(XmlWriterOptions.OutputJsonTypes))
{
_output.WriteAttributeString("type", "array");
}
base.WriteArray(fieldType, "item", items);
_output.WriteEndElement();
}
else
{
base.WriteArray(fieldType, field, items);
}
}
/// <summary>
/// Writes a System.Enum by the numeric and textual value
/// </summary>
protected override void WriteEnum(string field, int number, string name)
{
_output.WriteStartElement(field);
if (!TestOption(XmlWriterOptions.OutputJsonTypes) && TestOption(XmlWriterOptions.OutputEnumValues))
{
_output.WriteAttributeString("value", XmlConvert.ToString(number));
}
_output.WriteString(name);
_output.WriteEndElement();
}
}
}
|