2007-09-25 Atsushi Enomoto <atsushi@ximian.com>
authorAtsushi Eno <atsushieno@gmail.com>
Tue, 25 Sep 2007 15:55:48 +0000 (15:55 -0000)
committerAtsushi Eno <atsushieno@gmail.com>
Tue, 25 Sep 2007 15:55:48 +0000 (15:55 -0000)
* XmlTypeMapping.cs, XmlSerializationWriterInterpreter.cs,
  SerializationCodeGenerator.cs :
  More XmlSchemaProviderAttribute method check. Use QName returned
  by the method for the output root name.

* XmlSerializerTests.cs, XmlReflectionImporterTests.cs :
  added tests for XmlSchemaProviderAttribute and how its qname works.

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

mcs/class/System.XML/System.Xml.Serialization/ChangeLog
mcs/class/System.XML/System.Xml.Serialization/SerializationCodeGenerator.cs
mcs/class/System.XML/System.Xml.Serialization/XmlSerializationWriterInterpreter.cs
mcs/class/System.XML/System.Xml.Serialization/XmlTypeMapping.cs
mcs/class/System.XML/Test/System.Xml.Serialization/ChangeLog
mcs/class/System.XML/Test/System.Xml.Serialization/XmlReflectionImporterTests.cs
mcs/class/System.XML/Test/System.Xml.Serialization/XmlSerializerTests.cs

index 3378dfbdf53f3be3313ff0d5ddd81f150f77f060..f4f1d7fb87fe2d68c546e06c6fdf21d40514717b 100644 (file)
@@ -1,3 +1,10 @@
+2007-09-25  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * XmlTypeMapping.cs, XmlSerializationWriterInterpreter.cs,
+         SerializationCodeGenerator.cs :
+         More XmlSchemaProviderAttribute method check. Use QName returned
+         by the method for the output root name.
+
 2007-08-21  Atsushi Enomoto  <atsushi@ximian.com>
 
        * XmlReflectionImporter.cs : do not reject XmlArrayAttribute on
