2002-12-24 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[mono.git] / mcs / class / System.XML / System.Xml / XmlElement.cs
index 2b1c8f3d19143c1181f0bfb18937b610ef67ca8c..8ba8f30946c13a807193383095dbdfd97e9d1da3 100644 (file)
@@ -3,13 +3,16 @@
 //
 // Author:
 //   Jason Diamond (jason@injektilo.org)
+//   Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
 //
 // (C) 2002 Jason Diamond  http://injektilo.org/
+// (C) 2002 Atsushi Enomoto
 //
 
 using System;
 using System.Collections;
 using System.Xml.XPath;
+using System.IO;
 using System.Text;
 
 namespace System.Xml
@@ -84,23 +87,31 @@ namespace System.Xml
                                        this.RemoveChild(n);
                                }                 
 
-                               // How to get xml:lang and xml:space? Create logic as ConstructNamespaceManager()?
+                               // I hope there are any well-performance logic...
                                XmlNameTable nt = this.OwnerDocument.NameTable;
-                               XmlNamespaceManager nsmgr = this.ConstructNamespaceManager(); //new XmlNamespaceManager(nt);
-                               string lang = "";
-                               XmlSpace space = XmlSpace.Default;
-
-                               XmlParserContext ctx = new XmlParserContext(nt, nsmgr, lang, space);
-                               XmlTextReader xmlReader = new XmlTextReader(value, this.NodeType, ctx);
-                               this.ConstructDOM(xmlReader, this);
+                               XmlNamespaceManager nsmgr = this.ConstructNamespaceManager ();
+                               XmlParserContext ctx = new XmlParserContext (nt, nsmgr, XmlLang, this.XmlSpace);
+                               XmlTextReader xmlReader = OwnerDocument.ReusableReader;
+                               xmlReader.SetReaderContext (String.Empty, ctx);
+                               xmlReader.SetReaderFragment (new StringReader (value), XmlNodeType.Element);
+
+                               do {
+                                       XmlNode n = OwnerDocument.ReadNode (xmlReader);
+                                       if(n == null) break;
+                                       AppendChild (n);
+                               } while (true);
                        }
                }
 
-               [MonoTODO ("This is only a temporary fix, remove ASAP!")]
                public bool IsEmpty {
                        get { return isEmpty; }
 
-                       set { isEmpty = value; }
+                       set {
+                               if(value) {
+                                       RemoveAll();
+                               }
+                               isEmpty = value;
+                       }
                }
 
                public override string LocalName {
@@ -352,37 +363,21 @@ namespace System.Xml
                        w.WriteStartElement(Prefix, LocalName, NamespaceURI);
 
                        // write namespace declarations(if not exist)
-                       if(Prefix != null && w.LookupPrefix(Prefix) != NamespaceURI)
+                       if(Prefix != null && Prefix != String.Empty && w.LookupPrefix(Prefix) != NamespaceURI)
                                w.WriteAttributeString("xmlns", Prefix, "http://www.w3.org/2000/xmlns/", NamespaceURI);
 
                        foreach(XmlNode attributeNode in Attributes)
                        {
                                attributeNode.WriteTo(w);
                                // write namespace declarations(if not exist)
-                               if(attributeNode.Prefix != null && w.LookupPrefix(attributeNode.Prefix) != attributeNode.NamespaceURI)
+                               if(attributeNode.Prefix != null && attributeNode.Prefix != String.Empty &&
+                                  w.LookupPrefix(attributeNode.Prefix) != attributeNode.NamespaceURI &&
+                                  attributeNode.Prefix != "xmlns")
                                        w.WriteAttributeString("xmlns", attributeNode.Prefix, "http://www.w3.org/2000/xmlns/", attributeNode.NamespaceURI);
                        }
 
-                       // indent(when PreserveWhitespace = false)
-                       // Only XmlWriter has this XmlElement's xml:space information;-)
-                       if(!OwnerDocument.PreserveWhitespace && w.XmlSpace != XmlSpace.Preserve)
-                       {
-                               XmlNode n = this;
-                               StringBuilder sb = new StringBuilder();
-                               while(n != OwnerDocument)
-                               {
-                                       sb.Append('\t');
-                                       n = n.ParentNode;
-                               }
-                               w.WriteWhitespace(sb.ToString());
-                       }
-
                        WriteContentTo(w);
 
-                       // indent (linefeeding)
-                       if(!OwnerDocument.PreserveWhitespace && w.XmlSpace != XmlSpace.Preserve)
-                               w.WriteWhitespace("\n");
-
                        w.WriteEndElement();
                }