2006-11-20 Atsushi Enomoto <atsushi@ximian.com>
authorAtsushi Eno <atsushieno@gmail.com>
Mon, 20 Nov 2006 09:39:58 +0000 (09:39 -0000)
committerAtsushi Eno <atsushieno@gmail.com>
Mon, 20 Nov 2006 09:39:58 +0000 (09:39 -0000)
* XmlTextWriter2.cs : fixed some relationship between
  ConformanceLevel, OmitXmlDeclaration and WriteStartDocument().
  See new tests more details.
* XmlWriter.cs : it could reuse XmlTextWriter.ctor(TextWriter,
  XmlWriterSettings). It also resulted in dropping several internal
  members in XmlTextWriter.

* XmlWriterSettingsTests.cs : added tests for relationship between
  ConformanceLevel, OmitXmlDeclaration and WriteStartDocument().

svn path=/trunk/mcs/; revision=68180

mcs/class/System.XML/System.Xml/ChangeLog
mcs/class/System.XML/System.Xml/XmlTextWriter2.cs
mcs/class/System.XML/System.Xml/XmlWriter.cs
mcs/class/System.XML/Test/System.Xml/ChangeLog
mcs/class/System.XML/Test/System.Xml/XmlWriterSettingsTests.cs

index c89e1ec2ae57c6124de50324be0fdceea42bf68a..dc487fe79abf6113ebae24049c7f4df6e1899874 100644 (file)
@@ -1,3 +1,12 @@
+2006-11-20  Atsushi Enomoto <atsushi@ximian.com>
+
+       * XmlTextWriter2.cs : fixed some relationship between
+         ConformanceLevel, OmitXmlDeclaration and WriteStartDocument().
+         See new tests more details.
+       * XmlWriter.cs : it could reuse XmlTextWriter.ctor(TextWriter,
+         XmlWriterSettings). It also resulted in dropping several internal
+         members in XmlTextWriter.
+
 2006-11-14  Atsushi Enomoto <atsushi@ximian.com>
 
        * XmlReaderSettings.cs : in copy constructor, copy (dispatch)
index 6ac77c08e01a4f90430261ecf6bdce764e914e60..c4ae87431ff81d29aec0f104e712fc0076cd8f40 100644 (file)
@@ -195,6 +195,13 @@ namespace Mono.Xml
                        }
                }
 
+               enum XmlDeclState {
+                       Allow,
+                       Ignore,
+                       Auto,
+                       Prohibit,
+               }
+
                // Instance fields
 
                Stream base_stream;
@@ -209,7 +216,7 @@ namespace Mono.Xml
                bool close_output_stream = true;
                bool ignore_encoding;
                bool namespaces = true;
-               bool output_xmldecl = false;
+               XmlDeclState xmldecl_state = XmlDeclState.Allow;
 
                bool check_character_validity;
                NewLineHandling newline_handling = NewLineHandling.None;
@@ -255,7 +262,7 @@ namespace Mono.Xml
                }
 
 #if NET_2_0
