Added implementation of and tests for GetElementsByTagName courtesy of Matt Hunter...
authorJason Diamond <injektilo@mono-cvs.ximian.com>
Tue, 20 Aug 2002 03:56:55 +0000 (03:56 -0000)
committerJason Diamond <injektilo@mono-cvs.ximian.com>
Tue, 20 Aug 2002 03:56:55 +0000 (03:56 -0000)
svn path=/trunk/mcs/; revision=6788

mcs/class/System.XML/System.Xml/ChangeLog
mcs/class/System.XML/System.Xml/XmlDocument.cs
mcs/class/System.XML/System.Xml/XmlElement.cs
mcs/class/System.XML/Test/ChangeLog
mcs/class/System.XML/Test/XmlDocumentTests.cs
mcs/class/System.XML/Test/XmlElementTests.cs

index 9be31a65fc17861e2f5401af181ce5e6efc0663b..81fedfef6308276fdbde4f26f275735571dfddb1 100644 (file)
@@ -1,3 +1,8 @@
+2002-08-19  Jason Diamond <jason@injektilo.org>
+
+       * XmlDocument.cs, XmlElement.cs: Added implementation of 
+       GetElementsByTagName courtesy of Matt Hunter <xrkune@tconl.com>.
+
 2002-08-16  Jason Diamond <jason@injektilo.org>
 
        * XmlElement.cs: Fixed writing out qualified elements courtesy of
index 9a8b8f090aca0534821f61bada99e0e189b39b75..a3c4bd3b800868508d1209e7dac86dfc1cf71fc0 100644 (file)
@@ -17,6 +17,7 @@ using System.IO;
 using System.Text;
 using System.Xml.XPath;
 using System.Diagnostics;
+using System.Collections;
 
 namespace System.Xml
 {
@@ -339,10 +340,22 @@ namespace System.Xml
                        throw new NotImplementedException ();
                }
 
-               [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);
+                       }
                }
 
                [MonoTODO]
@@ -377,10 +390,10 @@ namespace System.Xml
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
                public virtual void Load (Stream inStream)
                {
-                       throw new NotImplementedException ();
+                       XmlReader xmlReader = new XmlTextReader (inStream);
+                       Load (xmlReader);
                }
 
                public virtual void Load (string filename)
index 67f1070bc967b6445789804a9f323206b174d799..9a11634a67214b9fd6086e7eb04ac1e896289af8 100644 (file)
@@ -8,6 +8,7 @@
 //
 
 using System;
+using System.Collections;
 
 namespace System.Xml
 {
@@ -166,10 +167,22 @@ namespace System.Xml
                        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);
+                       }
                }
 
                [MonoTODO]
index 2f8c6c0c42613c3b7acd4f8f842358c58441b50c..1ad4fa6afc1f28001a335a30484d7e5244942e95 100644 (file)
@@ -1,3 +1,8 @@
+2002-08-19  Jason Diamond <jason@injektilo.org>
+
+       * XmlDocument.cs, XmlElement.cs: Added tests for 
+       GetElementsByTagName courtesy of Matt Hunter <xrkune@tconl.com>.
+
 2002-08-17  Jason Diamond  <jason@injektilo.org>
 
        * XPathNavigatorMatchesTests.cs: Added tests for absolute patterns
index e44cfc9c869a75fdbfba1ade21bfabc9c1e444e3..427cc70091d2bf93bf8879d715aceaebcb1a659b 100644 (file)
@@ -11,6 +11,8 @@
 using System;
 using System.Collections;
 using System.Xml;
+using System.IO;
+using System.Text;
 
 using NUnit.Framework;
 
@@ -527,6 +529,24 @@ namespace MonoTests.System.Xml
                        catch (Exception) {}
                        AssertEquals (1, element.ChildNodes.Count);
                }
+
+               public void TestGetElementsByTagNameNoNameSpace ()
+               {
+                       string xml = @"<library><book><title>XML Fun</title><author>John Doe</author>
+                               <price>34.95</price></book><book><title>Bear and the Dragon</title>
+                               <author>Tom Clancy</author><price>6.95</price></book><book>
+                               <title>Bourne Identity</title><author>Robert Ludlum</author>
+                               <price>9.95</price></book><Fluffer><Nutter><book>
+                               <title>Bourne Ultimatum</title><author>Robert Ludlum</author>
+                               <price>9.95</price></book></Nutter></Fluffer></library>";
+
+                       MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml));
+                       document = new XmlDocument ();
+                       document.Load (memoryStream);
+                       XmlNodeList bookList = document.GetElementsByTagName ("book");
+                       AssertEquals ("GetElementsByTagName (string) returned incorrect count.", 4, bookList.Count);
+               }
+
        
                public void TestInnerAndOuterXml ()
                {
@@ -560,6 +580,22 @@ namespace MonoTests.System.Xml
                        AssertEquals (document.InnerXml, document.OuterXml);
                }
 
+               public void TestLoadWithSystemIOStream ()
+               {                       
+                       string xml = @"<library><book><title>XML Fun</title><author>John Doe</author>
+                               <price>34.95</price></book><book><title>Bear and the Dragon</title>
+                               <author>Tom Clancy</author><price>6.95</price></book><book>
+                               <title>Bourne Identity</title><author>Robert Ludlum</author>
+                               <price>9.95</price></book><Fluffer><Nutter><book>
+                               <title>Bourne Ultimatum</title><author>Robert Ludlum</author>
+                               <price>9.95</price></book></Nutter></Fluffer></library>";
+
+                       MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml));
+                       document = new XmlDocument ();
+                       document.Load (memoryStream);
+                       AssertEquals ("Not Loaded From IOStream", true, document.HasChildNodes);
+               }
+
                public void TestLoadXmlCDATA ()
                {
                        document.LoadXml ("<foo><![CDATA[bar]]></foo>");
index 58c910cce64ff947c798baf908ded43cded6438b..31fda435418aad5ed53ca565b7d74233548eae4f 100644 (file)
@@ -9,6 +9,8 @@
 
 using System;
 using System.Xml;
+using System.IO;
+using System.Text;
 
 using NUnit.Framework;
 
@@ -135,6 +137,23 @@ namespace MonoTests.System.Xml
                        AssertEquals ("val2", element.GetAttribute ("attr2"));
                }
 
+               public void TestGetElementsByTagNameNoNameSpace ()
+               {
+                       string xml = @"<library><book><title>XML Fun</title><author>John Doe</author>
+                               <price>34.95</price></book><book><title>Bear and the Dragon</title>
+                               <author>Tom Clancy</author><price>6.95</price></book><book>
+                               <title>Bourne Identity</title><author>Robert Ludlum</author>
+                               <price>9.95</price></book><Fluffer><Nutter><book>
+                               <title>Bourne Ultimatum</title><author>Robert Ludlum</author>
+                               <price>9.95</price></book></Nutter></Fluffer></library>";
+
+                       MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml));
+                       document = new XmlDocument ();
+                       document.Load (memoryStream);
+                       XmlNodeList bookList = document.GetElementsByTagName ("book");
+                       AssertEquals ("GetElementsByTagName (string) returned incorrect count.", 4, bookList.Count);
+               }
+
                public void TestOuterXmlWithNamespace ()
                {
                        XmlElement element = document.CreateElement ("foo", "bar", "#foo");