Browse Source

Namespace declarations are now treated like attributes. Fixed bug in XmlNamespaceManager.HasNamespace. All tests now pass for both Mono and Microsoft.

svn path=/trunk/mcs/; revision=2661
Jason Diamond 24 years ago
parent
commit
6808258fa8

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

@@ -1,3 +1,11 @@
+2002-02-26  Jason Diamond <[email protected]>
+
+	* XmlTextReader.cs: Apparently Microsoft's implementation treats
+	namespace declarations as attributes so we do now, too.
+
+	* XmlNamespaceManager.cs: HasNamespace fixed so that it only
+	checks the current scope.
+
 2002-02-26  Duncan Mak  <[email protected]>
 
 	* XmlDocumentType.cs: Added a few hacks here and there to

+ 3 - 22
mcs/class/System.XML/System.Xml/XmlNamespaceManager.cs

@@ -72,22 +72,7 @@ namespace System.Xml
 
 		public virtual bool HasNamespace(string prefix)
 		{
-			NamespaceScope scope = _Top;
-
-			while (scope != null)
-			{
-				if (scope.Namespaces != null)
-				{
-					if (scope.Namespaces.Contains(prefix))
-					{
-						return true;
-					}
-				}
-
-				scope = scope.Next;
-			}
-
-			return false;
+			return _Top != null && _Top.Namespaces != null && _Top.Namespaces.Contains(prefix);
 		}
 
 		public virtual string LookupNamespace(string prefix)
@@ -96,13 +81,9 @@ namespace System.Xml
 
 			while (scope != null)
 			{
-				if (scope.Namespaces != null)
+				if (scope.Namespaces != null && scope.Namespaces.Contains(prefix))
 				{
-					if (scope.Namespaces.Contains(prefix))
-					{
-						string uri = scope.Namespaces[prefix] as string;
-						return uri;
-					}
+					return scope.Namespaces[prefix] as string;
 				}
 
 				scope = scope.Next;

+ 6 - 4
mcs/class/System.XML/System.Xml/XmlTextReader.cs

@@ -459,6 +459,10 @@ namespace System.Xml
 						}
 					}
 				}
+				else if (localName == "xmlns" && namespaceURI == "http://www.w3.org/2000/xmlns/" && thisName == "xmlns")
+				{
+					return (string)attributes[thisName];
+				}
 			}
 
 			return String.Empty;
@@ -1102,10 +1106,8 @@ namespace System.Xml
 				{
 					namespaceManager.AddNamespace(name.Substring(6), value);
 				}
-				else
-				{
-					AddAttribute(name, value);
-				}
+
+				AddAttribute(name, value);
 			}
 			while (PeekChar() != '/' && PeekChar() != '>' && PeekChar() != -1);
 		}

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

@@ -1,3 +1,11 @@
+2002-02-26  Jason Diamond <[email protected]>
+
+	* XmlTextReaderTests.cs: Test for namespace declarations as
+	attributes.
+
+	* XmlNamespaceManagerTests.cs: Use the newly implemented NameTable
+	when creating the XmlNamespaceManager. Properly test HasNamespace.
+
 2002-02-25  Jason Diamond <[email protected]>
 
 	* XmlDocumentTests.cs: Added file.

+ 5 - 4
mcs/class/System.XML/Test/XmlNamespaceManagerTests.cs

@@ -24,8 +24,8 @@ namespace Ximian.Mono.Tests
 
 		protected override void SetUp()
 		{
-			//XmlNameTable nameTable = new NameTable();
-			_NamespaceManager = new XmlNamespaceManager(null);
+			XmlNameTable nameTable = new NameTable();
+			_NamespaceManager = new XmlNamespaceManager(nameTable);
 		}
 
 		public void TestNewNamespaceManager()
@@ -61,8 +61,9 @@ namespace Ximian.Mono.Tests
 			_NamespaceManager.PushScope();
 			// add a new namespace.
 			_NamespaceManager.AddNamespace("bar", "http://bar/");
-			// make sure the old namespace is still there.
-			Assert(_NamespaceManager.HasNamespace("foo"));
+			// make sure the old namespace is not in this new scope.
+			Assert(!_NamespaceManager.HasNamespace("foo"));
+			// but we're still supposed to be able to lookup the old namespace.
 			AssertEquals("http://foo/", _NamespaceManager.LookupNamespace("foo"));
 			// make sure the new namespace is there.
 			Assert(_NamespaceManager.HasNamespace("bar"));

+ 70 - 7
mcs/class/System.XML/Test/XmlTextReaderTests.cs

