Browse Source

* XmlTextWriter.cs: Modified WriteWhitespace to throw ArgumentException
if value is null or zero-length string. Modified WriteNmToken to throw
ArgumentException if name is null or zero-length string. Cosmetic
change to WriteStringInternal.
* XmlElement.cs: In 2.0 profile, do not throw ArgumentNullException
if new value for Prefix is null.
* XmlElementTests.cs: Improved tests for setting prefix to null
or zero-length string. On 2.0 profile, setting prefix to null should
not result in ArgumentNullException.
* XmlTextWriterTests.cs: Enabled WriteNmToken tests and
WriteWhitespace tests for null or zero-length value.

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

Gert Driesen 20 years ago
parent
commit
cd7644c997

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

@@ -1,3 +1,12 @@
+2006-01-05  Gert Driesen <[email protected]>
+
+	* XmlTextWriter.cs: Modified WriteWhitespace to throw ArgumentException
+	if value is null or zero-length string. Modified WriteNmToken to throw
+	ArgumentException if name is null or zero-length string. Cosmetic
+	change to WriteStringInternal.
+	* XmlElement.cs: In 2.0 profile, do not throw ArgumentNullException
+	if new value for Prefix is null.
+
 2005-12-26  Atsushi Enomoto <[email protected]>
 
 	* XmlTextWriter.cs : when namespaceURI is String.Empty, Prefix

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

@@ -216,8 +216,13 @@ namespace System.Xml
 			set {
 				if (IsReadOnly)
 					throw new ArgumentException ("This node is readonly.");
-				if (value == null)
-					throw new ArgumentNullException("Prefix value is null.");
+				if (value == null) {
+#if NET_2_0
+					value = string.Empty;
+#else
+					throw new ArgumentNullException ("Prefix value is null.");
+#endif
+				}
 				if ((!String.Empty.Equals(value))&&(!XmlChar.IsNCName (value)))
 					throw new ArgumentException ("Specified name is not a valid NCName: " + value);
 

+ 33 - 28
mcs/class/System.XML/System.Xml/XmlTextWriter.cs

@@ -683,6 +683,9 @@ openElements [openElementCount - 1]).IndentingOverriden;
 
 		public override void WriteNmToken (string name)
 		{
+			if (name == null || name.Length == 0)
+				throw ArgumentError ("The Name cannot be empty.");
+
 			WriteNmTokenInternal (name);
 		}
 