index b7b154f57e44c8d8f2fe34cd47c2b9c54b65dbd9..40b577324d9588ef81dabdb58482da6225de1b6d 100644 (file)
@@ -666,7 +666,9 @@ namespace System.Xml.Serialization
 
                        if (typeMap.TypeData.SchemaType == SchemaTypes.XmlSerializable)
                        {
-                               WriteLine ("WriteSerializable (ob, element, namesp, isNullable);");
+                               WriteLine (String.Format ("WriteSerializable (ob, {0}, {1}, isNullable);",
+                                                         typeMap.XmlType != null ? GetLiteral (typeMap.XmlType) : "element",
+                                                         typeMap.XmlTypeNamespace != null ? GetLiteral (typeMap.XmlTypeNamespace) : "namesp"));
                                
                                GenerateEndHook ();
                                WriteLineUni ("}");
@@ -2135,7 +2137,7 @@ namespace System.Xml.Serialization
                                        return GetReadObjectCall (elem.MappedType, GetLiteral(elem.IsNullable), "true");
 
                                case SchemaTypes.XmlSerializable:
-                                       return GetCast (elem.TypeData, String.Format ("ReadSerializable (({0}) Activator.CreateInstance(typeof({0}), true))", elem.TypeData.CSharpFullName));
+                                       return GetCast (elem.TypeData, String.Format ("({0}) ReadSerializable (({0}) Activator.CreateInstance(typeof({0}), true))", elem.TypeData.CSharpFullName));
 
                                default:
                                        throw new NotSupportedException ("Invalid value type");
@@ -2431,7 +2433,7 @@ namespace System.Xml.Serialization
                        WriteLine ("if (Reader.NodeType == XmlNodeType.Element)");
                        WriteLineInd ("{");
                        WriteLine ("if (Reader.LocalName == " + GetLiteral (typeMap.ElementName) + " && Reader.NamespaceURI == " + GetLiteral (typeMap.Namespace) + ")");
-                       WriteLine (String.Format ("\treturn ReadSerializable (({0}) Activator.CreateInstance(typeof({0}), true));", typeMap.TypeData.CSharpFullName));
+                       WriteLine (String.Format ("\treturn ({0}) ReadSerializable (({0}) Activator.CreateInstance(typeof({0}), true));", typeMap.TypeData.CSharpFullName));
                        WriteLine ("else");
                        WriteLine ("\tthrow CreateUnknownNodeException ();");
                        WriteLineUni ("}");
index bcb918281a5e58d87219e685efa68e33187d8398..29f7a61a53e19eebeddd7d2ad011bf733756960d 100644 (file)
@@ -116,7 +116,7 @@ namespace System.Xml.Serialization
 
                        if (typeMap.TypeData.SchemaType == SchemaTypes.XmlSerializable)
                        {
-                               WriteSerializable ((IXmlSerializable)ob, element, namesp, isNullable);
+                               WriteSerializable ((IXmlSerializable)ob, typeMap.XmlType ?? element, typeMap.XmlTypeNamespace ?? namesp, isNullable);
                                return;
                        }
 
index ef45eca4f552bab3a84f4a09cc90ecb192e3018b..bbf222f03dedf699f51a4fe4f789df2f78966b55 100644 (file)
@@ -205,6 +205,13 @@ namespace System.Xml.Serialization
                        if (schemaProvider != null) {
                                string method = schemaProvider.MethodName;
                                MethodInfo mi = typeData.Type.GetMethod (method, BindingFlags.Static | BindingFlags.Public);
+                               if (mi == null)
+                                       throw new InvalidOperationException (String.Format ("Type '{0}' must implement public static method '{1}'", typeData.Type, method));
+                               if (!typeof (XmlQualifiedName).IsAssignableFrom (mi.ReturnType) &&
+                                   // LAMESPEC: it is undocumented. (We don't have to tell users about it in the error message.)
+                                   // Also do not add such a silly compatibility test to assert that it does not raise an error.
+                                   !typeof (XmlSchemaComplexType).IsAssignableFrom (mi.ReturnType))
+                                       throw new InvalidOperationException (String.Format ("Method '{0}' indicated by XmlSchemaProviderAttribute must have its return type as XmlQualifiedName", method));
                                XmlSchemaSet xs = new XmlSchemaSet ();
                                object retVal = mi.Invoke (null, new object [] { xs });
                                _schemaTypeName = XmlQualifiedName.Empty;
index 2c692a76bb710fe2a0ddbdb9c84b4c67dadfd427..ced7c87f03ab0d035b0d4681e74e0fb3dc05bdc0 100644 (file)
@@ -1,3 +1,8 @@
+2007-09-25  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * XmlSerializerTests.cs, XmlReflectionImporterTests.cs :
+         added tests for XmlSchemaProviderAttribute and how its qname works.
+
 2007-08-21  Atsushi Enomoto  <atsushi@ximian.com>
 
        * XmlReflectionImporterTest.cs : test for bug #81880.
index d838c2bcc315a8902bd87ab4c577af5ec8fcb358..6e2778957378b44178f77d4a56e99c8a851f09b2 100644 (file)
@@ -1590,6 +1590,27 @@ namespace MonoTests.System.XmlSerialization
                {
                        new XmlSerializer (typeof (List<int>).GetGenericTypeDefinition ());
                }
+
+               [Test]
+               [ExpectedException (typeof (InvalidOperationException))]
+               public void XmlSchemaProviderMissingMethod ()
+               {
+                       new XmlSerializer (typeof (XmlSchemaProviderMissingMethodType));
+               }
+
+               [Test]
+               [ExpectedException (typeof (InvalidOperationException))]
+               public void XmlSchemaProviderMethodNonStatic ()
+               {
+                       new XmlSerializer (typeof (XmlSchemaProviderNonStaticType));
+               }
+
+               [Test]
+               [ExpectedException (typeof (InvalidOperationException))]
+               public void XmlSchemaProviderMethodIncorrectReturn ()
+               {
+                       new XmlSerializer (typeof (XmlSchemaProviderIncorrectReturnType));
+               }
 #endif
 
                public class Employee : IXmlSerializable
@@ -2081,6 +2102,67 @@ namespace MonoTests.System.XmlSerialization
                                set { this.value = value; }
                        }
                }
