2002-12-21 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[mono.git] / mcs / class / System.XML / System.Xml / XmlWriter.cs
index 71eb6b16506d67dab84e07a6cb9a666b494bf9d0..29050c5f31512e164c8d03426557c6184dbd5aea 100644 (file)
@@ -44,10 +44,38 @@ 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 ()) 
+                                       {
+                                               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 ()) ;
+                                       break;
+                               default:
+                                       throw new XmlException("NodeType is not one of Element, Attribute, nor XmlDeclaration.");
+                       }
                }
 
                public void WriteAttributeString (string localName, string value)
@@ -65,10 +93,14 @@ namespace System.Xml
                        if ((prefix == "xmlns") || (localName == "xmlns"))
                          {
                                ns = value;
+                               
                                if (prefix == "xmlns" && namespaceManager.HasNamespace (localName))
                                        return;
+                               
+                               /* Users need to be able to re-declare the default namespace for subnodes
                                else if (localName == "xmlns" && namespaceManager.HasNamespace (String.Empty))
                                        return;
+                               */
                          }
                        
                        WriteStartAttribute (prefix, localName, ns);
@@ -82,6 +114,7 @@ namespace System.Xml
                                else
                                        namespaceManager.AddNamespace ("", ns);
                        }
+                       
                }
 
                public abstract void WriteBase64 (byte[] buffer, int index, int count);