@@ -1077,40 +1080,38 @@ openElements [openElementCount - 1]).IndentingOverriden;
 
 		private void WriteStringInternal (string text, bool entitize)
 		{
-			if (text == null)
-				text = String.Empty;
-
-			if (text != String.Empty) {
-				CheckState ();
+			if (text == null || text.Length == 0)
+				return;
+			
+			CheckState ();
 
-				if (entitize)
-					text = EscapeString (text, !openAttribute);
+			if (entitize)
+				text = EscapeString (text, !openAttribute);
 
-				if (!openAttribute)
-				{
-					IndentingOverriden = true;
-					CloseStartElement ();
-				}
+			if (!openAttribute)
+			{
+				IndentingOverriden = true;
+				CloseStartElement ();
+			}
 
-				if (!openXmlLang && !openXmlSpace)
-					w.Write (text);
+			if (!openXmlLang && !openXmlSpace)
+				w.Write (text);
+			else 
+			{
+				if (openXmlLang)
+					xmlLang = text;
 				else 
 				{
-					if (openXmlLang)
-						xmlLang = text;
-					else 
+					switch (text) 
 					{
-						switch (text) 
-						{
-							case "default":
-								xmlSpace = XmlSpace.Default;
-								break;
-							case "preserve":
-								xmlSpace = XmlSpace.Preserve;
-								break;
-							default:
-								throw ArgumentError ("'{0}' is an invalid xml:space value.");
-						}
+						case "default":
+							xmlSpace = XmlSpace.Default;
+							break;
+						case "preserve":
+							xmlSpace = XmlSpace.Preserve;
+							break;
+						default:
+							throw ArgumentError ("'{0}' is an invalid xml:space value.");
 					}
 				}
 			}
@@ -1136,6 +1137,10 @@ openElements [openElementCount - 1]).IndentingOverriden;
 
 		public override void WriteWhitespace (string ws)
 		{
+			if (ws == null || ws.Length == 0) {
+				throw ArgumentError ("Only white space characters should be used.");
+			}
+
 			if (!XmlChar.IsWhitespace (ws))
 				throw ArgumentError ("Invalid Whitespace");
 

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

@@ -1,3 +1,11 @@
+2006-01-05  Gert Driesen  <[email protected]>
+
+	* XmlElementTests.cs: Improved tests for setting prefix to null
+	or zero-length string. On 2.0 profile, setting prefix to null should
+	not result in ArgumentNullException.
+	* XmlTextWriterTests.cs: Enabled WriteNmToken tests and 
+	WriteWhitespace tests for null or zero-length value.
+
 2006-01-05  Atsushi Enomoto  <[email protected]>
 
 	* XmlTextWriterTests.cs : removed silly part from

+ 29 - 1
mcs/class/System.XML/Test/System.Xml/XmlElementTests.cs

@@ -561,20 +561,48 @@ namespace MonoTests.System.Xml
 		}
 
 		[Test]
+#if ONLY_1_1
 		[ExpectedException (typeof (ArgumentNullException))]
+#endif
 		public void SetNullPrefix ()
 		{
 			XmlDocument doc = new XmlDocument ();
 			doc.LoadXml ("<root/>");
 			doc.DocumentElement.Prefix = null;
+
+#if NET_2_0
+			AssertEquals ("#1", string.Empty, doc.DocumentElement.Prefix);
+			AssertClearPrefix ((string) null);
+#endif
 		}
 
 		[Test]
 		public void SetEmptyStringPrefix ()
 		{
 			XmlDocument doc = new XmlDocument ();
-			doc.LoadXml ("<root/>");
+			doc.LoadXml ("<root />");
 			doc.DocumentElement.Prefix = String.Empty;
+			AssertEquals ("#1", string.Empty, doc.DocumentElement.Prefix);
+
+			AssertClearPrefix (string.Empty);
+
+		}
+
+		private void AssertClearPrefix (string newPrefix)
+		{
+			XmlDocument doc = new XmlDocument ();
+			doc.LoadXml ("<x:root xmlns:x=\"http://somenamespace.com\" />");
+			AssertEquals ("#Clear1", "<x:root xmlns:x=\"http://somenamespace.com\" />",
+				doc.OuterXml);
+			AssertEquals ("#Clear2", "<x:root xmlns:x=\"http://somenamespace.com\" />",
+				doc.DocumentElement.OuterXml);
+			AssertEquals ("#Clear3", "x", doc.DocumentElement.Prefix);
+			doc.DocumentElement.Prefix = newPrefix;
+			AssertEquals ("#Clear4", "<root xmlns:x=\"http://somenamespace.com\" xmlns=\"http://somenamespace.com\" />",
+				doc.OuterXml);
+			AssertEquals ("#Clear5", "<root xmlns:x=\"http://somenamespace.com\" xmlns=\"http://somenamespace.com\" />",
+				doc.DocumentElement.OuterXml);
+			AssertEquals ("#Clear6", string.Empty, doc.DocumentElement.Prefix);
 		}
 	}
 }

+ 0 - 4
mcs/class/System.XML/Test/System.Xml/XmlTextWriterTests.cs

@@ -1508,7 +1508,6 @@ namespace MonoTests.System.Xml
 		}
 
 		[Test]
-		[Category ("NotWorking")]
 		[ExpectedException (typeof (ArgumentException))]
 		public void WriteWhitespace_Null ()
 		{
@@ -1516,7 +1515,6 @@ namespace MonoTests.System.Xml
 		}
 
 		[Test]
-		[Category ("NotWorking")]
 		[ExpectedException (typeof (ArgumentException))]
 		public void WriteWhitespace_Empty ()
 		{
@@ -1524,7 +1522,6 @@ namespace MonoTests.System.Xml
 		}
 
 		[Test]
-		[Category ("NotWorking")]
 		[ExpectedException (typeof (ArgumentException))]
 		public void WriteNmToken_Null ()
 		{
@@ -1532,7 +1529,6 @@ namespace MonoTests.System.Xml
 		}
 
 		[Test]
-		[Category ("NotWorking")]
 		[ExpectedException (typeof (ArgumentException))]
 		public void WriteNmToken_Empty ()
 		{