* XmlDeclaration.cs : Fixed ParseInput() more parse strictly.
[mono.git] / mcs / class / System.XML / System.Xml / XmlWriter.cs
index e70e6869f7bfd4baa744c81eb0da27704b83e01c..ee0f46f802d6c27bf58eb9f344936c39c6363e76 100644 (file)
@@ -13,6 +13,13 @@ namespace System.Xml
 {
        public abstract class XmlWriter
        {
+               #region Fields
+
+               protected WriteState ws = WriteState.Start;
+               protected XmlNamespaceManager namespaceManager = new XmlNamespaceManager (new NameTable ());
+
+               #endregion
+
                #region Constructors
 
                protected XmlWriter () { }
@@ -37,28 +44,72 @@ namespace System.Xml
 
                public abstract string LookupPrefix (string ns);
 
-               [MonoTODO]
+               [MonoTODO("DTDs must be implemented to use 'defattr' parameter.")]
                public virtual void WriteAttributes (XmlReader reader, bool defattr)
                {
-                       throw new NotImplementedException ();
+                       if(reader == null)
+                               throw new ArgumentException("null XmlReader specified.", "reader");
+
+                       switch(reader.NodeType)
+                       {
+                               case XmlNodeType.XmlDeclaration:
+                                       // this method doesn't write "<?xml " and "?>", at least MS .NET Framework as yet.
+                                       XmlDeclaration decl = new XmlDeclaration("1.0", String.Empty, String.Empty, null);
+                                       decl.Value = reader.Value;
+                                       if(decl.Version != null && decl.Version != String.Empty) WriteAttributeString("version", decl.Version);
+                                       if(decl.Encoding != null && decl.Encoding != String.Empty) WriteAttributeString("encoding", decl.Encoding);
+                                       if(decl.Standalone != null && decl.Standalone != String.Empty) WriteAttributeString("standalone", decl.Standalone);
+                                       break;
+                               case XmlNodeType.Element:
+                                       while (reader.MoveToNextAttribute ()) \r
+                                       {
+                                               WriteAttributeString(reader.Prefix, reader.LocalName, reader.NamespaceURI, reader.Value);
+                                       }
+                                       break;
+                               case XmlNodeType.Attribute:
+                                       do
+                                       {
+                                               WriteAttributeString(reader.Prefix, reader.LocalName, reader.NamespaceURI, reader.Value);
+                                       }
+                                       while (reader.MoveToNextAttribute ()) ;\r
+                                       break;
+                               default:
+                                       throw new XmlException("NodeType is not one of Element, Attribute, nor XmlDeclaration.");
+                       }
                }
 
-               [MonoTODO]
                public void WriteAttributeString (string localName, string value)
                {
-                       throw new NotImplementedException ();
+                       WriteAttributeString ("", localName, "", value);
                }
 
-               [MonoTODO]
                public void WriteAttributeString (string localName, string ns, string value)
                {
-                       throw new NotImplementedException ();
+                       WriteAttributeString ("", localName, ns, value);
                }
 
-               [MonoTODO]
                public void WriteAttributeString (string prefix, string localName, string ns, string value)
                {
-                       throw new NotImplementedException ();
+                       if ((prefix == "xmlns") || (localName == "xmlns"))
+                         {
+                               ns = value;
+                               if (prefix == "xmlns" && namespaceManager.HasNamespace (localName))
+                                       return;
+                               else if (localName == "xmlns" && namespaceManager.HasNamespace (String.Empty))
+                                       return;
+                         }
+                       
+                       WriteStartAttribute (prefix, localName, ns);
+                       WriteString (value);
+                       WriteEndAttribute ();
+
+                       if ((prefix == "xmlns") || (localName == "xmlns")) 
+                       {
+                               if (prefix == "xmlns")
+                                       namespaceManager.AddNamespace (localName, ns);
+                               else
+                                       namespaceManager.AddNamespace ("", ns);
+                       }
                }
 
                public abstract void WriteBase64 (byte[] buffer, int index, int count);
@@ -117,10 +168,9 @@ namespace System.Xml
 
                public abstract void WriteRaw (char[] buffer, int index, int count);
 
-               [MonoTODO]
                public void WriteStartAttribute (string localName, string ns)
                {
-                       throw new NotImplementedException ();
+                       WriteStartAttribute ("", localName, ns);
                }
 
                public abstract void WriteStartAttribute (string prefix, string localName, string ns);
@@ -131,12 +181,12 @@ namespace System.Xml
 
                public void WriteStartElement (string localName)
                {
-                       WriteStartElement ("", localName, "");
+                       WriteStartElement (String.Empty, localName, String.Empty);
                }
 
                public void WriteStartElement (string localName, string ns)
                {
-                       WriteStartElement ("", localName, ns);
+                       WriteStartElement (String.Empty, localName, ns);
                }
 
                public abstract void WriteStartElement (string prefix, string localName, string ns);