Просмотр исходного кода

2006-12-04 Atsushi Enomoto <[email protected]>

	* SerializationCodeGenerator.cs, XmlCustomFormatter.cs :
	  GenerateToXmlString() needed hexBinary support as well. Since
	  XmlConvert.[To|From]BinHexString() are internal, use reflection.
	  Really fixed bug #79989 and #79990 for generated serializers.

	* XmlSerializerTests.cs :
	  added base64 tests which is similar to hexBinary.


svn path=/trunk/mcs/; revision=68927
Atsushi Eno 19 лет назад
Родитель
Сommit
4b62022259

+ 7 - 0
mcs/class/System.XML/System.Xml.Serialization/ChangeLog

@@ -1,3 +1,10 @@
+2006-12-04  Atsushi Enomoto  <[email protected]>
+
+	* SerializationCodeGenerator.cs, XmlCustomFormatter.cs :
+	  GenerateToXmlString() needed hexBinary support as well. Since
+	  XmlConvert.[To|From]BinHexString() are internal, use reflection.
+	  Really fixed bug #79989 and #79990 for generated serializers.
+
 2006-12-03  Gert Driesen  <[email protected]>
 
 	* XmlSchemaExporter.cs: Emit xml name of enum-based default values.

+ 12 - 0
mcs/class/System.XML/System.Xml.Serialization/SerializationCodeGenerator.cs

@@ -454,6 +454,12 @@ namespace System.Xml.Serialization
 				WriteLine ("internal class " + writerClassName + " : XmlSerializationWriter");
 			WriteLineInd ("{");
 			WriteLine ("const string xmlNamespace = \"http://www.w3.org/2000/xmlns/\";");
+			// ToBinHexString() is not public, so use reflection here.
+			WriteLine ("static readonly System.Reflection.MethodInfo toBinHexStringMethod = typeof (XmlConvert).GetMethod (\"ToBinHexString\", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic, null, new Type [] {typeof (byte [])}, null);");
+			WriteLine ("static string ToBinHexString (byte [] input)");
+			WriteLineInd ("{");
+			WriteLine ("return input == null ? null : (string) toBinHexStringMethod.Invoke (null, new object [] {input});");
+			WriteLineUni ("}");
 			
 			for (int n=0; n<maps.Count; n++)
 			{
@@ -1284,6 +1290,12 @@ namespace System.Xml.Serialization
 			else
 				WriteLine ("internal class " + readerClassName + " : XmlSerializationReader");
 			WriteLineInd ("{");
+			// FromBinHexString() is not public, so use reflection here.
+			WriteLine ("static readonly System.Reflection.MethodInfo fromBinHexStringMethod = typeof (XmlConvert).GetMethod (\"FromBinHexString\", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic, null, new Type [] {typeof (string)}, null);");
+			WriteLine ("static byte [] FromBinHexString (string input)");
+			WriteLineInd ("{");
+			WriteLine ("return input == null ? null : (byte []) fromBinHexStringMethod.Invoke (null, new object [] {input});");
+			WriteLineUni ("}");
 
 			_mapsToGenerate = new ArrayList ();
 			_fixupCallbacks = new ArrayList ();

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

@@ -305,6 +305,7 @@ namespace System.Xml.Serialization {
 				case "guid": return "XmlConvert.ToString (" + value + ")";
 				case "base64":
 				case "base64Binary": return value + " == null ? String.Empty : Convert.ToBase64String (" + value + ")";
+				case "hexBinary": return value + " == null ? String.Empty : ToBinHexString (" + value + ")";
 				case "duration": return "XmlConvert.ToString (" + value + ")";
 				case "NMTOKEN":
 				case "Name":
@@ -344,6 +345,7 @@ namespace System.Xml.Serialization {
 				case "guid": return "XmlConvert.ToGuid (" + value + ")";
 				case "base64:":
 				case "base64Binary": return "Convert.FromBase64String (" + value + ")";
+				case "hexBinary": return "FromBinHexString (" + value + ")";
 				case "duration": return "XmlConvert.ToTimeSpan (" + value + ")";
 				default: return value;
 			}

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

@@ -1,3 +1,8 @@
+2006-12-04  Atsushi Enomoto  <[email protected]>
+
+	* XmlSerializerTests.cs :
+	  added base64 tests which is similar to hexBinary.
+
 2006-12-03  Gert Driesen  <[email protected]>
 
 	* XmlSerializerTests.cs: Undo some of Hagit's code formatting changes.

+ 21 - 0
mcs/class/System.XML/Test/System.Xml.Serialization/XmlSerializerTests.cs

@@ -2152,6 +2152,20 @@ namespace MonoTests.System.XmlSerialization
 		}
 #endif
 
+		[Test]
+		public void SerializeBase64Binary()
+		{
+			XmlSerializer ser = new XmlSerializer (typeof (Base64Binary));
+			sw = new StringWriter ();
+			XmlTextWriter xtw = new XmlTextWriter (sw);
+			ser.Serialize (xtw, new Base64Binary ());
+			xtw.Close ();
+			string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><Base64Binary xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" Data=""AQID"" />";
+			Assert.AreEqual (Infoset (expected), WriterText);
+			Base64Binary h = (Base64Binary) ser.Deserialize (new StringReader (sw.ToString ()));
+			Assert.AreEqual (new byte [] {1, 2, 3}, h.Data);
+		}
+
 		[Test] // bug #79989, #79990
 		public void SerializeHexBinary ()
 		{
@@ -2582,6 +2596,13 @@ namespace MonoTests.System.XmlSerialization
 			public string[] Whee = new string[] { "foo", "bar" };
 		}
 
+		[XmlRoot ("Base64Binary")]
+		public class Base64Binary
+		{
+			[XmlAttribute (DataType = "base64Binary")]
+			public byte [] Data = new byte [] {1, 2, 3};
+		}
+
 		[XmlRoot ("HexBinary")]
 		public class HexBinary
 		{