@@ -941,7 +941,16 @@ namespace Ximian.Mono.Tests
 				"bar", // localName
 				"http://foo/", // namespaceURI
 				String.Empty, // value
-				0 // attributeCount
+				1 // attributeCount
+			);
+
+			AssertAttribute(
+				xmlReader, // xmlReader
+				"xmlns:foo", // name
+				"xmlns", // prefix
+				"foo", // localName
+				"http://www.w3.org/2000/xmlns/", // namespaceURI
+				"http://foo/" // value
 			);
 
 			AssertEquals("http://foo/", xmlReader.LookupNamespace("foo"));
@@ -967,7 +976,16 @@ namespace Ximian.Mono.Tests
 				"foo", // localName
 				"http://foo/", // namespaceURI
 				String.Empty, // value
-				0 // attributeCount
+				1 // attributeCount
+			);
+
+			AssertAttribute(
+				xmlReader, // xmlReader
+				"xmlns", // name
+				String.Empty, // prefix
+				"xmlns", // localName
+				"http://www.w3.org/2000/xmlns/", // namespaceURI
+				"http://foo/" // value
 			);
 
 			AssertEquals("http://foo/", xmlReader.LookupNamespace(String.Empty));
@@ -993,7 +1011,16 @@ namespace Ximian.Mono.Tests
 				"bar", // localName
 				"http://foo/", // namespaceURI
 				String.Empty, // value
-				0 // attributeCount
+				1 // attributeCount
+			);
+
+			AssertAttribute(
+				xmlReader, // xmlReader
+				"xmlns:foo", // name
+				"xmlns", // prefix
+				"foo", // localName
+				"http://www.w3.org/2000/xmlns/", // namespaceURI
+				"http://foo/" // value
 			);
 
 			AssertEquals("http://foo/", xmlReader.LookupNamespace("foo"));
@@ -1008,7 +1035,16 @@ namespace Ximian.Mono.Tests
 				"quux", // localName
 				"http://baz/", // namespaceURI
 				String.Empty, // value
-				0 // attributeCount
+				1 // attributeCount
+			);
+
+			AssertAttribute(
+				xmlReader, // xmlReader
+				"xmlns:baz", // name
+				"xmlns", // prefix
+				"baz", // localName
+				"http://www.w3.org/2000/xmlns/", // namespaceURI
+				"http://baz/" // value
 			);
 
 			AssertEquals("http://foo/", xmlReader.LookupNamespace("foo"));
@@ -1051,7 +1087,16 @@ namespace Ximian.Mono.Tests
 				"bar", // localName
 				"http://foo/", // namespaceURI
 				String.Empty, // value
-				0 // attributeCount
+				1 // attributeCount
+			);
+
+			AssertAttribute(
+				xmlReader, // xmlReader
+				"xmlns:foo", // name
+				"xmlns", // prefix
+				"foo", // localName
+				"http://www.w3.org/2000/xmlns/", // namespaceURI
+				"http://foo/" // value
 			);
 
 			AssertEquals("http://foo/", xmlReader.LookupNamespace("foo"));
@@ -1066,7 +1111,16 @@ namespace Ximian.Mono.Tests
 				"baz", // localName
 				"http://baz/", // namespaceURI
 				String.Empty, // value
-				0 // attributeCount
+				1 // attributeCount
+			);
+
+			AssertAttribute(
+				xmlReader, // xmlReader
+				"xmlns", // name
+				String.Empty, // prefix
+				"xmlns", // localName
+				"http://www.w3.org/2000/xmlns/", // namespaceURI
+				"http://baz/" // value
 			);
 
 			AssertEquals("http://foo/", xmlReader.LookupNamespace("foo"));
@@ -1108,7 +1162,7 @@ namespace Ximian.Mono.Tests
 				"foo", // localName
 				String.Empty, // namespaceURI
 				String.Empty, // value
-				1 // attributeCount
+				2 // attributeCount
 			);
 
 			AssertAttribute(
@@ -1120,6 +1174,15 @@ namespace Ximian.Mono.Tests
 				"quux" // value
 			);
 
+			AssertAttribute(
+				xmlReader, // xmlReader
+				"xmlns:bar", // name
+				"xmlns", // prefix
+				"bar", // localName
+				"http://www.w3.org/2000/xmlns/", // namespaceURI
+				"http://bar/" // value
+			);
+
 			AssertEquals("http://bar/", xmlReader.LookupNamespace("bar"));
 
 			AssertEndDocument(xmlReader);