Bläddra i källkod

XmlTextWriter impls for LookupPrefix, WriteBase64, and WriteCharEntity.

svn path=/trunk/mcs/; revision=3527
Kral Ferch 24 år sedan
förälder
incheckning
cbe4a80d16

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

@@ -1,3 +1,12 @@
+2002-03-31  Kral Ferch  <[email protected]>
+
+	* XmlTextWriter.cs: Impls for LookupPrefix, WriteBase64,
+	and WriteCharEntity.
+	
+	* XmlWrite.cs:  Fixed bug where attribute namespace decl
+	was pushing a scope onto the namespace manager when it shouldn't
+	have been.
+	
 2002-03-31  Kral Ferch  <[email protected]>
 
 	* XmlTextWriter.cs: Some tweaks for WriteAttibuteString

+ 18 - 11
mcs/class/System.XML/System.Xml/XmlTextWriter.cs

@@ -222,10 +222,14 @@ namespace System.Xml
 			w.Flush ();
 		}
 
-		[MonoTODO]
 		public override string LookupPrefix (string ns)
 		{
-			throw new NotImplementedException ();
+			string prefix = namespaceManager.LookupPrefix (ns);
+
+			if (prefix == String.Empty)
+				prefix = null;
+
+			return prefix;
 		}
 
 		private void UpdateIndentChars ()
@@ -235,10 +239,9 @@ namespace System.Xml
 				indentChars += indentChar;
 		}
 
-		[MonoTODO]
 		public override void WriteBase64 (byte[] buffer, int index, int count)
 		{
-			throw new NotImplementedException ();
+			w.Write (Convert.ToBase64String (buffer, index, count));
 		}
 
 		[MonoTODO]
@@ -249,10 +252,8 @@ namespace System.Xml
 
 		public override void WriteCData (string text)
 		{
-			if (text.IndexOf("]]>") > 0) 
-			{
+			if (text.IndexOf("]]>") > 0)
 				throw new ArgumentException ();
-			}
 
 			CheckState ();
 			CloseStartElement ();
@@ -260,10 +261,16 @@ namespace System.Xml
 			w.Write("<![CDATA[{0}]]>", text);
 		}
 
-		[MonoTODO]
 		public override void WriteCharEntity (char ch)
 		{
-			throw new NotImplementedException ();
+			Int16	intCh = (Int16)ch;
+
+			// Make sure the character is not in the surrogate pair
+			// character range, 0xd800- 0xdfff
+			if ((intCh >= -10240) && (intCh <= -8193))
+				throw new ArgumentException ("Surrogate Pair is invalid.");
+
+			w.Write("&#x{0:X};", intCh);
 		}
 
 		[MonoTODO]
@@ -337,8 +344,9 @@ namespace System.Xml
 			}
 			else {
 				w.Write ("{0}</{1}>", indentFormatting, openElements.Pop ());
-				namespaceManager.PopScope();
 			}
+
+			namespaceManager.PopScope();
 		}
 
 		[MonoTODO]
@@ -395,7 +403,6 @@ namespace System.Xml
 			throw new NotImplementedException ();
 		}
 
