Просмотр исходного кода

Added implementation of and tests for GetElementsByTagName courtesy of Matt Hunter <[email protected]>.

svn path=/trunk/mcs/; revision=6788
Jason Diamond 23 лет назад
Родитель
Сommit
afa895fbfd

+ 5 - 0
mcs/class/System.XML/System.Xml/ChangeLog

@@ -1,3 +1,8 @@
+2002-08-19  Jason Diamond <[email protected]>
+
+	* XmlDocument.cs, XmlElement.cs: Added implementation of 
+	GetElementsByTagName courtesy of Matt Hunter <[email protected]>.
+
 2002-08-16  Jason Diamond <[email protected]>
 
 	* XmlElement.cs: Fixed writing out qualified elements courtesy of

+ 17 - 4
mcs/class/System.XML/System.Xml/XmlDocument.cs

@@ -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)

+ 15 - 2
mcs/class/System.XML/System.Xml/XmlElement.cs

@@ -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]

+ 5 - 0
mcs/class/System.XML/Test/ChangeLog

@@ -1,3 +1,8 @@
+2002-08-19  Jason Diamond <[email protected]>
+
+	* XmlDocument.cs, XmlElement.cs: Added tests for 
+	GetElementsByTagName courtesy of Matt Hunter <[email protected]>.
+
 2002-08-17  Jason Diamond  <[email protected]>
 
 	* XPathNavigatorMatchesTests.cs: Added tests for absolute patterns

+ 36 - 0
mcs/class/System.XML/Test/XmlDocumentTests.cs

@@ -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>");

+ 19 - 0
mcs/class/System.XML/Test/XmlElementTests.cs

@@ -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");