2002-08-16 Jason Diamond <jason@injektilo.org>
[mono.git] / mcs / class / System.XML / System.Xml / XmlElement.cs
index b4dac1e5a46a6e275efa4895f0937f53b92e62d1..67f1070bc967b6445789804a9f323206b174d799 100644 (file)
@@ -1,10 +1,11 @@
 //
-// System.Xml.XmlElement
+// System.Xml.XmlAttribute
 //
 // Author:
-//   Daniel Weber (daniel-weber@austin.rr.com)
+//   Jason Diamond (jason@injektilo.org)
+//
+// (C) 2002 Jason Diamond  http://injektilo.org/
 //
-// (C) 2001 Daniel Weber
 
 using System;
 
@@ -15,6 +16,7 @@ namespace System.Xml
                #region Fields
 
                private XmlAttributeCollection attributes;
+               private XmlLinkedNode lastLinkedChild;
                private string localName;
                private string namespaceURI;
                private string prefix;
@@ -23,105 +25,117 @@ namespace System.Xml
 
                #region Constructor
 
-               protected internal XmlElement(string prefix, string localName, string namespaceURI, XmlDocument doc) : base(doc)
+               protected internal XmlElement (
+                       string prefix, 
+                       string localName, 
+                       string namespaceURI, 
+                       XmlDocument doc) : base (doc)
                {
                        this.prefix = prefix;
                        this.localName = localName;
                        this.namespaceURI = namespaceURI;
 
-                       attributes = new XmlAttributeCollection(this);
+                       attributes = new XmlAttributeCollection (this);
                }
 
                #endregion
 
                #region Properties
 
