MoveToAttribute* Fix 'if' brackets.
[mono.git] / mcs / class / System.XML / System.Xml / XmlTextWriter.cs
index 76342c3a4a0a6252303f70f271b6884da9d710f9..5d0de250549ceb11fe6bcb3a735506b3770aa021 100644 (file)
@@ -24,14 +24,15 @@ namespace System.Xml
                protected bool openStartElement = false;
                protected bool openStartAttribute = false;
                protected bool documentStarted = false;
-               protected bool namespaces = true;
+               private bool namespaces = true;
                protected bool openAttribute = false;
+               protected bool attributeWrittenForElement = false;
                protected Stack openElements = new Stack ();
-               protected Formatting formatting = Formatting.None;
-               protected int indentation = 2;
-               protected char indentChar = ' ';
+               private Formatting formatting = Formatting.None;
+               private int indentation = 2;
+               private char indentChar = ' ';
                protected string indentChars = "  ";
-               protected char quoteChar = '\"';
+               private char quoteChar = '\"';
                protected int indentLevel = 0;
                protected string indentFormatting;
                protected Stream baseStream = null;
@@ -125,7 +126,6 @@ namespace System.Xml
                        }
                }
 
-               [MonoTODO]
                public char QuoteChar {
                        get { return quoteChar; }
                        set {
@@ -197,22 +197,30 @@ namespace System.Xml
 
                public override void Close ()
                {
-                       while (openElements.Count > 0) {
-                               WriteEndElement();
-                       }
+                       CloseOpenAttributeAndElements ();
 
                        w.Close();
                        ws = WriteState.Closed;
                        openWriter = false;
                }
 
+               private void CloseOpenAttributeAndElements ()
+               {
+                       if (openAttribute)
+                               WriteEndAttribute ();
+
+                       while (openElements.Count > 0) {
+                               WriteEndElement();
+                       }
+               }
+
                private void CloseStartElement ()
                {
-                       if (openStartElement) 
-                       {
+                       if (openStartElement) {
                                w.Write(">");
                                ws = WriteState.Content;
                                openStartElement = false;
+                               attributeWrittenForElement = false;
                        }
                }
 
@@ -221,10 +229,14 @@ namespace System.Xml
                        w.Flush ();
                }
 
-               [MonoTODO]
                public override string LookupPrefix (string ns)
                {
-                       throw new NotImplementedException ();
+                       string prefix = namespaceManager.LookupPrefix (ns);
+
+                       if (prefix == String.Empty)
+                               prefix = null;
+
+                       return prefix;
                }
 
                private void UpdateIndentChars ()
@@ -234,10 +246,9 @@ namespace System.Xml
                                indentChars += indentChar;
                }
 
-               [MonoTODO]
                public override void WriteBase64 (byte[] buffer, int index, int count)
                {
-                       throw new NotImplementedException ();
+                       w.Write (Convert.ToBase64String (buffer, index, count));
                }
 
                [MonoTODO]
@@ -248,10 +259,8 @@ namespace System.Xml
 
                public override void WriteCData (string text)
                {
-                       if (text.IndexOf("]]>") > 0) 
-                       {
+                       if (text.IndexOf("]]>") > 0)
                                throw new ArgumentException ();
-                       }
 
                        CheckState ();
                        CloseStartElement ();
@@ -259,10 +268,16 @@ namespace System.Xml
                        w.Write("<![CDATA[{0}]]>", text);
                }
 
-               [MonoTODO]
                public override void WriteCharEntity (char ch)
                {
-                       throw new NotImplementedException ();
+                       Int16   intCh = (Int16)ch;
+
+                       // Make sure the character is not in the surrogate pair
+                       // character range, 0xd800- 0xdfff
+                       if ((intCh >= -10240) && (intCh <= -8193))
+                               throw new ArgumentException ("Surrogate Pair is invalid.");
+
+                       w.Write("&#x{0:X};", intCh);
                }
 
                [MonoTODO]
@@ -314,13 +329,22 @@ namespace System.Xml
                        openAttribute = false;
                }
 
-               [MonoTODO]
                public override void WriteEndDocument ()
                {
-                       throw new NotImplementedException ();
+                       if ((ws == WriteState.Start) || (ws == WriteState.Prolog))
+                               throw new ArgumentException ("This document does not have a root element.");
+
+                       CloseOpenAttributeAndElements ();
+
+                       ws = WriteState.Start;
                }
 
                public override void WriteEndElement ()
+               {
+                       WriteEndElementInternal (false);
+               }
+
+               private void WriteEndElementInternal (bool fullEndElement)
                {
                        if (openElements.Count == 0)
                                throw new InvalidOperationException("There was no XML start tag open.");
@@ -330,14 +354,21 @@ namespace System.Xml
                        CheckState ();
 
                        if (openStartElement) {
-                               w.Write (" />");
+                               if (openAttribute)
+                                       WriteEndAttribute ();
+
+                               if (fullEndElement)
+                                       w.Write ("></{0}>", ((XmlTextWriterOpenElement)openElements.Peek ()).Name);
+                               else
+                                       w.Write (" />");
+
                                openElements.Pop ();
                                openStartElement = false;
-                       }
-                       else {
+                       } else {
                                w.Write ("{0}</{1}>", indentFormatting, openElements.Pop ());
-                               namespaceManager.PopScope();
                        }
+
+                       namespaceManager.PopScope();
                }
 
                [MonoTODO]
@@ -346,10 +377,9 @@ namespace System.Xml
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
                public override void WriteFullEndElement ()
                {
-                       throw new NotImplementedException ();
+                       WriteEndElementInternal (true);
                }
 
                [MonoTODO]
@@ -394,7 +424,6 @@ namespace System.Xml
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO("haven't tested namespaces on attributes code yet.")]
                public override void WriteStartAttribute (string prefix, string localName, string ns)
                {
                        if ((prefix == "xml") && (localName == "lang"))
@@ -408,6 +437,9 @@ namespace System.Xml
 
                        CheckState ();
 
+                       if (ws == WriteState.Content)
+                               throw new InvalidOperationException ("Token StartAttribute in state " + WriteState + " would result in an invalid XML document.");
+
                        if (prefix == null)
                                prefix = String.Empty;
 
@@ -415,6 +447,7 @@ namespace System.Xml
                                ns = String.Empty;
 
                        string formatPrefix = "";
+                       string formatSpace = "";
 
                        if (ns != String.Empty) 
                        {
@@ -429,9 +462,13 @@ namespace System.Xml
                                formatPrefix = prefix + ":";
                        }
 
-                       w.Write (" {0}{1}={2}", formatPrefix, localName, quoteChar);
+                       if (openStartElement || attributeWrittenForElement)
+                               formatSpace = " ";
+
+                       w.Write ("{0}{1}{2}={3}", formatSpace, formatPrefix, localName, quoteChar);
 
                        openAttribute = true;
+                       attributeWrittenForElement = true;
                        ws = WriteState.Attribute;
                }