merge -r 53370:58178
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Description / ServiceDescription.cs
1 // \r
2 // System.Web.Services.Description.ServiceDescription.cs\r
3 //\r
4 // Author:\r
5 //   Tim Coleman (tim@timcoleman.com)\r
6 //   Lluis Sanchez Gual (lluis@ximian.com)\r
7 //\r
8 // Copyright (C) Tim Coleman, 2002\r
9 //\r
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 \r
32 using System.IO;\r
33 using System.Collections;\r
34 using System.Reflection;\r
35 using System.Web.Services;\r
36 using System.Web.Services.Configuration;\r
37 using System.Xml;\r
38 using System.Xml.Schema;\r
39 using System.Xml.Serialization;\r
40 \r
41 namespace System.Web.Services.Description\r
42 {\r
43         [XmlFormatExtensionPoint ("Extensions")]\r
44         [XmlRoot ("definitions", Namespace = "http://schemas.xmlsoap.org/wsdl/")]\r
45         public sealed class ServiceDescription :\r
46 #if NET_2_0\r
47                 NamedItem\r
48 #else\r
49                 DocumentableItem \r
50 #endif\r
51         {\r
52                 #region Fields\r
53 \r
54                 public const string Namespace = "http://schemas.xmlsoap.org/wsdl/";\r
55 \r
56                 BindingCollection bindings;\r
57                 ServiceDescriptionFormatExtensionCollection extensions;\r
58                 ImportCollection imports;\r
59                 MessageCollection messages;\r
60 #if !NET_2_0\r
61                 string name;\r
62 #endif\r
63                 PortTypeCollection portTypes;\r
64                 string retrievalUrl;\r
65                 ServiceDescriptionCollection serviceDescriptions;\r
66                 ServiceCollection services;\r
67                 string targetNamespace;\r
68                 Types types;\r
69 #if !TARGET_JVM
70                 static ServiceDescriptionSerializer serializer;\r
71 #else
72                 static ServiceDescriptionSerializer serializer {
73                         get {
74                                 return (ServiceDescriptionSerializer)AppDomain.CurrentDomain.GetData("ServiceDescriptionSerializer.serializer");
75                         }
76                         set {
77                                 AppDomain.CurrentDomain.SetData("ServiceDescriptionSerializer.serializer", value);
78                         }
79                 }
80 #endif
81 \r
82                 #endregion // Fields\r
83 \r
84                 #region Constructors\r
85 \r
86                 static ServiceDescription ()\r
87                 {\r
88                         serializer = new ServiceDescriptionSerializer ();\r
89                 }\r
90 \r
91                 public ServiceDescription ()\r
92                 {\r
93                         bindings = new BindingCollection (this);\r
94                         extensions = new ServiceDescriptionFormatExtensionCollection (this);\r
95                         imports = new ImportCollection (this);\r
96                         messages = new MessageCollection (this);\r
97 #if !NET_2_0\r
98 //                      name = String.Empty;            \r
99 #endif\r
100                         portTypes = new PortTypeCollection (this);\r
101 \r
102                         serviceDescriptions = null;\r
103                         services = new ServiceCollection (this);\r
104                         targetNamespace = String.Empty;\r
105                         types = null;\r
106                 }\r
107                 \r
108                 #endregion // Constructors\r
109 \r
110                 #region Properties\r
111 \r
112                 [XmlElement ("import")]\r
113                 public ImportCollection Imports {\r
114                         get { return imports; }\r
115                 }\r
116 \r
117                 [XmlElement ("types")]\r
118                 public Types Types {\r
119                         get { return types; }\r
120                         set { types = value; }\r
121                 }\r
122 \r
123                 [XmlElement ("message")]\r
124                 public MessageCollection Messages {\r
125                         get { return messages; }\r
126                 }\r
127 \r
128                 [XmlElement ("portType")]       \r
129                 public PortTypeCollection PortTypes {\r
130                         get { return portTypes; }\r
131                 }\r
132         \r
133                 [XmlElement ("binding")]\r
134                 public BindingCollection Bindings {\r
135                         get { return bindings; }\r
136                 }\r
137 \r
138                 [XmlIgnore]\r
139                 public ServiceDescriptionFormatExtensionCollection Extensions {         \r
140                         get { return extensions; }\r
141                 }\r
142 \r
143 #if !NET_2_0\r
144                 [XmlAttribute ("name", DataType = "NMTOKEN")]   \r
145                 public string Name {\r
146                         get { return name; }\r
147                         set { name = value; }\r
148                 }\r
149 #endif\r
150 \r
151                 [XmlIgnore]     \r
152                 public string RetrievalUrl {\r
153                         get { return retrievalUrl; }\r
154                         set { retrievalUrl = value; }\r
155                 }\r
156         \r
157                 [XmlIgnore]     \r
158                 public static XmlSerializer Serializer {\r
159                         get { return serializer; }\r
160                 }\r
161 \r
162                 [XmlIgnore]\r
163                 public ServiceDescriptionCollection ServiceDescriptions {\r
164                         get { \r
165                                 if (serviceDescriptions == null) \r
166                                         throw new NullReferenceException ();\r
167                                 return serviceDescriptions; \r
168                         }\r
169                 }\r
170 \r
171                 [XmlElement ("service")]\r
172                 public ServiceCollection Services {\r
173                         get { return services; }\r
174                 }\r
175 \r
176                 [XmlAttribute ("targetNamespace")]\r
177                 public string TargetNamespace {\r
178                         get { return targetNamespace; }\r
179                         set { targetNamespace = value; }\r
180                 }\r
181 \r
182                 #endregion // Properties\r
183 \r
184                 #region Methods\r
185 \r
186                 public static bool CanRead (XmlReader reader)\r
187                 {\r
188                         reader.MoveToContent ();\r
189                         return reader.LocalName == "definitions" && \r
190                                 reader.NamespaceURI == "http://schemas.xmlsoap.org/wsdl/";\r
191                 }\r
192 \r
193                 public static ServiceDescription Read (Stream stream)\r
194                 {\r
195                         return (ServiceDescription) serializer.Deserialize (stream);\r
196                 }\r
197 \r
198                 public static ServiceDescription Read (string fileName)\r
199                 {\r
200                         return Read (new FileStream (fileName, FileMode.Open));\r
201                 }\r
202 \r
203                 public static ServiceDescription Read (TextReader textReader)\r
204                 {\r
205                         return (ServiceDescription) serializer.Deserialize (textReader);\r
206                 }\r
207 \r
208                 public static ServiceDescription Read (XmlReader reader)\r
209                 {\r
210                         return (ServiceDescription) serializer.Deserialize (reader);\r
211                 }\r
212 \r
213                 public void Write (Stream stream)\r
214                 {\r
215                         serializer.Serialize (stream, this, GetNamespaceList ());\r
216                 }\r
217 \r
218                 public void Write (string fileName)\r
219                 {\r
220                         Write (new FileStream (fileName, FileMode.Create));\r
221                 }\r
222 \r
223                 public void Write (TextWriter writer)\r
224                 {\r
225                         serializer.Serialize (writer, this, GetNamespaceList ());\r
226                 }\r
227 \r
228                 public void Write (XmlWriter writer)\r
229                 {\r
230                         serializer.Serialize (writer, this, GetNamespaceList ());\r
231                 }\r
232 \r
233                 internal void SetParent (ServiceDescriptionCollection serviceDescriptions)\r
234                 {\r
235                         this.serviceDescriptions = serviceDescriptions; \r
236                 }\r
237                 \r
238                 XmlSerializerNamespaces GetNamespaceList ()\r
239                 {\r
240                         XmlSerializerNamespaces ns;\r
241                         ns = new XmlSerializerNamespaces ();\r
242                         ns.Add ("soap", SoapBinding.Namespace);\r
243                         ns.Add ("soapenc", "http://schemas.xmlsoap.org/soap/encoding/");\r
244                         ns.Add ("s", XmlSchema.Namespace);\r
245                         ns.Add ("http", HttpBinding.Namespace);\r
246                         ns.Add ("mime", MimeContentBinding.Namespace);\r
247                         ns.Add ("tm", MimeTextBinding.Namespace);\r
248                         ns.Add ("s0", TargetNamespace);\r
249                         \r
250                         AddExtensionNamespaces (ns, Extensions);\r
251                         \r
252                         if (Types != null) AddExtensionNamespaces (ns, Types.Extensions);\r
253                         \r
254                         foreach (Service ser in Services)\r
255                                 foreach (Port port in ser.Ports)\r
256                                         AddExtensionNamespaces (ns, port.Extensions);\r
257 \r
258                         foreach (Binding bin in Bindings)\r
259                         {\r
260                                 AddExtensionNamespaces (ns, bin.Extensions);\r
261                                 foreach (OperationBinding op in bin.Operations)\r
262                                 {\r
263                                         AddExtensionNamespaces (ns, op.Extensions);\r
264                                         if (op.Input != null) AddExtensionNamespaces (ns, op.Input.Extensions);\r
265                                         if (op.Output != null) AddExtensionNamespaces (ns, op.Output.Extensions);\r
266                                 }\r
267                         }\r
268                         return ns;\r
269                 }\r
270                 \r
271                 void AddExtensionNamespaces (XmlSerializerNamespaces ns, ServiceDescriptionFormatExtensionCollection extensions)\r
272                 {\r
273                         foreach (ServiceDescriptionFormatExtension ext in extensions)\r
274                         {\r
275                                 ExtensionInfo einf = ExtensionManager.GetFormatExtensionInfo (ext.GetType ());\r
276                                 foreach (XmlQualifiedName qname in einf.NamespaceDeclarations)\r
277                                         ns.Add (qname.Name, qname.Namespace);\r
278                         }\r
279                 }\r
280                 \r
281                 internal static void WriteExtensions (XmlWriter writer, object ob)\r
282                 {\r
283                         ServiceDescriptionFormatExtensionCollection extensions = ExtensionManager.GetExtensionPoint (ob);\r
284                         if (extensions != null)\r
285                         {\r
286                                 foreach (ServiceDescriptionFormatExtension ext in extensions)\r
287                                         WriteExtension (writer, ext);\r
288                         }\r
289                 }\r
290                 \r
291                 static void WriteExtension (XmlWriter writer, ServiceDescriptionFormatExtension ext)\r
292                 {\r
293                         Type type = ext.GetType ();\r
294                         ExtensionInfo info = ExtensionManager.GetFormatExtensionInfo (type);\r
295                         \r
296 //                              if (prefix != null && prefix != "")\r
297 //                                      Writer.WriteStartElement (prefix, info.ElementName, info.Namespace);\r
298 //                              else\r
299 //                                      WriteStartElement (info.ElementName, info.Namespace, false);\r
300 \r
301                         XmlSerializerNamespaces ns = new XmlSerializerNamespaces ();\r
302                         ns.Add ("","");\r
303                         info.Serializer.Serialize (writer, ext, ns);\r
304                 }\r
305                 \r
306                 internal static void ReadExtension (XmlReader reader, object ob)\r
307                 {\r
308                         ServiceDescriptionFormatExtensionCollection extensions = ExtensionManager.GetExtensionPoint (ob);\r
309                         if (extensions != null)\r
310                         {\r
311                                 ExtensionInfo info = ExtensionManager.GetFormatExtensionInfo (reader.LocalName, reader.NamespaceURI);\r
312                                 if (info != null)\r
313                                 {\r
314                                         object extension = info.Serializer.Deserialize (reader);\r
315                                         extensions.Add ((ServiceDescriptionFormatExtension)extension);\r
316                                         return;\r
317                                 }\r
318                         }\r
319                         reader.Skip ();\r
320                 }\r
321 \r
322 \r
323                 #endregion\r
324 \r
325                 internal class ServiceDescriptionSerializer : XmlSerializer \r
326                 {\r
327                         protected override void Serialize (object o, XmlSerializationWriter writer)\r
328                         {\r
329                                 ServiceDescriptionWriterBase xsWriter = writer as ServiceDescriptionWriterBase;\r
330                                 xsWriter.WriteTree ((ServiceDescription)o);\r
331                         }\r
332                         \r
333                         protected override object Deserialize (XmlSerializationReader reader)\r
334                         {\r
335                                 ServiceDescriptionReaderBase xsReader = reader as ServiceDescriptionReaderBase;\r
336                                 return xsReader.ReadTree ();\r
337                         }\r
338                         \r
339                         protected override XmlSerializationWriter CreateWriter ()\r
340                         {\r
341                                 return new ServiceDescriptionWriterBase ();\r
342                         }\r
343                         \r
344                         protected override XmlSerializationReader CreateReader ()\r
345                         {\r
346                                 return new ServiceDescriptionReaderBase ();\r
347                         }\r
348                 }               \r
349         }\r
350 }