-               XmlTextWriter (
+               internal XmlTextWriter (
                        TextWriter writer, XmlWriterSettings settings)
                {
                        if (settings == null)
@@ -265,7 +272,27 @@ namespace Mono.Xml
 
                        close_output_stream = settings.CloseOutput;
                        allow_doc_fragment =
-                               settings.ConformanceLevel != System.Xml.ConformanceLevel.Document;
+                               settings.ConformanceLevel != ConformanceLevel.Document;
+                       switch (settings.ConformanceLevel) {
+                       case ConformanceLevel.Auto:
+                               xmldecl_state = settings.OmitXmlDeclaration ? XmlDeclState.Ignore : XmlDeclState.Allow;
+                               break;
+                       case ConformanceLevel.Document:
+                               // LAMESPEC:
+                               // On MSDN, XmlWriterSettings.OmitXmlDeclaration is documented as:
+                               // "The XML declaration is always written if
+                               //  ConformanceLevel is set to Document, even 
+                               //  if OmitXmlDeclaration is set to true. "
+                               // but it is incorrect. It does consider 
+                               // OmitXmlDeclaration property.
+                               xmldecl_state = settings.OmitXmlDeclaration ? XmlDeclState.Ignore : XmlDeclState.Auto;
+                               break;
+                       case ConformanceLevel.Fragment:
+                               xmldecl_state = XmlDeclState.Prohibit;
+                               break;
+                       }
+                       if (settings.Indent)
+                               Formatting = Formatting.Indented;
                        indent_string = settings.IndentChars == null ?
                                String.Empty : settings.IndentChars;
                        if (settings.NewLineChars != null)
@@ -274,8 +301,6 @@ namespace Mono.Xml
 
                        check_character_validity = settings.CheckCharacters;
                        newline_handling = settings.NewLineHandling;
-                       if (settings.OmitXmlDeclaration)
-                               output_xmldecl = false;
                }
 #endif
 
@@ -302,41 +327,12 @@ namespace Mono.Xml
 #if NET_2_0
                // 2.0 XmlWriterSettings support
 
-               internal bool CheckCharacters {
-                       set { check_character_validity = value; }
-               }
-
-               internal bool CloseOutput {
-                       set { close_output_stream = value; }
-               }
-
                // As for ConformanceLevel, MS.NET is inconsistent with
                // MSDN documentation. For example, even if ConformanceLevel
                // is set as .Auto, multiple WriteStartDocument() calls
                // result in an error.
                // ms-help://MS.NETFramework.v20.en/wd_xml/html/7db8802b-53d8-4735-a637-4d2d2158d643.htm
-               [MonoTODO]
-               internal ConformanceLevel ConformanceLevel {
-                       set {
-                               allow_doc_fragment = (value == System.Xml.ConformanceLevel.Fragment);
-                       }
-               }
 
-               internal string IndentChars {
-                       set { indent_string = (value == null) ? String.Empty : value; }
-               }
-
-               internal string NewLineChars {
-                       set { newline = (value == null) ? String.Empty : value; }
-               }
-
-               internal bool NewLineOnAttributes {
-                       set { indent_attributes = value; }
-               }
-
-               internal bool OmitXmlDeclaration {
-                       set { output_xmldecl = !value; }
-               }
 #endif
 
                // Literal Output Control
@@ -470,6 +466,15 @@ namespace Mono.Xml
                        if (state != WriteState.Start)
                                throw StateError ("XmlDeclaration");
 
+                       state = WriteState.Prolog;
+
+                       switch (xmldecl_state) {
+                       case XmlDeclState.Ignore:
+                               return;
+                       case XmlDeclState.Prohibit:
+                               throw InvalidOperation ("WriteStartDocument cannot be called when ConformanceLevel is Fragment.");
+                       }
+
                        writer.Write ("<?xml version=");
                        writer.Write (quote_char);
                        writer.Write ("1.0");
@@ -488,8 +493,7 @@ namespace Mono.Xml
                        }
                        writer.Write ("?>");
 
-                       output_xmldecl = false;
-                       state = WriteState.Prolog;
+                       xmldecl_state = XmlDeclState.Ignore;
                }
 
                public override void WriteEndDocument ()
@@ -526,7 +530,7 @@ namespace Mono.Xml
                                throw StateError ("DocType");
                        node_state = XmlNodeType.DocumentType;
 
-                       if (output_xmldecl)
+                       if (xmldecl_state == XmlDeclState.Auto)
                                OutputAutoStartDocument ();
 
                        WriteIndent ();
@@ -609,7 +613,7 @@ namespace Mono.Xml
                                throw new ArgumentException ("A prefix cannot be equivalent to \"xml\" in case-insensitive match.");
 
 
-                       if (output_xmldecl)
+                       if (xmldecl_state == XmlDeclState.Auto)
                                OutputAutoStartDocument ();
                        if (state == WriteState.Element)
                                CloseStartElement ();
@@ -1259,7 +1263,7 @@ namespace Mono.Xml
                        case WriteState.Start:
                                if (isCharacter)
                                        CheckMixedContentState ();
-                               if (output_xmldecl && !dontCheckXmlDecl)
+                               if (xmldecl_state == XmlDeclState.Auto && !dontCheckXmlDecl)
                                        OutputAutoStartDocument ();
                                state = WriteState.Prolog;
                                break;
@@ -1301,7 +1305,7 @@ namespace Mono.Xml
                        case WriteState.Start:
                                if (!allow_doc_fragment || is_document_entity)
                                        goto case WriteState.Closed;
-                               if (output_xmldecl)
+                               if (xmldecl_state == XmlDeclState.Auto)
                                        OutputAutoStartDocument ();
                                CheckMixedContentState ();
                                state = WriteState.Content;