-		[MonoTODO("haven't tested namespaces on attributes code yet.")]
 		public override void WriteStartAttribute (string prefix, string localName, string ns)
 		{
 			if ((prefix == "xml") && (localName == "lang"))

+ 0 - 2
mcs/class/System.XML/System.Xml/XmlWriter.cs

@@ -71,8 +71,6 @@ namespace System.Xml
 
 			if ((prefix == "xmlns") || (localName == "xmlns")) 
 			{
-				namespaceManager.PushScope ();
-
 				if (prefix == "xmlns")
 					namespaceManager.AddNamespace (localName, ns);
 				else

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

@@ -1,3 +1,8 @@
+2002-03-31  Kral Ferch <[email protected]>
+
+	* XmlTextWriterTests.cs: Tests for LookupPrefix, WriteBase64,
+	and WriteCharEntity.
+
 2002-03-31  Kral Ferch <[email protected]>
 
 	* XmlTextWriterTests.cs: Finished XmlLang, XmlSpace, and

+ 1 - 1
mcs/class/System.XML/Test/MonoMicro.Test.csproj

@@ -70,7 +70,7 @@
                 <Reference
                     Name = "System.XML"
                     AssemblyName = "System.XML"
-                    HintPath = "..\obj\Debug\System.XML.dll"
+                    HintPath = "..\..\..\..\..\WINDOWS\Microsoft.NET\Framework\v1.0.3705\System.XML.dll"
                 />
             </References>
         </Build>

+ 104 - 32
mcs/class/System.XML/Test/XmlTextWriterTests.cs

@@ -31,16 +31,8 @@ namespace Ximian.Mono.Tests
 			xtw.QuoteChar = '\'';
 		}
 
-		public void TestAttributeNamespacesXmlnsXmlns ()
+		private string StringWriterText 
 		{
-			xtw.WriteStartElement ("foo");
-			try {
-				xtw.WriteAttributeString ("xmlns", "xmlns", null, "http://abc.def");
-				Fail ("Expected an ArgumentException to be thrown.");
-			} catch (ArgumentException) {}
-		}
-
-		private string StringWriterText {
 			get { return sw.GetStringBuilder ().ToString (); }
 		}
 
@@ -61,11 +53,10 @@ namespace Ximian.Mono.Tests
 			AssertEquals ("<foo xmlns:abc='http://abc.def' bar='baz'", StringWriterText);
 		}
 
-		public void TestAttributeNamespacesWithTextInNamespaceParam ()
+		public void TestAttributeNamespacesThreeParamWithNullInNamespaceParam ()
 		{
-			try {
-				xtw.WriteAttributeString ("xmlns", "abc", "http://somenamespace.com", "http://abc.def");
-			} catch (ArgumentException) {}
+			xtw.WriteAttributeString ("xmlns", null, "http://abc.def");
+			AssertEquals ("xmlns='http://abc.def'", StringWriterText);
 		}
 
 		public void TestAttributeNamespacesThreeParamWithTextInNamespaceParam ()
@@ -83,30 +74,22 @@ namespace Ximian.Mono.Tests
 			AssertEquals ("xmlns:abc='http://abc.def'", StringWriterText);
 		}
 
-		public void TestAttributeNamespacesThreeParamWithNullInNamespaceParam ()
-		{
-			xtw.WriteAttributeString ("xmlns", null, "http://abc.def");
-			AssertEquals ("xmlns='http://abc.def'", StringWriterText);
-		}
-
-		public void TestAttributeWriteAttributeStringWithoutParentElement ()
+		public void TestAttributeNamespacesWithTextInNamespaceParam ()
 		{
-			xtw.WriteAttributeString ("foo", "bar");
-			AssertEquals ("foo='bar'", StringWriterText);
-
-			xtw.WriteAttributeString ("baz", "quux");
-			AssertEquals ("foo='bar' baz='quux'", StringWriterText);
+			try {
+				xtw.WriteAttributeString ("xmlns", "abc", "http://somenamespace.com", "http://abc.def");
+			} catch (ArgumentException) {}
 		}
 
-		public void TestAttributeWriteAttributeStringNotInsideOpenStartElement ()
+		public void TestAttributeNamespacesXmlnsXmlns ()
 		{
 			xtw.WriteStartElement ("foo");
-			xtw.WriteString ("bar");
-			
-			try {
-				xtw.WriteAttributeString ("baz", "quux");
-				Fail ("Expected an InvalidOperationException to be thrown.");
-			} catch (InvalidOperationException) {}
+			try 
+			{
+				xtw.WriteAttributeString ("xmlns", "xmlns", null, "http://abc.def");
+				Fail ("Expected an ArgumentException to be thrown.");
+			} 
+			catch (ArgumentException) {}
 		}
 
 		public void TestAttributeWriteAttributeString ()
@@ -131,6 +114,28 @@ namespace Ximian.Mono.Tests
 			AssertEquals ("<foo foo='bar' bar='' baz='' ='quux' ='quuux'", StringWriterText);
 		}
 