+
+               [XmlSchemaProvider ("GetXsdType")]
+               public class XmlSchemaProviderMissingMethodType : IXmlSerializable
+               {
+                       public void ReadXml (XmlReader reader)
+                       {
+                       }
+
+                       public void WriteXml (XmlWriter writer)
+                       {
+                       }
+
+                       public XmlSchema GetSchema ()
+                       {
+                               return null;
+                       }
+               }
+
+               [XmlSchemaProvider ("GetXsdType")]
+               public class XmlSchemaProviderNonStaticType : IXmlSerializable
+               {
+                       public void ReadXml (XmlReader reader)
+                       {
+                       }
+
+                       public void WriteXml (XmlWriter writer)
+                       {
+                       }
+
+                       public XmlSchema GetSchema ()
+                       {
+                               return null;
+                       }
+
+                       public object GetXsdType ()
+                       {
+                               return null;
+                       }
+               }
+
+               [XmlSchemaProvider ("GetXsdType")]
+               public class XmlSchemaProviderIncorrectReturnType : IXmlSerializable
+               {
+                       public void ReadXml (XmlReader reader)
+                       {
+                       }
+
+                       public void WriteXml (XmlWriter writer)
+                       {
+                       }
+
+                       public XmlSchema GetSchema ()
+                       {
+                               return null;
+                       }
+
+                       public static object GetXsdType ()
+                       {
+                               return null;
+                       }
+               }
 #endif
        }
 }
index a293d3fbedef0392ea640a833aa7ed959ffb2312..d36183aa0b6cd8476b47ca1591c20a0fe5afc38e 100644 (file)
@@ -2662,6 +2662,14 @@ namespace MonoTests.System.XmlSerialization
                        xs.Serialize (sw, PrivateCtorOnly.Instance);
                        xs.Deserialize (new StringReader (sw.ToString ()));
                }
+
+               [Test]
+               public void XmlSchemaProviderQNameBecomesRootName ()
+               {
+                       xs = new XmlSerializer (typeof (XmlSchemaProviderQNameBecomesRootNameType));
+                       Serialize (new XmlSchemaProviderQNameBecomesRootNameType ());
+                       Assert.AreEqual (Infoset ("<foo />"), WriterText);
+               }
 #endif
 
                #endregion //GenericsSeralizationTests
@@ -2760,6 +2768,33 @@ namespace MonoTests.System.XmlSerialization
                                get { return NullableInt.HasValue; }
                        }
                }
+
+               [XmlSchemaProvider ("GetXsdType")]
+               public class XmlSchemaProviderQNameBecomesRootNameType : IXmlSerializable
+               {
+                       public XmlSchema GetSchema ()
+                       {
+                               return null;
+                       }
+
+                       public void ReadXml (XmlReader reader)
+                       {
+                       }
+
+                       public void WriteXml (XmlWriter writer)
+                       {
+                       }
+
+                       public static XmlQualifiedName GetXsdType (XmlSchemaSet xss)
+                       {
+                               XmlSchema xs = new XmlSchema ();
+                               XmlSchemaComplexType ct = new XmlSchemaComplexType ();
+                               ct.Name = "foo";
+                               xs.Items.Add (ct);
+                               xss.Add (xs);
+                               return new XmlQualifiedName ("foo");
+                       }
+               }
 #endif
 
                void CDataTextNodes_BadNode (object s, XmlNodeEventArgs e)