* Mono.Posix.dll.sources: Rename Mono.Posix to Mono.Unix.
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Description / ServiceDescription.cs
index 73343baff41da40f58d4ccf590ce182cbe657274..66729c338ab7dccfc43d4f12f03793533a8aac6a 100644 (file)
@@ -3,21 +3,52 @@
 //\r
 // Author:\r
 //   Tim Coleman (tim@timcoleman.com)\r
+//   Lluis Sanchez Gual (lluis@ximian.com)\r
 //\r
 // Copyright (C) Tim Coleman, 2002\r
 //\r
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
 \r
 using System.IO;\r
+using System.Collections;\r
+using System.Reflection;\r
 using System.Web.Services;\r
 using System.Web.Services.Configuration;\r
 using System.Xml;\r
+using System.Xml.Schema;\r
 using System.Xml.Serialization;\r
 \r
-namespace System.Web.Services.Description {\r
+namespace System.Web.Services.Description\r
+{\r
        [XmlFormatExtensionPoint ("Extensions")]\r
        [XmlRoot ("definitions", Namespace = "http://schemas.xmlsoap.org/wsdl/")]\r
-       public sealed class ServiceDescription : DocumentableItem {\r
-\r
+       public sealed class ServiceDescription :\r
+#if NET_2_0\r
+               NamedItem\r
+#else\r
+               DocumentableItem \r
+#endif\r
+       {\r
                #region Fields\r
 \r
                public const string Namespace = "http://schemas.xmlsoap.org/wsdl/";\r
@@ -26,18 +57,16 @@ namespace System.Web.Services.Description {
                ServiceDescriptionFormatExtensionCollection extensions;\r
                ImportCollection imports;\r
                MessageCollection messages;\r
+#if !NET_2_0\r
                string name;\r
+#endif\r
                PortTypeCollection portTypes;\r
                string retrievalUrl;\r
                ServiceDescriptionCollection serviceDescriptions;\r
                ServiceCollection services;\r
                string targetNamespace;\r
                Types types;\r
-\r
-               static XmlSerializer serializer;\r
-               static SoapReflectionImporter importer;\r
-               static XmlTypeMapping mapping;\r
-\r
+               static ServiceDescriptionSerializer serializer;\r
 \r
                #endregion // Fields\r
 \r
@@ -45,24 +74,19 @@ namespace System.Web.Services.Description {
 \r
                static ServiceDescription ()\r
                {\r
-                       importer = new SoapReflectionImporter ();\r
-                       mapping = importer.ImportTypeMapping (typeof (ServiceDescription));\r
-                       serializer = new XmlSerializer (mapping);\r
+                       serializer = new ServiceDescriptionSerializer ();\r
                }\r
-       \r
+\r
                public ServiceDescription ()\r
                {\r
                        bindings = new BindingCollection (this);\r
                        extensions = new ServiceDescriptionFormatExtensionCollection (this);\r
                        imports = new ImportCollection (this);\r
                        messages = new MessageCollection (this);\r
+#if !NET_2_0\r
                        name = String.Empty;            \r
+#endif\r
                        portTypes = new PortTypeCollection (this);\r
-                       retrievalUrl = String.Empty;\r
-\r
-                       SoapReflectionImporter importer = new SoapReflectionImporter ();\r
-                       XmlTypeMapping mapping = importer.ImportTypeMapping (typeof (ServiceDescription));\r
-                       serializer = new XmlSerializer (mapping);\r
 \r
                        serviceDescriptions = null;\r
                        services = new ServiceCollection (this);\r
@@ -74,37 +98,45 @@ namespace System.Web.Services.Description {
 \r
                #region Properties\r
 \r
-               [XmlElement ("binding")]\r
-               public BindingCollection Bindings {\r
-                       get { return bindings; }\r
-               }\r
-\r
-               [XmlIgnore]\r
-               public ServiceDescriptionFormatExtensionCollection Extensions {         \r
-                       get { return extensions; }\r
-               }\r
-\r
                [XmlElement ("import")]\r
                public ImportCollection Imports {\r
                        get { return imports; }\r
                }\r
 \r
+               [XmlElement ("types")]\r
+               public Types Types {\r
+                       get { return types; }\r
+                       set { types = value; }\r
+               }\r
+\r
                [XmlElement ("message")]\r
                public MessageCollection Messages {\r
                        get { return messages; }\r
                }\r
 \r
+               [XmlElement ("portType")]       \r
+               public PortTypeCollection PortTypes {\r
+                       get { return portTypes; }\r
+               }\r
+       \r
+               [XmlElement ("binding")]\r
+               public BindingCollection Bindings {\r
+                       get { return bindings; }\r
+               }\r
+\r
+               [XmlIgnore]\r
+               public ServiceDescriptionFormatExtensionCollection Extensions {         \r
+                       get { return extensions; }\r
+               }\r
+\r
+#if !NET_2_0\r
                [XmlAttribute ("name", DataType = "NMTOKEN")]   \r
                public string Name {\r
                        get { return name; }\r
                        set { name = value; }\r
                }\r
+#endif\r
 \r
-               [XmlElement ("portType")]       \r
-               public PortTypeCollection PortTypes {\r
-                       get { return portTypes; }\r
-               }\r
-       \r
                [XmlIgnore]     \r
                public string RetrievalUrl {\r
                        get { return retrievalUrl; }\r
@@ -136,19 +168,15 @@ namespace System.Web.Services.Description {
                        set { targetNamespace = value; }\r
                }\r
 \r
-               [XmlElement ("type")]\r
-               public Types Types {\r
-                       get { return types; }\r
-                       set { types = value; }\r
-               }\r
-\r
                #endregion // Properties\r
 \r
                #region Methods\r
 \r
                public static bool CanRead (XmlReader reader)\r
                {\r
-                       return serializer.CanDeserialize (reader);\r
+                       reader.MoveToContent ();\r
+                       return reader.LocalName == "definitions" && \r
+                               reader.NamespaceURI == "http://schemas.xmlsoap.org/wsdl/";\r
                }\r
 \r
                public static ServiceDescription Read (Stream stream)\r
@@ -173,7 +201,7 @@ namespace System.Web.Services.Description {
 \r
                public void Write (Stream stream)\r
                {\r
-                       serializer.Serialize (stream, this);\r
+                       serializer.Serialize (stream, this, GetNamespaceList ());\r
                }\r
 \r
                public void Write (string fileName)\r
@@ -183,19 +211,129 @@ namespace System.Web.Services.Description {
 \r
                public void Write (TextWriter writer)\r
                {\r
-                       serializer.Serialize (writer, this);\r
+                       serializer.Serialize (writer, this, GetNamespaceList ());\r
                }\r
 \r
                public void Write (XmlWriter writer)\r
                {\r
-                       serializer.Serialize (writer, this);\r
+                       serializer.Serialize (writer, this, GetNamespaceList ());\r
                }\r
 \r
                internal void SetParent (ServiceDescriptionCollection serviceDescriptions)\r
                {\r
                        this.serviceDescriptions = serviceDescriptions; \r
                }\r
+               \r
+               XmlSerializerNamespaces GetNamespaceList ()\r
+               {\r
+                       XmlSerializerNamespaces ns;\r
+                       ns = new XmlSerializerNamespaces ();\r
+                       ns.Add ("soap", SoapBinding.Namespace);\r
+                       ns.Add ("soapenc", "http://schemas.xmlsoap.org/soap/encoding/");\r
+                       ns.Add ("s", XmlSchema.Namespace);\r
+                       ns.Add ("http", HttpBinding.Namespace);\r
+                       ns.Add ("mime", MimeContentBinding.Namespace);\r
+                       ns.Add ("tm", MimeTextBinding.Namespace);\r
+                       ns.Add ("s0", TargetNamespace);\r
+                       \r
+                       AddExtensionNamespaces (ns, Extensions);\r
+                       \r
+                       if (Types != null) AddExtensionNamespaces (ns, Types.Extensions);\r
+                       \r
+                       foreach (Service ser in Services)\r
+                               foreach (Port port in ser.Ports)\r
+                                       AddExtensionNamespaces (ns, port.Extensions);\r
+\r
+                       foreach (Binding bin in Bindings)\r
+                       {\r
+                               AddExtensionNamespaces (ns, bin.Extensions);\r
+                               foreach (OperationBinding op in bin.Operations)\r
+                               {\r
+                                       AddExtensionNamespaces (ns, op.Extensions);\r
+                                       if (op.Input != null) AddExtensionNamespaces (ns, op.Input.Extensions);\r
+                                       if (op.Output != null) AddExtensionNamespaces (ns, op.Output.Extensions);\r
+                               }\r
+                       }\r
+                       return ns;\r
+               }\r
+               \r
+               void AddExtensionNamespaces (XmlSerializerNamespaces ns, ServiceDescriptionFormatExtensionCollection extensions)\r
+               {\r
+                       foreach (ServiceDescriptionFormatExtension ext in extensions)\r
+                       {\r
+                               ExtensionInfo einf = ExtensionManager.GetFormatExtensionInfo (ext.GetType ());\r
+                               foreach (XmlQualifiedName qname in einf.NamespaceDeclarations)\r
+                                       ns.Add (qname.Name, qname.Namespace);\r
+                       }\r
+               }\r
+               \r
+               internal static void WriteExtensions (XmlWriter writer, object ob)\r
+               {\r
+                       ServiceDescriptionFormatExtensionCollection extensions = ExtensionManager.GetExtensionPoint (ob);\r
+                       if (extensions != null)\r
+                       {\r
+                               foreach (ServiceDescriptionFormatExtension ext in extensions)\r
+                                       WriteExtension (writer, ext);\r
+                       }\r
+               }\r
+               \r
+               static void WriteExtension (XmlWriter writer, ServiceDescriptionFormatExtension ext)\r
+               {\r
+                       Type type = ext.GetType ();\r
+                       ExtensionInfo info = ExtensionManager.GetFormatExtensionInfo (type);\r
+                       \r
+//                             if (prefix != null && prefix != "")\r
+//                                     Writer.WriteStartElement (prefix, info.ElementName, info.Namespace);\r
+//                             else\r
+//                                     WriteStartElement (info.ElementName, info.Namespace, false);\r
+\r
+                       XmlSerializerNamespaces ns = new XmlSerializerNamespaces ();\r
+                       ns.Add ("","");\r
+                       info.Serializer.Serialize (writer, ext, ns);\r
+               }\r
+               \r
+               internal static void ReadExtension (XmlReader reader, object ob)\r
+               {\r
+                       ServiceDescriptionFormatExtensionCollection extensions = ExtensionManager.GetExtensionPoint (ob);\r
+                       if (extensions != null)\r
+                       {\r
+                               ExtensionInfo info = ExtensionManager.GetFormatExtensionInfo (reader.LocalName, reader.NamespaceURI);\r
+                               if (info != null)\r
+                               {\r
+                                       object extension = info.Serializer.Deserialize (reader);\r
+                                       extensions.Add ((ServiceDescriptionFormatExtension)extension);\r
+                                       return;\r
+                               }\r
+                       }\r
+                       reader.Skip ();\r
+               }\r
+\r
 \r
                #endregion\r
+\r
+               internal class ServiceDescriptionSerializer : XmlSerializer \r
+               {\r
+                       protected override void Serialize (object o, XmlSerializationWriter writer)\r
+                       {\r
+                               ServiceDescriptionWriterBase xsWriter = writer as ServiceDescriptionWriterBase;\r
+                               xsWriter.WriteTree ((ServiceDescription)o);\r
+                       }\r
+                       \r
+                       protected override object Deserialize (XmlSerializationReader reader)\r
+                       {\r
+                               ServiceDescriptionReaderBase xsReader = reader as ServiceDescriptionReaderBase;\r
+                               return xsReader.ReadTree ();\r
+                       }\r
+                       \r
+                       protected override XmlSerializationWriter CreateWriter ()\r
+                       {\r
+                               return new ServiceDescriptionWriterBase ();\r
+                       }\r
+                       \r
+                       protected override XmlSerializationReader CreateReader ()\r
+                       {\r
+                               return new ServiceDescriptionReaderBase ();\r
+                       }\r
+               }               \r
        }\r
-}\r
+}