2003-02-03 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml / XmlElement.cs
index 2b1c8f3d19143c1181f0bfb18937b610ef67ca8c..6b6fd2838b8e06b11f48d2df56ea53c6d1970595 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
@@ -63,44 +66,52 @@ namespace System.Xml
                                return base.InnerText;
                        }
                        set {
-                               foreach(XmlNode n in ChildNodes)
-                               {
-                                       this.RemoveChild(n);
+                               // Why its behavior (of MS FCL) is different from InnerXml...?
+                               if (FirstChild != null && FirstChild.NodeType == XmlNodeType.Text)
+                                       FirstChild.Value = value;
+                               else {
+                                       if(FirstChild != null) {
+                                               foreach (XmlNode n in ChildNodes)
+                                                       this.RemoveChild (n);
+                                       }
+                                       // creates new Text node
+                                       AppendChild(OwnerDocument.CreateTextNode(value));
                                }
-                               // creates new Text node
-                               AppendChild(OwnerDocument.CreateTextNode(value));
                        }
                }
 
-               [MonoTODO ("Setter is immature")]
                public override string InnerXml {
                        get {
-                               // Not sure why this is an override.  Passing through for now.
                                return base.InnerXml;
                        }
                        set {
                                foreach(XmlNode n in ChildNodes)
-                               {
                                        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 {
@@ -346,43 +357,24 @@ namespace System.Xml
                                childNode.WriteTo(w);
                }
 
-               [MonoTODO("indenting feature is incomplete.")]
+               [MonoTODO]
                public override void WriteTo (XmlWriter w)
                {
                        w.WriteStartElement(Prefix, LocalName, NamespaceURI);
 
-                       // write namespace declarations(if not exist)
-                       if(Prefix != null && 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)
-                                       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());
+                       // write namespace declarations(if not exist)
+                       foreach(XmlNode attributeNode in Attributes) {
+                               if(attributeNode.Prefix != null && attributeNode.Prefix != String.Empty &&\r
+                                       w.LookupPrefix(attributeNode.Prefix) != attributeNode.NamespaceURI &&\r
+                                       attributeNode.Prefix != "xmlns")\r
+                                       w.WriteAttributeString("xmlns", attributeNode.Prefix, "http://www.w3.org/2000/xmlns/", attributeNode.NamespaceURI);
                        }
 
                        WriteContentTo(w);
 
-                       // indent (linefeeding)
-                       if(!OwnerDocument.PreserveWhitespace && w.XmlSpace != XmlSpace.Preserve)
-                               w.WriteWhitespace("\n");
-
                        w.WriteEndElement();
                }