Browse Source

2007-09-27 Atsushi Enomoto <[email protected]>

	* XmlWriter.cs : implemented WriteValue(object) based on
	  MSDN "Writing Typed Data" with some fix.
	  http://msdn2.microsoft.com/en-us/library/bft97s8e(VS.80).aspx
	* XmlTextWriter.cs : state check is extraneous. It caused unexpected
	  rejection of empty namespace URI.

	* XmlWriterTests.cs : added test for WriteValue(object).
	* XmlTextWriterTests.cs : added WriteQualifiedName() test with empty
	  namespace.


svn path=/trunk/mcs/; revision=86483
Atsushi Eno 18 years ago
parent
commit
28bfedcf61

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

@@ -1,3 +1,11 @@
+2007-09-27  Atsushi Enomoto  <[email protected]>
+
+	* XmlWriter.cs : implemented WriteValue(object) based on
+	  MSDN "Writing Typed Data" with some fix.
+	  http://msdn2.microsoft.com/en-us/library/bft97s8e(VS.80).aspx
+	* XmlTextWriter.cs : state check is extraneous. It caused unexpected
+	  rejection of empty namespace URI.
+
 2007-08-13  Atsushi Enomoto  <[email protected]>
 
 	* XmlTextWriter2.cs : fix extraneous indentation which is put after

+ 1 - 3
mcs/class/System.XML/System.Xml/XmlTextWriter2.cs

@@ -1155,9 +1155,7 @@ namespace Mono.Xml
 
 			ShiftStateContent ("QName", true);
 
-			string prefix =
-				state == WriteState.Content || ns.Length > 0 ?
-				LookupPrefix (ns) : String.Empty;
+			string prefix = ns.Length > 0 ? LookupPrefix (ns) : String.Empty;
 			if (prefix == null) {
 				if (state == WriteState.Attribute)
 					prefix = MockupPrefix (ns, false);

+ 35 - 11
mcs/class/System.XML/System.Xml/XmlWriter.cs

@@ -7,6 +7,7 @@
 //
 // (C) 2002 Kral Ferch
 // (C) 2002-2003 Atsushi Enomoto
+// (C) 2004-2007 Novell, Inc.
 //
 
 //
@@ -31,6 +32,7 @@
 //
 
 using System;
+using System.Collections;
 using System.IO;
 using System.Text;
 #if NET_2_0 && !NET_2_1
@@ -597,74 +599,96 @@ namespace System.Xml
 		public abstract void WriteWhitespace (string ws);
 
 #if NET_2_0
-		[MonoTODO]
 		public virtual void WriteValue (bool value)
 		{
 			WriteString (XQueryConvert.BooleanToString (value));
 		}
 
-		[MonoTODO]
 		public virtual void WriteValue (DateTime value)
 		{
 			WriteString (XmlConvert.ToString (value));
 		}
 
-		[MonoTODO]
 		public virtual void WriteValue (decimal value)
 		{
 			WriteString (XQueryConvert.DecimalToString (value));
 		}
 
-		[MonoTODO]
 		public virtual void WriteValue (double value)
 		{
 			WriteString (XQueryConvert.DoubleToString (value));
 		}
 
-		[MonoTODO]
 		public virtual void WriteValue (int value)
 		{
 			WriteString (XQueryConvert.IntToString (value));
 		}
 
-		[MonoTODO]
 		public virtual void WriteValue (long value)
 		{
 			WriteString (XQueryConvert.IntegerToString (value));
 		}
 
-		[MonoTODO]
 		public virtual void WriteValue (object value)
 		{
+			if (value == null)
+				throw new ArgumentNullException ("value");
+
 			if (value is string)
 				WriteString ((string) value);
 			else if (value is bool)
 				WriteValue ((bool) value);
+			else if (value is byte)
+				WriteValue ((int) value);
+			else if (value is byte [])
+				WriteBase64 ((byte []) value, 0, ((byte []) value).Length);
+			else if (value is char [])
+				WriteChars ((char []) value, 0, ((char []) value).Length);
 			else if (value is DateTime)
 				WriteValue ((DateTime) value);
 			else if (value is decimal)
 				WriteValue ((decimal) value);
 			else if (value is double)
 				WriteValue ((double) value);
+			else if (value is short)
+				WriteValue ((int) value);
 			else if (value is int)
 				WriteValue ((int) value);
 			else if (value is long)
 				WriteValue ((long) value);
+			else if (value is float)
+				WriteValue ((float) value);
+			else if (value is TimeSpan) // undocumented
+				WriteString (XmlConvert.ToString ((TimeSpan) value));
 			else if (value is XmlQualifiedName) {
 				XmlQualifiedName qname = (XmlQualifiedName) value;
-				WriteQualifiedName (qname.Name, qname.Namespace);
+				if (!qname.Equals (XmlQualifiedName.Empty)) {
+					if (qname.Namespace.Length > 0 && LookupPrefix (qname.Namespace) == null)
+						throw new InvalidCastException (String.Format ("The QName '{0}' cannot be written. No corresponding prefix is declared", qname));
+					WriteQualifiedName (qname.Name, qname.Namespace);
+				}
+				else
+					WriteString (String.Empty);
+			}
+			else if (value is IEnumerable) {
+				bool follow = false;
+				foreach (object obj in (IEnumerable) value) {
+					if (follow)
+						WriteString (" ");
+					else
+						follow = true;
+					WriteValue (obj);
+				}
 			}
 			else
-				throw new NotImplementedException ("Argument value is " + value);
+				throw new InvalidCastException (String.Format ("Type '{0}' cannot be cast to string", value.GetType ()));
 		}
 
-		[MonoTODO]
 		public virtual void WriteValue (float value)
 		{
 			WriteString (XQueryConvert.FloatToString (value));
 		}
 
-		[MonoTODO]
 		public virtual void WriteValue (string value)
 		{
 			WriteString (value);

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

@@ -1,3 +1,9 @@
+2007-09-27  Atsushi Enomoto  <[email protected]>
+
+	* XmlWriterTests.cs : added test for WriteValue(object).
+	* XmlTextWriterTests.cs : added WriteQualifiedName() test with empty
+	  namespace.
+
 2007-08-13  Atsushi Enomoto  <[email protected]>
 
 	* XmlWriterSettingsTests.cs : added test for extraneous newline on

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

@@ -1257,6 +1257,15 @@ namespace MonoTests.System.Xml
 			Assert.IsTrue (xml.IndexOf (" xmlns:d1p2='urn:b'") > 0, "xmlns:b");
 		}
 
+		[Test]
+		public void WriteQualifiedNameNonNamespacedName ()
+		{
+			xtw.WriteStartElement ("root");
+			xtw.WriteQualifiedName ("foo", "");
+			xtw.WriteEndElement ();
+			Assert.AreEqual ("<root>foo</root>", StringWriterText);
+		}
+
 		[Test]
 		[ExpectedException (typeof (ArgumentException))]
 		public void WriteQualifiedNameNonDeclaredContent ()

+ 75 - 0
mcs/class/System.XML/Test/System.Xml/XmlWriterTests.cs

@@ -376,6 +376,81 @@ namespace MonoTests.System.Xml
 			Assert.IsFalse (ms.CanWrite, "#B3");
 		}
 
