Author: atsushi
Date: 2007-09-27 07:39:15 -0400 (Thu, 27 Sep 2007)
New Revision: 86483
Modified:
trunk/mcs/class/System.XML/System.Xml/ChangeLog
trunk/mcs/class/System.XML/System.Xml/XmlTextWriter2.cs
trunk/mcs/class/System.XML/System.Xml/XmlWriter.cs
trunk/mcs/class/System.XML/Test/System.Xml/ChangeLog
trunk/mcs/class/System.XML/Test/System.Xml/XmlTextWriterTests.cs
trunk/mcs/class/System.XML/Test/System.Xml/XmlWriterTests.cs
Log:
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.
Modified: trunk/mcs/class/System.XML/System.Xml/ChangeLog
===================================================================
--- trunk/mcs/class/System.XML/System.Xml/ChangeLog 2007-09-27 11:38:47 UTC
(rev 86482)
+++ trunk/mcs/class/System.XML/System.Xml/ChangeLog 2007-09-27 11:39:15 UTC
(rev 86483)
@@ -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
Modified: trunk/mcs/class/System.XML/System.Xml/XmlTextWriter2.cs
===================================================================
--- trunk/mcs/class/System.XML/System.Xml/XmlTextWriter2.cs 2007-09-27
11:38:47 UTC (rev 86482)
+++ trunk/mcs/class/System.XML/System.Xml/XmlTextWriter2.cs 2007-09-27
11:39:15 UTC (rev 86483)
@@ -1155,9 +1155,7 @@
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);
Modified: trunk/mcs/class/System.XML/System.Xml/XmlWriter.cs
===================================================================
--- trunk/mcs/class/System.XML/System.Xml/XmlWriter.cs 2007-09-27 11:38:47 UTC
(rev 86482)
+++ trunk/mcs/class/System.XML/System.Xml/XmlWriter.cs 2007-09-27 11:39:15 UTC
(rev 86483)
@@ -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 @@
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);
Modified: trunk/mcs/class/System.XML/Test/System.Xml/ChangeLog
===================================================================
--- trunk/mcs/class/System.XML/Test/System.Xml/ChangeLog 2007-09-27
11:38:47 UTC (rev 86482)
+++ trunk/mcs/class/System.XML/Test/System.Xml/ChangeLog 2007-09-27
11:39:15 UTC (rev 86483)
@@ -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
Modified: trunk/mcs/class/System.XML/Test/System.Xml/XmlTextWriterTests.cs
===================================================================
--- trunk/mcs/class/System.XML/Test/System.Xml/XmlTextWriterTests.cs
2007-09-27 11:38:47 UTC (rev 86482)
+++ trunk/mcs/class/System.XML/Test/System.Xml/XmlTextWriterTests.cs
2007-09-27 11:39:15 UTC (rev 86483)
@@ -1258,6 +1258,15 @@
}
[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 ()
{
Modified: trunk/mcs/class/System.XML/Test/System.Xml/XmlWriterTests.cs
===================================================================
--- trunk/mcs/class/System.XML/Test/System.Xml/XmlWriterTests.cs
2007-09-27 11:38:47 UTC (rev 86482)
+++ trunk/mcs/class/System.XML/Test/System.Xml/XmlWriterTests.cs
2007-09-27 11:39:15 UTC (rev 86483)
@@ -376,6 +376,81 @@
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 (
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches