Преглед на файлове

[S.R.Serialization] fix referencesource glitch in XmlMtomDictionaryWriter.

MTOM writer didn't expect multiple top-level elements are rejected at
ConformanceLevel.Auto, neiher do against Write[Start/End]Document() with
such elements.

Also Flush is often needed before directly writing to the Stream(Writer).

EOLs handling was also inconsistent between referencesource and old mono.
Atsushi Eno преди 11 години
родител
ревизия
a0dece2329

+ 18 - 5
mcs/class/System.Runtime.Serialization/System.Xml/XmlMtomDictionaryWriter.cs

@@ -44,6 +44,8 @@ namespace System.Xml
 			var settings = new XmlWriterSettings ();
 			settings.Encoding = encoding;
 			settings.OmitXmlDeclaration = true;
+			settings.ConformanceLevel = ConformanceLevel.Fragment;
+			settings.NewLineChars = "\r\n";
 			xml_writer_settings = settings;
 
 			// FIXME: actually it does not likely use ContentType.ToString() but writes those header items by own.
@@ -134,7 +136,8 @@ namespace System.Xml
 
 		public override void WriteEndDocument ()
 		{
-			w.WriteEndDocument ();
+			// We don't call w.WriteEndElement() because that causes state error
+			// (which is correct; MTOM writer just does not expect it).
 		}
 
 		public override void WriteEndElement ()
@@ -182,13 +185,15 @@ namespace System.Xml
 		public override void WriteStartDocument ()
 		{
 			CheckState ();
-			w.WriteStartDocument ();
+			// We don't call w.WriteStartDocument() because that causes state error
+			// (which is correct; MTOM writer just does not expect it).
 		}
 
 		public override void WriteStartDocument (bool standalone)
 		{
 			CheckState ();
-			w.WriteStartDocument (standalone);
+			// We don't call w.WriteStartDocument() because that causes state error
+			// (which is correct; MTOM writer just does not expect it).
 		}
 
 		public override void WriteStartElement (string prefix, string localName, string namespaceURI)
@@ -206,16 +211,21 @@ namespace System.Xml
 			get { return w.WriteState; }
 		}
 
+		static readonly char [] eol_chars = "\r\n".ToCharArray ();
+
 		public override void WriteString (string text)
 		{
 			CheckState ();
 
 			int i1, i2 = 0;
 			do {
-				i1 = text.IndexOf ('\r', i2);
+				i1 = text.IndexOfAny (eol_chars, i2);
 				if (i1 >= 0) {
 					w.WriteString (text.Substring (i2, i1 - i2));
-					WriteCharEntity ('\r');
+					if (text [i1] == '\r')
+						w.WriteCharEntity ('\r');
+					else
+						w.WriteRaw ("\r\n");
 					i2 = i1 + 1;
 				} else {
 					w.WriteString (text.Substring (i2));
@@ -248,12 +258,14 @@ namespace System.Xml
 		{
 			if (w == null && write_headers)
 				WriteMimeHeaders ();
+			
 			if (w == null || w.WriteState == WriteState.Closed || w.WriteState == WriteState.Error)
 				w = CreateWriter ();
 		}
 
 		void WriteMimeHeaders ()
 		{
+			w.Flush ();
 			writer.Write ("MIME-Version: 1.0\r\n");
 			writer.Write ("Content-Type: ");
 			writer.Write (content_type.ToString ());
@@ -293,6 +305,7 @@ namespace System.Xml
 			if (section_count > 1)
 				return;
 
+			w.Flush ();
 			writer.Write ("\r\n");
 			writer.Write ("--");
 			writer.Write (content_type.Boundary);

+ 1 - 0
mcs/class/System.Runtime.Serialization/Test/System.Xml/XmlMtomDictionaryWriterTest.cs

@@ -47,6 +47,7 @@ namespace MonoTests.System.Xml
 		{
 			MemoryStream ms = new MemoryStream ();
 			var w = XmlDictionaryWriter.CreateMtomWriter (ms, Encoding.UTF8, 10000, "sTaRt", "myboundary", "urn:foo", false, false);
+			w.WriteStartDocument ();
 			w.WriteStartElement ("root");
 			w.WriteRaw ("RAW");
 			w.WriteStartElement ("foo");