aboutsummaryrefslogtreecommitdiff
path: root/src/ProtocolBuffers.Serialization/XmlFormatWriter.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/ProtocolBuffers.Serialization/XmlFormatWriter.cs')
-rw-r--r--src/ProtocolBuffers.Serialization/XmlFormatWriter.cs77
1 files changed, 57 insertions, 20 deletions
diff --git a/src/ProtocolBuffers.Serialization/XmlFormatWriter.cs b/src/ProtocolBuffers.Serialization/XmlFormatWriter.cs
index fd36c1de..4bd27562 100644
--- a/src/ProtocolBuffers.Serialization/XmlFormatWriter.cs
+++ b/src/ProtocolBuffers.Serialization/XmlFormatWriter.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Collections;
using System.IO;
using System.Text;
@@ -14,10 +14,14 @@ namespace Google.ProtocolBuffers.Serialization
/// </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;
+ // The default element name used for WriteMessageStart
private string _rootElementName;
+ // Used to assert matching WriteMessageStart/WriteMessageEnd calls
+ private int _messageOpenCount;
private static XmlWriterSettings DefaultSettings(Encoding encoding)
{
@@ -43,7 +47,7 @@ namespace Google.ProtocolBuffers.Serialization
/// </summary>
public static XmlFormatWriter CreateInstance(Stream output)
{
- return new XmlFormatWriter(XmlWriter.Create(output, DefaultSettings(Encoding.UTF8)));
+ return new XmlFormatWriter(XmlWriter.Create(output, DefaultSettings(DefaultEncoding)));
}
/// <summary>
@@ -65,21 +69,11 @@ namespace Google.ProtocolBuffers.Serialization
protected XmlFormatWriter(XmlWriter output)
{
_output = output;
+ _messageOpenCount = 0;
_rootElementName = DefaultRootElementName;
}
/// <summary>
- /// Closes the underlying XmlTextWriter
- /// </summary>
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- _output.Close();
- }
- }
-
- /// <summary>
/// Gets or sets the default element name to use when using the Merge&lt;TBuilder>()
/// </summary>
public string RootElementName
@@ -112,17 +106,30 @@ namespace Google.ProtocolBuffers.Serialization
}
/// <summary>
- /// Writes a message as an element using the name defined in <see cref="RootElementName"/>
+ /// Completes any pending write operations
/// </summary>
- public override void WriteMessage(IMessageLite message)
+ public override void Flush()
{
- WriteMessage(_rootElementName, message);
+ _output.Flush();
+ base.Flush();
}
/// <summary>
- /// Writes a message as an element with the given name
+ /// Used to write the root-message preamble, in xml this is open element for RootElementName,
+ /// by default "&lt;root&gt;". After this call you can call IMessageLite.MergeTo(...) and
+ /// complete the message with a call to WriteMessageEnd().
/// </summary>
- public void WriteMessage(string elementName, IMessageLite message)
+ public override void WriteMessageStart()
+ {
+ WriteMessageStart(_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 WriteMessageStart(string elementName)
{
if (TestOption(XmlWriterOptions.OutputJsonTypes))
{
@@ -133,10 +140,40 @@ namespace Google.ProtocolBuffers.Serialization
{
_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();
+ }
- message.WriteTo(this);
_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)
+ {
+ WriteMessageStart(elementName);
+ message.WriteTo(this);
+ WriteMessageEnd();
}
/// <summary>