-               public override XmlAttributeCollection Attributes
-               {
+               public override XmlAttributeCollection Attributes {
                        get { return attributes; }
                }
 
-               public virtual bool HasAttributes
-               {
+               public virtual bool HasAttributes {
                        get { return attributes.Count > 0; }
                }
 
-               [MonoTODO]
-               public override string InnerText
-               {
-                       get { throw new NotImplementedException(); }
-                       set { throw new NotImplementedException(); }
+               [MonoTODO ("Setter.")]
+               public override string InnerXml {
+                       get {
+                               // Not sure why this is an override.  Passing through for now.
+                               return base.InnerXml;
+                       }
+                       set { throw new NotImplementedException (); }
                }
 
                [MonoTODO]
-               public override string InnerXml
-               {
-                       get { throw new NotImplementedException(); }
-                       set { throw new NotImplementedException(); }
-               }
+               public bool IsEmpty {
+                       get { throw new NotImplementedException (); }
 
-               [MonoTODO]
-               public bool IsEmpty
-               {
-                       get { throw new NotImplementedException(); }
-                       set { throw new NotImplementedException(); }
+                       set { throw new NotImplementedException (); }
                }
 
-               public override string LocalName
-               {
+               internal override XmlLinkedNode LastLinkedChild {
+                       get { return lastLinkedChild; }
+
+                       set { lastLinkedChild = value; }
+               }
+               
+               public override string LocalName {
                        get { return localName; }
                }
 
-               public override string Name
-               {
-                       get { return prefix != String.Empty ? prefix + ":" + localName : localName; }
+               public override string Name {
+                       get { 
+                               return prefix != String.Empty ? prefix + ":" + localName : localName; 
+                       }
                }
 
-               public override string NamespaceURI
-               {
+               public override string NamespaceURI {
                        get { return namespaceURI; }
                }
 
-               [MonoTODO()]
-               public override XmlNode NextSibling
-               {
-                       get { return base.NextSibling; }
-               }
-
-               public override XmlNodeType NodeType
-               {
-                       get { return XmlNodeType.Element; }
+               [MonoTODO]
+               public override XmlNode NextSibling {
+                       get { 
+                               return base.NextSibling; 
+                       }
                }
 
-               public override string Prefix
-               {
-                       get { return prefix; }
+               public override XmlNodeType NodeType {
+                       get { 
+                               return XmlNodeType.Element; 
+                       }
                }
 
                [MonoTODO]
-               public override XmlDocument OwnerDocument
-               {
-                       get { return base.OwnerDocument; }
+               public override XmlDocument OwnerDocument {
+                       get { 
+                               return base.OwnerDocument; 
+                       }
                }
 
-               public override string Value
-               {
-                       get { return null; }
-
-                       set
-                       {
-                               // Do nothing.
+               public override string Prefix {
+                       get { 
+                               return prefix; 
                        }
                }
 
                #endregion
 
                #region Methods
-
+               
                [MonoTODO]
                public override XmlNode CloneNode (bool deep)
                {
-                       throw new NotImplementedException();
+                       XmlNode node =  new XmlElement (prefix, localName, namespaceURI,
+                                                       OwnerDocument);
+
+                       for (int i = 0; i < node.Attributes.Count; i++)
+                               node.AppendChild (node.Attributes [i].CloneNode (false));
+                       
+                       if (deep) {
+                               while ((node != null) && (node.HasChildNodes)) {                                        
+                                       AppendChild (node.NextSibling.CloneNode (true));
+                                       node = node.NextSibling;
+                               }
+                       } // shallow cloning
+                               
+                       //
+                       // Reminder: Also look into Default attributes.
+                       //
+                       return node;
                }
 
                [MonoTODO]
@@ -132,129 +146,142 @@ namespace System.Xml
                }
 
                [MonoTODO]
-               public virtual string GetAttribute(string localName, string namespaceURI)
+               public virtual string GetAttribute (string localName, string namespaceURI)
                {
-                       throw new NotImplementedException();
+                       XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
+                       return attributeNode != null ? attributeNode.Value : String.Empty;
                }
 
                [MonoTODO]
-               public virtual XmlAttribute GetAttributeNode(string name)
+               public virtual XmlAttribute GetAttributeNode (string name)
                {
-                       throw new NotImplementedException();
+                       XmlNode attributeNode = Attributes.GetNamedItem (name);
+                       return attributeNode != null ? attributeNode as XmlAttribute : null;
                }
 
                [MonoTODO]
-               public virtual XmlAttribute GetAttributeNode(string localName, string namespaceURI)
+               public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
                {
-                       throw new NotImplementedException();
+                       XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
+                       return attributeNode != null ? attributeNode as XmlAttribute : null;
                }
 
                [MonoTODO]
-               public virtual XmlNodeList GetElementsByTagName(string name)
+               public virtual XmlNodeList GetElementsByTagName (string name)
                {
-                       throw new NotImplementedException();
+                       throw new NotImplementedException ();
                }
 
                [MonoTODO]
-               public virtual XmlNodeList GetElementsByTagName(string localName, string namespaceURI)
+               public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
                {
-                       throw new NotImplementedException();
+                       throw new NotImplementedException ();
                }
 
                [MonoTODO]
-               public virtual bool HasAttribute(string name)
+               public virtual bool HasAttribute (string name)
                {
-                       throw new NotImplementedException();
+                       XmlNode attributeNode = Attributes.GetNamedItem (name);
+                       return attributeNode != null;
                }
 
                [MonoTODO]
-               public virtual bool HasAttribute(string localName, string namespaceURI)
+               public virtual bool HasAttribute (string localName, string namespaceURI)
                {
-                       throw new NotImplementedException();
+                       throw new NotImplementedException ();
                }
 
-               [MonoTODO("Don't remove default attributes.")]
-               public override void RemoveAll()
+               [MonoTODO ("Don't remove default attributes.")]
+               public override void RemoveAll ()
                {
                        // Remove the child nodes.
-                       base.RemoveAll();
+                       base.RemoveAll ();
 
                        // Remove all attributes.
-                       attributes.RemoveAll();
+                       attributes.RemoveAll ();
                }
 
                [MonoTODO]
-               public virtual void RemoveAllAttributes()
+               public virtual void RemoveAllAttributes ()
                {
-                       throw new NotImplementedException();
+                       throw new NotImplementedException ();
                }
 
                [MonoTODO]
-               public virtual void RemoveAttribute(string name)
+               public virtual void RemoveAttribute (string name)
                {
-                       throw new NotImplementedException();
+                       throw new NotImplementedException ();
                }
 
                [MonoTODO]
-               public virtual void RemoveAttribute(string localName, string namespaceURI)
+               public virtual void RemoveAttribute (string localName, string namespaceURI)
                {
-                       throw new NotImplementedException();
+                       throw new NotImplementedException ();
                }
 
                [MonoTODO]
-               public virtual XmlNode RemoveAttributeAt(int i)
+               public virtual XmlNode RemoveAttributeAt (int i)
                {
-                       throw new NotImplementedException();
+                       throw new NotImplementedException ();
                }
 
                [MonoTODO]
-               public virtual XmlAttribute RemoveAttributeNode(XmlAttribute oldAttr)
+               public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
                {
-                       throw new NotImplementedException();
+                       throw new NotImplementedException ();
                }
 
                [MonoTODO]
-               public virtual XmlAttribute RemoveAttributeNode(string localName, string namespaceURI)
+               public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
                {
-                       throw new NotImplementedException();
+                       throw new NotImplementedException ();
                }
 
                [MonoTODO]
-               public virtual void SetAttribute(string name, string value)
+               public virtual void SetAttribute (string name, string value)
                {
                        XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
+                       attribute.SetParentNode (this);
                        attribute.Value = value;
                        Attributes.SetNamedItem (attribute);
                }
 
                [MonoTODO]
-               public virtual void SetAttribute(string localName, string namespaceURI, string value)
+               public virtual string SetAttribute (string localName, string namespaceURI, string value)
                {
-                       throw new NotImplementedException();
+                       throw new NotImplementedException ();
                }
 
                [MonoTODO]
-               public virtual XmlAttribute SetAttributeNode(XmlAttribute newAttr)
+               public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
                {
-                       throw new NotImplementedException();
+                       newAttr.SetParentNode(this);
+                       XmlNode oldAttr = Attributes.SetNamedItem(newAttr);
+                       return oldAttr != null ? oldAttr as XmlAttribute : null;
                }
 
                [MonoTODO]
-               public virtual XmlAttribute SetAttributeNode(string localName, string namespaceURI)
+               public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
                {
-                       throw new NotImplementedException();
+                       throw new NotImplementedException ();
                }
 
-               [MonoTODO]
-               public override void WriteContentTo(XmlWriter w)
+               public override void WriteContentTo (XmlWriter w)
                {
-                       throw new NotImplementedException();
+                       foreach(XmlNode childNode in ChildNodes)
+                               childNode.WriteTo(w);
                }
 
-               [MonoTODO]
-               public override void WriteTo(XmlWriter w)
+               public override void WriteTo (XmlWriter w)
                {
-                       throw new NotImplementedException();
+                       w.WriteStartElement(Prefix, LocalName, NamespaceURI);
+
+                       foreach(XmlNode attributeNode in Attributes)
+                               attributeNode.WriteTo(w);
+
+                       WriteContentTo(w);
+
+                       w.WriteEndElement();
                }
 
                #endregion