+		public void TestAttributeWriteAttributeStringNotInsideOpenStartElement ()
+		{
+			xtw.WriteStartElement ("foo");
+			xtw.WriteString ("bar");
+			
+			try 
+			{
+				xtw.WriteAttributeString ("baz", "quux");
+				Fail ("Expected an InvalidOperationException to be thrown.");
+			} 
+			catch (InvalidOperationException) {}
+		}
+
+		public void TestAttributeWriteAttributeStringWithoutParentElement ()
+		{
+			xtw.WriteAttributeString ("foo", "bar");
+			AssertEquals ("foo='bar'", StringWriterText);
+
+			xtw.WriteAttributeString ("baz", "quux");
+			AssertEquals ("foo='bar' baz='quux'", StringWriterText);
+		}
+
 		public void TestCDataValid ()
 		{
 			xtw.WriteCData ("foo");
@@ -375,6 +380,26 @@ namespace Ximian.Mono.Tests
 			AssertEquals ("<ol>\r\n  <li>The big <b>E</b><i>lephant</i> walks slowly.</li>\r\n</ol>", StringWriterText);
 		}
 
+		public void TestLookupPrefix ()
+		{
+			xtw.WriteStartElement ("root");
+
+			xtw.WriteStartElement ("one");
+			xtw.WriteAttributeString ("xmlns", "foo", null, "http://abc.def");
+			xtw.WriteAttributeString ("xmlns", "bar", null, "http://ghi.jkl");
+			AssertEquals ("foo", xtw.LookupPrefix ("http://abc.def"));
+			AssertEquals ("bar", xtw.LookupPrefix ("http://ghi.jkl"));
+			xtw.WriteEndElement ();
+
+			xtw.WriteStartElement ("two");
+			xtw.WriteAttributeString ("xmlns", "baz", null, "http://mno.pqr");
+			xtw.WriteString("quux");
+			AssertEquals ("baz", xtw.LookupPrefix ("http://mno.pqr"));
+			AssertNull (xtw.LookupPrefix ("http://abc.def"));
+			AssertNull (xtw.LookupPrefix ("http://ghi.jkl"));
+
+			AssertNull (xtw.LookupPrefix ("http://bogus"));
+		}
 
 		public void TestNamespacesAttributesPassingInNamespaces ()
 		{
@@ -554,6 +579,53 @@ namespace Ximian.Mono.Tests
 			} catch (ArgumentException) {}
 		}
 
+		public void TestWriteBase64 ()
+		{
+			UTF8Encoding encoding = new UTF8Encoding();
+			byte[] fooBar = encoding.GetBytes("foobar");
+			xtw.WriteBase64 (fooBar, 0, 6);
+			AssertEquals("Zm9vYmFy", StringWriterText);
+
+			try {
+				xtw.WriteBase64 (fooBar, 3, 6);
+				Fail ("Expected an Argument Exception to be thrown.");
+			} catch (ArgumentException) {}
+
+			try {
+				xtw.WriteBase64 (fooBar, -1, 6);
+				Fail ("Expected an Argument Exception to be thrown.");
+			} catch (ArgumentOutOfRangeException) {}
+
+			try {
+				xtw.WriteBase64 (fooBar, 3, -1);
+				Fail ("Expected an Argument Exception to be thrown.");
+			} catch (ArgumentOutOfRangeException) {}
+
+			try {
+				xtw.WriteBase64 (null, 0, 6);
+				Fail ("Expected an Argument Exception to be thrown.");
+			} catch (ArgumentNullException) {}
+		}
+
+		public void TestWriteCharEntity ()
+		{
+			xtw.WriteCharEntity ('a');
+			AssertEquals ("&#x61;", StringWriterText);
+
+			xtw.WriteCharEntity ('A');
+			AssertEquals ("&#x61;&#x41;", StringWriterText);
+
+			xtw.WriteCharEntity ('1');
+			AssertEquals ("&#x61;&#x41;&#x31;", StringWriterText);
+
+			xtw.WriteCharEntity ('K');
+			AssertEquals ("&#x61;&#x41;&#x31;&#x4B;", StringWriterText);
+
+			try {
+				xtw.WriteCharEntity ((char)0xd800);
+			} catch (ArgumentException) {}
+		}
+
 		public void TestWriteEndAttribute ()
 		{
 			try