2006-11-02 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Description / ServiceDescriptionImporter.cs
1 // \r
2 // System.Web.Services.Description.ServiceDescriptionImporter.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;\r
33 using System.CodeDom;\r
34 using System.CodeDom.Compiler;\r
35 using System.Web.Services;\r
36 using System.Web.Services.Protocols;\r
37 using System.Web.Services.Description;\r
38 using System.Xml.Serialization;\r
39 using System.Xml;\r
40 using System.Xml.Schema;\r
41 using System.Collections;\r
42 using System.Collections.Specialized;\r
43 using System.Configuration;\r
44 \r
45 namespace System.Web.Services.Description {\r
46         public class ServiceDescriptionImporter {\r
47 \r
48                 #region Fields\r
49 \r
50                 string protocolName;\r
51                 XmlSchemas schemas;\r
52                 ServiceDescriptionCollection serviceDescriptions;\r
53                 ServiceDescriptionImportStyle style;\r
54                 \r
55 #if NET_2_0\r
56                 CodeGenerationOptions options;\r
57                 ICodeGenerator codeGenerator;\r
58                 ImportContext context;\r
59 #endif\r
60 \r
61                 ArrayList importInfo = new ArrayList ();\r
62                 \r
63 \r
64                 #endregion // Fields\r
65 \r
66                 #region Constructors\r
67         \r
68                 public ServiceDescriptionImporter ()\r
69                 {\r
70                         protocolName = String.Empty;\r
71                         schemas = new XmlSchemas ();\r
72                         serviceDescriptions = new ServiceDescriptionCollection ();
73                         serviceDescriptions.SetImporter (this);\r
74                         style = ServiceDescriptionImportStyle.Client;\r
75                 }\r
76                 \r
77                 #endregion // Constructors\r
78 \r
79                 #region Properties\r
80 \r
81                 public string ProtocolName {\r
82                         get { return protocolName; }\r
83                         set { protocolName = value; }\r
84                 }\r
85 \r
86                 public XmlSchemas Schemas {\r
87                         get { return schemas; }\r
88                 }\r
89 \r
90                 public ServiceDescriptionCollection ServiceDescriptions {\r
91                         get { return serviceDescriptions; }\r
92                 }\r
93 \r
94                 public ServiceDescriptionImportStyle Style {\r
95                         get { return style; }\r
96                         set { style = value; }\r
97                 }\r
98                 \r
99 #if NET_2_0\r
100                 [System.Runtime.InteropServices.ComVisible(false)]\r
101                 public CodeGenerationOptions CodeGenerationOptions {\r
102                         get { return options; }\r
103                         set { options = value; }\r
104                 }\r
105                 \r
106                 [System.Runtime.InteropServices.ComVisible(false)]\r
107                 public ICodeGenerator CodeGenerator {\r
108                         get { return codeGenerator; }\r
109                         set { codeGenerator = value; }\r
110                 }\r
111                 \r
112                 \r
113                 internal ImportContext Context {\r
114                         get { return context; }\r
115                         set { context = value; }\r
116                 }\r
117 #endif\r
118         \r
119                 #endregion // Properties\r
120 \r
121                 #region Methods\r
122 \r
123                 public void AddServiceDescription (ServiceDescription serviceDescription, string appSettingUrlKey, string appSettingBaseUrl)\r
124                 {\r
125                         if (appSettingUrlKey != null && appSettingUrlKey == string.Empty && style == ServiceDescriptionImportStyle.Server)\r
126                                 throw new InvalidOperationException ("Cannot set appSettingUrlKey if Style is Server");\r
127
128                         serviceDescriptions.Add (serviceDescription, appSettingUrlKey, appSettingBaseUrl);\r
129                 }\r
130 \r
131                 internal void OnServiceDescriptionAdded (ServiceDescription serviceDescription, string appSettingUrlKey, string appSettingBaseUrl)\r
132                 {\r
133                         ImportInfo info = new ImportInfo (serviceDescription, appSettingUrlKey, appSettingBaseUrl);\r
134                         importInfo.Add (info);\r
135                         \r
136                         if (serviceDescription.Types != null)\r
137                                 schemas.Add (serviceDescription.Types.Schemas);\r
138                 }\r
139 \r
140                 public ServiceDescriptionImportWarnings Import (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit)\r
141                 {\r
142                         ProtocolImporter importer = GetImporter ();\r
143                         \r
144                         if (!importer.Import (this, codeNamespace, codeCompileUnit, importInfo))\r
145                                 throw new Exception ("None of the supported bindings was found");\r
146                                 \r
147                         return importer.Warnings;\r
148                 }\r
149                 \r
150                 ProtocolImporter GetImporter ()\r
151                 {\r
152                         ArrayList importers = GetSupportedImporters ();\r
153                         if (protocolName == null || protocolName == "") protocolName = "Soap";\r
154                         foreach (ProtocolImporter importer in importers) {\r
155                                 if (importer.ProtocolName == protocolName)\r
156                                         return importer;\r
157                         }\r
158                         \r
159                         throw new Exception ("Protocol " + protocolName + " not supported");\r
160                 }\r
161                 \r
162                 ArrayList GetSupportedImporters ()\r
163                 {\r
164                         ArrayList list = new ArrayList ();\r
165                         list.Add (new SoapProtocolImporter ());\r
166                         list.Add (new HttpGetProtocolImporter ());\r
167                         list.Add (new HttpPostProtocolImporter ());\r
168                         return list;\r
169                 }\r
170                 \r
171 #if NET_2_0\r
172 \r
173                 public static StringCollection GenerateWebReferences (\r
174                         WebReferenceCollection webReferences, \r
175                         CodeGenerationOptions options, \r
176                         ServiceDescriptionImportStyle style, \r
177                         ICodeGenerator codeGenerator)\r
178                 {\r
179                         CodeCompileUnit codeCompileUnit = new CodeCompileUnit ();\r
180                         return GenerateWebReferences (webReferences, options, style, codeGenerator, codeCompileUnit, false);\r
181                 }\r
182 \r
183                 [MonoTODO ("verbose?")]\r
184                 public static StringCollection GenerateWebReferences (\r
185                         WebReferenceCollection webReferences, \r
186                         CodeGenerationOptions options, \r
187                         ServiceDescriptionImportStyle style, \r
188                         ICodeGenerator codeGenerator, \r
189                         CodeCompileUnit codeCompileUnit, \r
190                         bool verbose)\r
191                 {\r
192                         StringCollection allWarnings = new StringCollection ();\r
193                         ImportContext context = new ImportContext (new CodeIdentifiers(), true);\r
194                         \r
195                         foreach (WebReference reference in webReferences) \r
196                         {\r
197                                 ServiceDescriptionImporter importer = new ServiceDescriptionImporter ();\r
198                                 importer.CodeGenerator = codeGenerator;\r
199                                 importer.CodeGenerationOptions = options;\r
200                                 importer.Context = context;\r
201                                 importer.Style = style;\r
202                                 importer.ProtocolName = reference.ProtocolName;\r
203                                 \r
204                                 importer.AddReference (reference);\r
205                                 \r
206                                 reference.Warnings = importer.Import (reference.ProxyCode, codeCompileUnit);\r
207                                 reference.SetValidationWarnings (context.Warnings);\r
208                                 foreach (string s in context.Warnings)\r
209                                         allWarnings.Add (s);\r
210 \r
211                                 context.Warnings.Clear ();\r
212                         }\r
213 \r
214                         return allWarnings;\r
215                 }\r
216                 \r
217                 internal void AddReference (WebReference reference)\r
218                 {\r
219                         foreach (object doc in reference.Documents.Values)\r
220                         {\r
221                                 if (doc is ServiceDescription) {\r
222                                         ServiceDescription service = (ServiceDescription) doc;\r
223                                         ImportInfo info = new ImportInfo (service, reference);\r
224                                         importInfo.Add (info);\r
225                                         serviceDescriptions.Add (service);\r
226                                         \r
227                                         if (service.Types != null)\r
228                                                 schemas.Add (service.Types.Schemas);\r
229                                 }\r
230                                 else if (doc is XmlSchema) {\r
231                                         schemas.Add ((XmlSchema) doc);\r
232                                 }\r
233                         }\r
234                 }\r
235                 \r
236 #endif\r
237 \r
238 #endregion\r
239         }\r
240 \r
241         internal class ImportInfo\r
242         {\r
243                 string _appSettingUrlKey;\r
244                 string _appSettingBaseUrl;\r
245                 ServiceDescription _serviceDescription;\r
246                 \r
247                 public WebReference _reference;\r
248                 \r
249                 public ImportInfo (ServiceDescription serviceDescription, string appSettingUrlKey, string appSettingBaseUrl)\r
250                 {\r
251                         _serviceDescription = serviceDescription;\r
252                         _appSettingUrlKey = appSettingUrlKey;\r
253                         _appSettingBaseUrl = appSettingBaseUrl;\r
254                 }\r
255                 \r
256                 public ImportInfo (ServiceDescription serviceDescription, WebReference reference)\r
257                 {\r
258                         _reference = reference;\r
259                         _serviceDescription = serviceDescription;\r
260                 }\r
261                 \r
262                 public WebReference Reference {\r
263                         get { return _reference; }\r
264                 }\r
265                 \r
266                 public ServiceDescription ServiceDescription {\r
267                         get { return _serviceDescription; }\r
268                 }\r
269                 \r
270                 public string AppSettingUrlKey {\r
271                         get {\r
272                                 if (_reference != null) return _reference.AppSettingUrlKey;\r
273                                 else return _appSettingUrlKey;\r
274                         }\r
275                         set {\r
276                                 _appSettingUrlKey = value;\r
277                         }\r
278                 }\r
279                 \r
280                 public string AppSettingBaseUrl {\r
281                         get {\r
282                                 if (_reference != null) return _reference.AppSettingBaseUrl;\r
283                                 else return _appSettingBaseUrl;\r
284                         }\r
285                         set {\r
286                                 _appSettingBaseUrl = value;\r
287                         }\r
288                 }\r
289         }\r
290 \r
291 }