index a30759875cf3ec2337a0d1905ff11ba9541815c9..f0022aaf052e500139389d6154ac32ad9ceb7d92 100644 (file)
@@ -150,23 +150,7 @@ namespace System.Xml
                {
                        if (settings == null)
                                settings = new XmlWriterSettings ();
-                       XmlTextWriter xtw = new XmlTextWriter (writer);
-                       // Indent, IndentChars
-                       if (settings.Indent) {
-                               xtw.Formatting = Formatting.Indented;
-                               xtw.IndentChars = settings.IndentChars;
-                               xtw.NewLineOnAttributes = settings.NewLineOnAttributes;
-                       }
-                       // NewLineChars
-                       xtw.NewLineChars = settings.NewLineChars;
-                       // CloseOutput
-                       xtw.CloseOutput = settings.CloseOutput;
-                       // ConformanceLevel
-                       xtw.ConformanceLevel = settings.ConformanceLevel;
-                       // OmitXmlDeclaration
-                       xtw.OmitXmlDeclaration = settings.OmitXmlDeclaration;
-                       // CheckCharacters
-                       xtw.CheckCharacters = settings.CheckCharacters;
+                       XmlTextWriter xtw = new XmlTextWriter (writer, settings);
                        return Create (xtw, settings);
                }
 
index d27e8e69f010868dba645cf8621031fb0621971d..41a59bc0851ed9e8d493fd31ac78142ea9b2f2db 100644 (file)
@@ -1,3 +1,8 @@
+2006-11-20  Atsushi Enomoto <atsushi@ximian.com>
+
+       * XmlWriterSettingsTests.cs : added tests for relationship between
+         ConformanceLevel, OmitXmlDeclaration and WriteStartDocument().
+
 2006-11-17  Atsushi Enomoto <atsushi@ximian.com>
 
        * XmlReaderSettingsTests.cs : added test for bug #79224; it does not
index 276d5b6215ad9edaa694777c681dfb80394837b9..3c8c507dd757c1b8c0bd12e1a10c5c5273b8aa76 100644 (file)
@@ -107,10 +107,73 @@ namespace MonoTests.System.Xml
                }
 
                [Test]
-               [Ignore ("Write Test!")]
-               public void ConformanceLevelTest ()
+               [ExpectedException (typeof (InvalidOperationException))]
+               public void ConformanceLevelFragmentAndWriteStartDocument ()
                {
-                       throw new NotImplementedException ();
+                       XmlWriterSettings s = new XmlWriterSettings ();
+                       s.ConformanceLevel = ConformanceLevel.Fragment;
+                       s.OmitXmlDeclaration = true;
+                       XmlWriter w = XmlWriter.Create (Console.Out, s);
+                       w.WriteStartDocument ();
+               }
+
+               [Test]
+               public void ConformanceLevelAuto ()
+               {
+                       XmlWriterSettings s = new XmlWriterSettings ();
+                       s.ConformanceLevel = ConformanceLevel.Auto;
+                       StringWriter sw = new StringWriter ();
+                       XmlWriter w = XmlWriter.Create (sw, s);
+                       w.WriteElementString ("foo", "");
+                       w.Close ();
+                       AssertEquals ("<foo />", sw.ToString ());
+               }
+
+               [Test]
+               public void ConformanceLevelAuto_WriteStartDocument ()
+               {
+                       XmlWriterSettings s = new XmlWriterSettings ();
+                       s.ConformanceLevel = ConformanceLevel.Auto;
+                       StringWriter sw = new StringWriter ();
+                       XmlWriter w = XmlWriter.Create (sw, s);
+                       w.WriteStartDocument ();
+                       w.WriteElementString ("foo", "");
+                       w.Close ();
+                       AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\"?><foo />", sw.ToString ());
+               }
+
+               [Test]
+               public void ConformanceLevelAuto_OmitXmlDecl_WriteStartDocument ()
+               {
+                       XmlWriterSettings s = new XmlWriterSettings ();
+                       s.ConformanceLevel = ConformanceLevel.Auto;
+                       s.OmitXmlDeclaration = true;
+                       StringWriter sw = new StringWriter ();
+                       XmlWriter w = XmlWriter.Create (sw, s);
+                       w.WriteStartDocument ();
+                       w.WriteElementString ("foo", "");
+                       w.Close ();
+                       AssertEquals ("<foo />", sw.ToString ());
+               }
+
+               [Test]
+               public void ConformanceLevelDocument_OmitXmlDeclDeclaration ()
+               {
+                       XmlWriterSettings s = new XmlWriterSettings ();
+                       s.ConformanceLevel = ConformanceLevel.Document;
+                       // LAMESPEC:
+                       // On MSDN, XmlWriterSettings.OmitXmlDeclaration is documented as:
+                       // "The XML declaration is always written if
+                       //  ConformanceLevel is set to Document, even 
+                       //  if OmitXmlDeclaration is set to true. "
+                       // but it is incorrect. It does consider 
+                       // OmitXmlDeclaration property.
+                       s.OmitXmlDeclaration = true;
+                       StringWriter sw = new StringWriter ();
+                       XmlWriter w = XmlWriter.Create (sw, s);
+                       w.WriteElementString ("foo", "");
+                       w.Close ();
+                       AssertEquals ("<foo />", sw.ToString ());
                }
 
                [Test]