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