2002-12-24 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[mono.git] / mcs / class / System.XML / System.Xml / XmlElement.cs
index c79010411e065730f6d961aba10111b2e4d6d8d5..8ba8f30946c13a807193383095dbdfd97e9d1da3 100644 (file)
@@ -1,15 +1,19 @@
 //
-// System.Xml.XmlAttribute
+// System.Xml.XmlElement
 //
 // 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
 {
@@ -18,10 +22,10 @@ namespace System.Xml
                #region Fields
 
                private XmlAttributeCollection attributes;
-               private XmlLinkedNode lastLinkedChild;
                private string localName;
                private string namespaceURI;
                private string prefix;
+               private bool isEmpty;
 
                #endregion
 
@@ -38,6 +42,11 @@ namespace System.Xml
                        this.namespaceURI = namespaceURI;
 
                        attributes = new XmlAttributeCollection (this);
+
+                       // TODO: Adds default attributes
+                       if(doc.DocumentType != null)
+                       {
+                       }
                }
 
                #endregion
@@ -52,13 +61,18 @@ namespace System.Xml
                        get { return attributes.Count > 0; }
                }
 
-               [MonoTODO ("Setter.")]
                public override string InnerText {
                        get {
-                               // Not sure why this is an override.  Passing through for now.
                                return base.InnerText;
                        }
-                       set { throw new NotImplementedException (); }
+                       set {
+                               foreach(XmlNode n in ChildNodes)
+                               {
+                                       this.RemoveChild(n);
+                               }
+                               // creates new Text node
+                               AppendChild(OwnerDocument.CreateTextNode(value));
+                       }
                }
 
                [MonoTODO ("Setter is immature")]
@@ -73,31 +87,33 @@ 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]
                public bool IsEmpty {
-                       get { throw new NotImplementedException (); }
+                       get { return isEmpty; }
 
-                       set { throw new NotImplementedException (); }
+                       set {
+                               if(value) {
+                                       RemoveAll();
+                               }
+                               isEmpty = value;
+                       }
                }
 
-               internal override XmlLinkedNode LastLinkedChild {
-                       get { return lastLinkedChild; }
-
-                       set { lastLinkedChild = value; }
-               }
-               
                public override string LocalName {
                        get { return localName; }
                }
@@ -246,10 +262,11 @@ namespace System.Xml
                [MonoTODO]
                public virtual bool HasAttribute (string localName, string namespaceURI)
                {
-                       throw new NotImplementedException ();
+                       XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
+                       return attributeNode != null;
                }
 
-               [MonoTODO ("Don't remove default attributes.")]
+               [MonoTODO ("confirm not removing default attributes [when DTD feature was implemented.")]
                public override void RemoveAll ()
                {
                        // Remove the child nodes.
@@ -259,69 +276,78 @@ namespace System.Xml
                        attributes.RemoveAll ();
                }
 
+               [MonoTODO ("confirm not removing default attributes [when DTD feature was implemented.")]
                public virtual void RemoveAllAttributes ()
                {
                        attributes.RemoveAll ();
                }
 
-               [MonoTODO]
+               [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
                public virtual void RemoveAttribute (string name)
                {
-                       throw new NotImplementedException ();
+                       attributes.Remove((XmlAttribute)attributes.GetNamedItem(name));
                }
 
-               [MonoTODO]
+               [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
                public virtual void RemoveAttribute (string localName, string namespaceURI)
                {
-                       throw new NotImplementedException ();
+                       attributes.Remove((XmlAttribute)attributes.GetNamedItem(localName, namespaceURI));
                }
 
-               [MonoTODO]
+               [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
                public virtual XmlNode RemoveAttributeAt (int i)
                {
-                       throw new NotImplementedException ();
+                       return attributes.Remove(attributes[i]);
                }
 
-               [MonoTODO]
+               [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
                public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
                {
-                       throw new NotImplementedException ();
+                       return attributes.Remove(oldAttr);
                }
 
-               [MonoTODO]
+               [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
                public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
                {
-                       throw new NotImplementedException ();
+                       return attributes.Remove(attributes[localName, namespaceURI]);
                }
 
                [MonoTODO]
                public virtual void SetAttribute (string name, string value)
                {
                        XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
-                       attribute.SetParentNode (this);
+                       attribute.SetOwnerElement(this);
                        attribute.Value = value;
                        Attributes.SetNamedItem (attribute);
                }
 
-               [MonoTODO]
+//             [MonoTODO]
                public virtual string SetAttribute (string localName, string namespaceURI, string value)
                {
-                       throw new NotImplementedException ();
+                       XmlAttribute attr = attributes[localName, namespaceURI];
+                       if(attr == null)
+                       {
+                               attr = OwnerDocument.CreateAttribute(localName, namespaceURI);
+                               attr.Value = value;
+                               attributes.SetNamedItem(attr);
+                       }
+                       else
+                               attr.Value = value;
+                       return attr.Value;
                }
 
-               [MonoTODO]
+//             [MonoTODO]
                public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
                {
-                       newAttr.SetParentNode(this);
+                       newAttr.SetOwnerElement(this);
                        XmlNode oldAttr = Attributes.SetNamedItem(newAttr);
                        return oldAttr != null ? oldAttr as XmlAttribute : null;
                }
 
-               
                public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
                {
-                       XmlDocument xmlDoc = this.OwnerDocument;\r
-                       XmlAttribute xmlAttribute = new XmlAttribute (String.Empty, localName, namespaceURI, xmlDoc);   \r
+                       XmlDocument xmlDoc = this.OwnerDocument;
+                       XmlAttribute xmlAttribute = new XmlAttribute (String.Empty, localName, namespaceURI, xmlDoc);
                        return this.attributes.Append (xmlAttribute);
                }
 
@@ -331,12 +357,24 @@ namespace System.Xml
                                childNode.WriteTo(w);
                }
 
+               [MonoTODO("indenting feature is incomplete.")]
                public override void WriteTo (XmlWriter w)
                {
                        w.WriteStartElement(Prefix, LocalName, NamespaceURI);
 
+                       // write namespace declarations(if not exist)
+                       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 && 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);
+                       }
 
                        WriteContentTo(w);