* XmlDeclaration.cs : Fixed ParseInput() more parse strictly.
[mono.git] / mcs / class / System.XML / System.Xml / XmlElement.cs
index b98235f990ccb2b30b9bd0f4a10a072fca264592..4ccb1e7ba95a1003f5c8055e837089725a18649a 100644 (file)
@@ -8,6 +8,7 @@
 //
 
 using System;
+using System.Collections;
 
 namespace System.Xml
 {
@@ -16,6 +17,7 @@ namespace System.Xml
                #region Fields
 
                private XmlAttributeCollection attributes;
+               private XmlLinkedNode lastLinkedChild;
                private string localName;
                private string namespaceURI;
                private string prefix;
@@ -42,54 +44,56 @@ namespace System.Xml
                #region Properties
 
                public override XmlAttributeCollection Attributes {
-                       get { 
-                               return attributes; 
-                       }
+                       get { return attributes; }
                }
 
                public virtual bool HasAttributes {
-                       get { 
-                               return attributes.Count > 0; 
-                       }
+                       get { return attributes.Count > 0; }
                }
 
-               [MonoTODO]
+               [MonoTODO ("Setter.")]
                public override string InnerText {
-                       get { 
-                               throw new NotImplementedException (); 
-                       }
-
-                       set { 
-                               throw new NotImplementedException (); 
+                       get {
+                               // Not sure why this is an override.  Passing through for now.
+                               return base.InnerText;
                        }
+                       set { throw new NotImplementedException (); }
                }
 
-               [MonoTODO]
+               [MonoTODO ("Setter is immature")]
                public override string InnerXml {
-                       get { 
-                               throw new NotImplementedException (); 
+                       get {
+                               // Not sure why this is an override.  Passing through for now.
+                               return base.InnerXml;
                        }
-
-                       set { 
-                               throw new NotImplementedException (); 
+                       set {
+                               // How to get xml:lang and xml:space? Create logic as ConstructNamespaceManager()?
+                               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);
                        }
                }
 
                [MonoTODO]
-               public bool IsEmpty     {
-                       get { 
-                               throw new NotImplementedException (); 
-                       }
+               public bool IsEmpty {
+                       get { throw new NotImplementedException (); }
 
-                       set { 
-                               throw new NotImplementedException (); 
-                       }
+                       set { throw new NotImplementedException (); }
                }
 
+               internal override XmlLinkedNode LastLinkedChild {
+                       get { return lastLinkedChild; }
+
+                       set { lastLinkedChild = value; }
+               }
+               
                public override string LocalName {
-                       get { 
-                               return localName; 
-                       }
+                       get { return localName; }
                }
 
                public override string Name {
@@ -99,9 +103,7 @@ namespace System.Xml
                }
 
                public override string NamespaceURI {
-                       get { 
-                               return namespaceURI; 
-                       }
+                       get { return namespaceURI; }
                }
 
                [MonoTODO]
@@ -125,19 +127,34 @@ namespace System.Xml
                }
 
                public override string Prefix {
-                       get { 
-                               return prefix; 
-                       }
+                       get { return prefix; }
+                       set { prefix = value; }
                }
 
                #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]
@@ -150,37 +167,68 @@ namespace System.Xml
                [MonoTODO]
                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)
                {
-                       throw new NotImplementedException ();
+                       XmlNode attributeNode = Attributes.GetNamedItem (name);
+                       return attributeNode != null ? attributeNode as XmlAttribute : null;
                }
 
                [MonoTODO]
                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)
                {
-                       throw new NotImplementedException ();
+                       ArrayList nodeArrayList = new ArrayList ();
+                       this.searchNodesRecursively (this, name, nodeArrayList);
+                       return new XmlNodeArrayList (nodeArrayList);
+               }
+
+               private void searchNodesRecursively (XmlNode argNode, string argName, 
+                       ArrayList argArrayList)
+               {
+                       XmlNodeList xmlNodeList = argNode.ChildNodes;
+                       foreach (XmlNode node in xmlNodeList){
+                               if (node.Name.Equals (argName))
+                                       argArrayList.Add (node);
+                               else    
+                                       this.searchNodesRecursively (node, argName, argArrayList);
+                       }
+               }
+
+               private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI, 
+                       ArrayList argArrayList)
+               {
+                       XmlNodeList xmlNodeList = argNode.ChildNodes;
+                       foreach (XmlNode node in xmlNodeList)
+                       {
+                               if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
+                                       argArrayList.Add (node);
+                               else    
+                                       this.searchNodesRecursively (node, argName, argNamespaceURI, argArrayList);
+                       }
                }
 
-               [MonoTODO]
                public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
                {
-                       throw new NotImplementedException ();
+                       ArrayList nodeArrayList = new ArrayList ();
+                       this.searchNodesRecursively (this, localName, namespaceURI, nodeArrayList);
+                       return new XmlNodeArrayList (nodeArrayList);
                }
 
                [MonoTODO]
                public virtual bool HasAttribute (string name)
                {
-                       throw new NotImplementedException ();
+                       XmlNode attributeNode = Attributes.GetNamedItem (name);
+                       return attributeNode != null;
                }
 
                [MonoTODO]
@@ -199,10 +247,9 @@ namespace System.Xml
                        attributes.RemoveAll ();
                }
 
-               [MonoTODO]
                public virtual void RemoveAllAttributes ()
                {
-                       throw new NotImplementedException ();
+                       attributes.RemoveAll ();
                }
 
                [MonoTODO]
@@ -239,13 +286,13 @@ namespace System.Xml
                public virtual void SetAttribute (string name, string value)
                {
                        XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
-                       attribute.SetOwnerElement (this);
+                       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 ();
                }
@@ -253,25 +300,35 @@ namespace System.Xml
                [MonoTODO]
                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)
                {
-                       throw new NotImplementedException ();
+                       XmlDocument xmlDoc = this.OwnerDocument;\r
+                       XmlAttribute xmlAttribute = new XmlAttribute (String.Empty, localName, namespaceURI, xmlDoc);   \r
+                       return this.attributes.Append (xmlAttribute);
                }
 
-               [MonoTODO]
                public override void WriteContentTo (XmlWriter w)
                {
-                       throw new NotImplementedException ();
+                       foreach(XmlNode childNode in ChildNodes)
+                               childNode.WriteTo(w);
                }
 
-               [MonoTODO]
                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