Forráskód Böngészése

Added implementation of namepsace qualified GetElementsByTagName courtesy of
Matt Hunter <[email protected]>.

svn path=/trunk/mcs/; revision=6885

Jason Diamond 23 éve
szülő
commit
86ffcfbcd7

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

@@ -1,3 +1,9 @@
+2002-08-22  Jason Diamond <[email protected]>
+
+	* XmlDocument.cs, XmlElement.cs: Added implementation of namepsace
+	qualified GetElementsByTagName courtesy of Matt Hunter 
+	<[email protected]>.
+
 2002-08-19  Jason Diamond <[email protected]>
 
 	* XmlDocument.cs, XmlElement.cs: Added implementation of 

+ 8 - 6
mcs/class/System.XML/System.Xml/XmlDocument.cs

@@ -343,25 +343,27 @@ namespace System.Xml
 		public virtual XmlNodeList GetElementsByTagName (string name)
 		{
 			ArrayList nodeArrayList = new ArrayList ();
-			this.searchNodesRecursively (this, name, nodeArrayList);
+			this.searchNodesRecursively (this, name, String.Empty, nodeArrayList);
 			return new XmlNodeArrayList (nodeArrayList);
 		}
 
-		private void searchNodesRecursively (XmlNode argNode, string argName, ArrayList argArrayList)
+		private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI, 
+			ArrayList argArrayList)
 		{
 			XmlNodeList xmlNodeList = argNode.ChildNodes;
 			foreach (XmlNode node in xmlNodeList){
-				if (node.Name.Equals (argName))
+				if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
 					argArrayList.Add (node);
 				else	
-					this.searchNodesRecursively (node, argName, argArrayList);
+					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);
 		}
 
 		private XmlNodeType GetNodeTypeFromString (string nodeTypeString)

+ 7 - 11
mcs/class/System.XML/System.Xml/XmlElement.cs

@@ -170,27 +170,23 @@ namespace System.Xml
 		public virtual XmlNodeList GetElementsByTagName (string name)
 		{
 			ArrayList nodeArrayList = new ArrayList ();
-			this.searchNodesRecursively (this, name, nodeArrayList);
+			this.searchNodesRecursively (this, name, String.Empty, nodeArrayList);
 			return new XmlNodeArrayList (nodeArrayList);
 		}
 
-		private void searchNodesRecursively (XmlNode argNode, string argName, ArrayList argArrayList)
+		private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI, 
+			ArrayList argArrayList)
 		{
 			XmlNodeList xmlNodeList = argNode.ChildNodes;
-			foreach (XmlNode node in xmlNodeList){
-				if (node.Name.Equals (argName))
+			foreach (XmlNode node in xmlNodeList)
+			{
+				if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
 					argArrayList.Add (node);
 				else	
-					this.searchNodesRecursively (node, argName, argArrayList);
+					this.searchNodesRecursively (node, argName, argNamespaceURI, argArrayList);
 			}
 		}
 
-		[MonoTODO]
-		public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
-		{
-			throw new NotImplementedException ();
-		}
-
 		[MonoTODO]
 		public virtual bool HasAttribute (string name)
 		{

+ 7 - 1
mcs/class/System.XML/Test/ChangeLog

@@ -1,6 +1,12 @@
+2002-08-22  Jason Diamond <[email protected]>
+
+	* XmlDocumentTests.cs, XmlElementTests.cs: Added tests for 
+	namespace qualified GetElementsByTagName courtesy of Matt Hunter 
+	<[email protected]>.
+
 2002-08-19  Jason Diamond <[email protected]>
 
-	* XmlDocument.cs, XmlElement.cs: Added tests for 
+	* XmlDocumentTests.cs, XmlElementTests.cs: Added tests for 
 	GetElementsByTagName courtesy of Matt Hunter <[email protected]>.
 
 2002-08-17  Jason Diamond  <[email protected]>

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

@@ -547,6 +547,29 @@ namespace MonoTests.System.Xml
 			AssertEquals ("GetElementsByTagName (string) returned incorrect count.", 4, bookList.Count);
 		}
 
+		public void TestGetElementsByTagNameUsingNameSpace ()
+		{
+			StringBuilder xml = new StringBuilder ();
+			xml.Append ("<?xml version=\"1.0\" ?><library xmlns:North=\"http://www.foo.com\"");
+			xml.Append ("xmlns:South=\"http://www.goo.com\"><North:book type=\"non-fiction\"> ");
+			xml.Append ("<North:title type=\"intro\">XML Fun</North:title> " );
+			xml.Append ("<North:author>John Doe</North:author> " );
+			xml.Append ("<North:price>34.95</North:price></North:book> " );
+			xml.Append ("<South:book type=\"fiction\"> " );
+			xml.Append ("<South:title>Bear and the Dragon</South:title> " );
+			xml.Append ("<South:author>Tom Clancy</South:author> " );
+                        xml.Append ("<South:price>6.95</South:price></South:book> " );
+			xml.Append ("<South:book type=\"fiction\"><South:title>Bourne Identity</South:title> " );
+			xml.Append ("<South:author>Robert Ludlum</South:author> " );
+			xml.Append ("<South:price>9.95</South:price></South:book></library>");
+
+			MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
+			document = new XmlDocument ();
+			document.Load (memoryStream);
+			XmlNodeList bookList = document.GetElementsByTagName ("book", "http://www.goo.com");
+			AssertEquals ("GetElementsByTagName (string, uri) returned incorrect count.", 2, bookList.Count);
+		}
+
 	
 		public void TestInnerAndOuterXml ()
 		{

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

@@ -154,6 +154,29 @@ namespace MonoTests.System.Xml
 			AssertEquals ("GetElementsByTagName (string) returned incorrect count.", 4, bookList.Count);
 		}
 
+		public void TestGetElementsByTagNameUsingNameSpace ()
+		{
+			StringBuilder xml = new StringBuilder ();
+			xml.Append ("<?xml version=\"1.0\" ?><library xmlns:North=\"http://www.foo.com\"");
+			xml.Append ("xmlns:South=\"http://www.goo.com\"><North:book type=\"non-fiction\"> ");
+			xml.Append ("<North:title type=\"intro\">XML Fun</North:title> " );
+			xml.Append ("<North:author>John Doe</North:author> " );
+			xml.Append ("<North:price>34.95</North:price></North:book> " );
+			xml.Append ("<South:book type=\"fiction\"> " );
+			xml.Append ("<South:title>Bear and the Dragon</South:title> " );
+			xml.Append ("<South:author>Tom Clancy</South:author> " );
+			xml.Append ("<South:price>6.95</South:price></South:book> " );
+			xml.Append ("<South:book type=\"fiction\"><South:title>Bourne Identity</South:title> " );
+			xml.Append ("<South:author>Robert Ludlum</South:author> " );
+			xml.Append ("<South:price>9.95</South:price></South:book></library>");
+
+			MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
+			document = new XmlDocument ();
+			document.Load (memoryStream);
+			XmlNodeList bookList = document.GetElementsByTagName ("book", "http://www.foo.com");
+			AssertEquals ("GetElementsByTagName (string, uri) returned incorrect count.", 1, bookList.Count);
+		}
+
 		public void TestOuterXmlWithNamespace ()
 		{
 			XmlElement element = document.CreateElement ("foo", "bar", "#foo");