2006-11-02 Atsushi Enomoto <atsushi@ximian.com>
[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                 static ServiceDescriptionSerializer serializer;\r
70 \r
71                 #endregion // Fields\r
72 \r
73                 #region Constructors\r
74 \r
75                 static ServiceDescription ()\r
76                 {\r
77                         serializer = new ServiceDescriptionSerializer ();\r
78                 }\r
79 \r
80                 public ServiceDescription ()\r
81                 {\r
82                         bindings = new BindingCollection (this);\r
83                         extensions = new ServiceDescriptionFormatExtensionCollection (this);\r
84                         imports = new ImportCollection (this);\r
85                         messages = new MessageCollection (this);\r
86 #if !NET_2_0\r
87                         name = String.Empty;            \r
88 #endif\r
89                         portTypes = new PortTypeCollection (this);\r
90 \r
91                         serviceDescriptions = null;\r
92                         services = new ServiceCollection (this);\r
93                         targetNamespace = String.Empty;\r
94                         types = null;\r
95                 }\r
96                 \r
97                 #endregion // Constructors\r
98 \r
99                 #region Properties\r
100 \r
101                 [XmlElement ("import")]\r
102                 public ImportCollection Imports {\r
103                         get { return imports; }\r
104                 }\r
105 \r
106                 [XmlElement ("types")]\r
107                 public Types Types {\r
108                         get { return types; }\r
109                         set { types = value; }\r
110                 }\r
111 \r
112                 [XmlElement ("message")]\r
113                 public MessageCollection Messages {\r
114                         get { return messages; }\r
115                 }\r
116 \r
117                 [XmlElement ("portType")]       \r
118                 public PortTypeCollection PortTypes {\r
119                         get { return portTypes; }\r
120                 }\r
121         \r
122                 [XmlElement ("binding")]\r
123                 public BindingCollection Bindings {\r
124                         get { return bindings; }\r
125                 }\r
126 \r
127                 [XmlIgnore]\r
128                 public ServiceDescriptionFormatExtensionCollection Extensions {         \r
129                         get { return extensions; }\r
130                 }\r
131 \r
132 #if !NET_2_0\r
133                 [XmlAttribute ("name", DataType = "NMTOKEN")]   \r
134                 public string Name {\r
135                         get { return name; }\r
136                         set { name = value; }\r
137                 }\r
138 #endif\r
139 \r
140                 [XmlIgnore]     \r
141                 public string RetrievalUrl {\r
142                         get { return retrievalUrl; }\r
143                         set { retrievalUrl = value; }\r
144                 }\r
145         \r
146                 [XmlIgnore]     \r
147                 public static XmlSerializer Serializer {\r
148                         get { return serializer; }\r
149                 }\r
150 \r
151                 [XmlIgnore]\r
152                 public ServiceDescriptionCollection ServiceDescriptions {\r
153                         get { \r
154                                 if (serviceDescriptions == null) \r
155                                         throw new NullReferenceException ();\r
156                                 return serviceDescriptions; \r
157                         }\r
158                 }\r
159 \r
160                 [XmlElement ("service")]\r
161                 public ServiceCollection Services {\r
162                         get { return services; }\r
163                 }\r
164 \r
165                 [XmlAttribute ("targetNamespace")]\r
166                 public string TargetNamespace {\r
167                         get { return targetNamespace; }\r
168                         set { targetNamespace = value; }\r
169                 }\r
170 \r
171                 #endregion // Properties\r
172 \r
173                 #region Methods\r
174 \r
175                 public static bool CanRead (XmlReader reader)\r
176                 {\r
177                         reader.MoveToContent ();\r
178                         return reader.LocalName == "definitions" && \r
179                                 reader.NamespaceURI == "http://schemas.xmlsoap.org/wsdl/";\r
180                 }\r
181 \r
182                 public static ServiceDescription Read (Stream stream)\r
183                 {\r
184                         return (ServiceDescription) serializer.Deserialize (stream);\r
185                 }\r
186 \r
187                 public static ServiceDescription Read (string fileName)\r
188                 {\r
189                         return Read (new FileStream (fileName, FileMode.Open));\r
190                 }\r
191 \r
192                 public static ServiceDescription Read (TextReader textReader)\r
193                 {\r
194                         return (ServiceDescription) serializer.Deserialize (textReader);\r
195                 }\r
196 \r
197                 public static ServiceDescription Read (XmlReader reader)\r
198                 {\r
199                         return (ServiceDescription) serializer.Deserialize (reader);\r
200                 }\r
201 \r
202                 public void Write (Stream stream)\r
203                 {\r
204                         serializer.Serialize (stream, this, GetNamespaceList ());\r
205                 }\r
206 \r
207                 public void Write (string fileName)\r
208                 {\r
209                         Write (new FileStream (fileName, FileMode.Create));\r
210                 }\r
211 \r
212                 public void Write (TextWriter writer)\r
213                 {\r
214                         serializer.Serialize (writer, this, GetNamespaceList ());\r
215                 }\r
216 \r
217                 public void Write (XmlWriter writer)\r
218                 {\r
219                         serializer.Serialize (writer, this, GetNamespaceList ());\r
220                 }\r
221 \r
222                 internal void SetParent (ServiceDescriptionCollection serviceDescriptions)\r
223                 {\r
224                         this.serviceDescriptions = serviceDescriptions; \r
225                 }\r
226                 \r
227                 XmlSerializerNamespaces GetNamespaceList ()\r
228                 {\r
229                         XmlSerializerNamespaces ns;\r
230                         ns = new XmlSerializerNamespaces ();\r
231                         ns.Add ("soap", SoapBinding.Namespace);\r
232                         ns.Add ("soapenc", "http://schemas.xmlsoap.org/soap/encoding/");\r
233                         ns.Add ("s", XmlSchema.Namespace);\r
234                         ns.Add ("http", HttpBinding.Namespace);\r
235                         ns.Add ("mime", MimeContentBinding.Namespace);\r
236                         ns.Add ("tm", MimeTextBinding.Namespace);\r
237                         ns.Add ("s0", TargetNamespace);\r
238                         \r
239                         AddExtensionNamespaces (ns, Extensions);\r
240                         \r
241                         if (Types != null) AddExtensionNamespaces (ns, Types.Extensions);\r
242                         \r
243                         foreach (Service ser in Services)\r
244                                 foreach (Port port in ser.Ports)\r
245                                         AddExtensionNamespaces (ns, port.Extensions);\r
246 \r
247                         foreach (Binding bin in Bindings)\r
248                         {\r
249                                 AddExtensionNamespaces (ns, bin.Extensions);\r
250                                 foreach (OperationBinding op in bin.Operations)\r
251                                 {\r
252                                         AddExtensionNamespaces (ns, op.Extensions);\r
253                                         if (op.Input != null) AddExtensionNamespaces (ns, op.Input.Extensions);\r
254                                         if (op.Output != null) AddExtensionNamespaces (ns, op.Output.Extensions);\r
255                                 }\r
256                         }\r
257                         return ns;\r
258                 }\r
259                 \r
260                 void AddExtensionNamespaces (XmlSerializerNamespaces ns, ServiceDescriptionFormatExtensionCollection extensions)\r
261                 {\r
262                         foreach (ServiceDescriptionFormatExtension ext in extensions)\r
263                         {\r
264                                 ExtensionInfo einf = ExtensionManager.GetFormatExtensionInfo (ext.GetType ());\r
265                                 foreach (XmlQualifiedName qname in einf.NamespaceDeclarations)\r
266                                         ns.Add (qname.Name, qname.Namespace);\r
267                         }\r
268                 }\r
269                 \r
270                 internal static void WriteExtensions (XmlWriter writer, object ob)\r
271                 {\r
272                         ServiceDescriptionFormatExtensionCollection extensions = ExtensionManager.GetExtensionPoint (ob);\r
273                         if (extensions != null)\r
274                         {\r
275                                 foreach (ServiceDescriptionFormatExtension ext in extensions)\r
276                                         WriteExtension (writer, ext);\r
277                         }\r
278                 }\r
279                 \r
280                 static void WriteExtension (XmlWriter writer, ServiceDescriptionFormatExtension ext)\r
281                 {\r
282                         Type type = ext.GetType ();\r
283                         ExtensionInfo info = ExtensionManager.GetFormatExtensionInfo (type);\r
284                         \r
285 //                              if (prefix != null && prefix != "")\r
286 //                                      Writer.WriteStartElement (prefix, info.ElementName, info.Namespace);\r
287 //                              else\r
288 //                                      WriteStartElement (info.ElementName, info.Namespace, false);\r
289 \r
290                         XmlSerializerNamespaces ns = new XmlSerializerNamespaces ();\r
291                         ns.Add ("","");\r
292                         info.Serializer.Serialize (writer, ext, ns);\r
293                 }\r
294                 \r
295                 internal static void ReadExtension (XmlReader reader, object ob)\r
296                 {\r
297                         ServiceDescriptionFormatExtensionCollection extensions = ExtensionManager.GetExtensionPoint (ob);\r
298                         if (extensions != null)\r
299                         {\r
300                                 ExtensionInfo info = ExtensionManager.GetFormatExtensionInfo (reader.LocalName, reader.NamespaceURI);\r
301                                 if (info != null)\r
302                                 {\r
303                                         object extension = info.Serializer.Deserialize (reader);\r
304                                         extensions.Add ((ServiceDescriptionFormatExtension)extension);\r
305                                         return;\r
306                                 }\r
307                         }\r
308                         reader.Skip ();\r
309                 }\r
310 \r
311 \r
312                 #endregion\r
313 \r
314                 internal class ServiceDescriptionSerializer : XmlSerializer \r
315                 {\r
316                         protected override void Serialize (object o, XmlSerializationWriter writer)\r
317                         {\r
318                                 ServiceDescriptionWriterBase xsWriter = writer as ServiceDescriptionWriterBase;\r
319                                 xsWriter.WriteTree ((ServiceDescription)o);\r
320                         }\r
321                         \r
322                         protected override object Deserialize (XmlSerializationReader reader)\r
323                         {\r
324                                 ServiceDescriptionReaderBase xsReader = reader as ServiceDescriptionReaderBase;\r
325                                 return xsReader.ReadTree ();\r
326                         }\r
327                         \r
328                         protected override XmlSerializationWriter CreateWriter ()\r
329                         {\r
330                                 return new ServiceDescriptionWriterBase ();\r
331                         }\r
332                         \r
333                         protected override XmlSerializationReader CreateReader ()\r
334                         {\r
335                                 return new ServiceDescriptionReaderBase ();\r
336                         }\r
337                 }               \r
338         }\r
339 }