+		XmlWriter CreateWriter (TextWriter tw)
+		{
+			XmlWriterSettings s = new XmlWriterSettings ();
+			s.OmitXmlDeclaration = true;
+			XmlWriter w = XmlWriter.Create (tw, s);
+			w.WriteStartElement ("root");
+			return w;
+		}
+
+		[Test]
+		[ExpectedException (typeof (ArgumentNullException))]
+		public void WriteValueNull ()
+		{
+			XmlWriter w = CreateWriter (TextWriter.Null);
+			w.WriteValue ((object) null);
+		}
+
+		[Test]
+		[ExpectedException (typeof (InvalidCastException))] // it throws somewhat funny exception
+		public void WriteValueNonExistentQName ()
+		{
+			XmlWriter w = CreateWriter (TextWriter.Null);
+			w.WriteValue (new XmlQualifiedName ("foo", "urn:foo"));
+		}
+
+		[Test]
+		public void WriteValueEmptyQName ()
+		{
+			StringWriter sw = new StringWriter ();
+			XmlWriter w = CreateWriter (sw);
+			w.WriteValue (XmlQualifiedName.Empty);
+			w.Close ();
+		}
+
+		[Test]
+		public void WriteValueQName ()
+		{
+			StringWriter sw = new StringWriter ();
+			XmlWriter w = CreateWriter (sw);
+			w.WriteAttributeString ("xmlns", "x", "http://www.w3.org/2000/xmlns/", "urn:foo");
+			w.WriteValue (new XmlQualifiedName ("foo", "urn:foo"));
+			w.Close ();
+			Assert.AreEqual ("<root xmlns:x=\"urn:foo\">x:foo</root>", sw.ToString ());
+		}
+
+		[Test]
+		public void WriteValueTimeSpan ()
+		{
+			StringWriter sw = new StringWriter ();
+			XmlWriter w = CreateWriter (sw);
+			w.WriteValue (TimeSpan.FromSeconds (5));
+			w.Close ();
+			Assert.AreEqual ("<root>PT5S</root>", sw.ToString ());
+		}
+
+		[Test]
+		public void WriteValueArray ()
+		{
+			StringWriter sw = new StringWriter ();
+			XmlWriter w = CreateWriter (sw);
+			w.WriteValue (new int [] {1, 2, 3});
+			w.WriteValue (new int [] {4, 5, 6});
+			w.Close ();
+			Assert.AreEqual ("<root>1 2 34 5 6</root>", sw.ToString ());
+		}
+
+		[Test]
+		[ExpectedException (typeof (InvalidCastException))] // it throws somewhat funny exception
+		public void WriteValueTextReader ()
+		{
+			// it is documented as supported, but actually isn't.
+			XmlWriter w = CreateWriter (TextWriter.Null);
+			w.WriteValue (new StringReader ("foobar"));
+		}
+
 		XPathNavigator GetNavigator (string xml)
 		{
 			return new XPathDocument (XmlReader.Create (