2010-04-15 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Web.Services / System.Web.Services.Description / ServiceDescriptionImporter.cs
blobe61a93ca8566bd48b1490ddcdf96466e251bc68b
1 //
2 // System.Web.Services.Description.ServiceDescriptionImporter.cs
3 //
4 // Author:
5 // Tim Coleman (tim@timcoleman.com)
6 // Lluis Sanchez Gual (lluis@ximian.com)
7 //
8 // Copyright (C) Tim Coleman, 2002
9 //
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.
32 using System;
33 using System.CodeDom;
34 using System.CodeDom.Compiler;
35 using System.Web.Services;
36 using System.Web.Services.Protocols;
37 using System.Web.Services.Description;
38 using System.Xml.Serialization;
39 using System.Xml;
40 using System.Xml.Schema;
41 using System.Collections;
42 using System.Collections.Specialized;
43 using System.Configuration;
45 #if !MONOTOUCH
46 using Microsoft.CSharp;
47 #endif
49 namespace System.Web.Services.Description {
50 public class ServiceDescriptionImporter {
52 #region Fields
54 string protocolName;
55 XmlSchemas schemas;
56 ServiceDescriptionCollection serviceDescriptions;
57 ServiceDescriptionImportStyle style;
59 #if NET_2_0 && !MONOTOUCH
60 CodeGenerationOptions options;
61 CodeDomProvider codeGenerator = new CSharpCodeProvider ();
62 ImportContext context;
63 #endif
65 ArrayList importInfo = new ArrayList ();
68 #endregion // Fields
70 #region Constructors
72 public ServiceDescriptionImporter ()
74 protocolName = String.Empty;
75 schemas = new XmlSchemas ();
76 serviceDescriptions = new ServiceDescriptionCollection ();
77 serviceDescriptions.SetImporter (this);
78 style = ServiceDescriptionImportStyle.Client;
81 #endregion // Constructors
83 #region Properties
85 public string ProtocolName {
86 get { return protocolName; }
87 set { protocolName = value; }
90 public XmlSchemas Schemas {
91 get { return schemas; }
94 public ServiceDescriptionCollection ServiceDescriptions {
95 get { return serviceDescriptions; }
98 public ServiceDescriptionImportStyle Style {
99 get { return style; }
100 set { style = value; }
103 #if NET_2_0 && !MONOTOUCH
104 [System.Runtime.InteropServices.ComVisible(false)]
105 public CodeGenerationOptions CodeGenerationOptions {
106 get { return options; }
107 set { options = value; }
110 [System.Runtime.InteropServices.ComVisible(false)]
111 public CodeDomProvider CodeGenerator {
112 get { return codeGenerator; }
113 set { codeGenerator = value; }
117 internal ImportContext Context {
118 get { return context; }
119 set { context = value; }
121 #endif
123 #endregion // Properties
125 #region Methods
127 public void AddServiceDescription (ServiceDescription serviceDescription, string appSettingUrlKey, string appSettingBaseUrl)
129 if (appSettingUrlKey != null && appSettingUrlKey == string.Empty && style == ServiceDescriptionImportStyle.Server)
130 throw new InvalidOperationException ("Cannot set appSettingUrlKey if Style is Server");
132 OnServiceDescriptionAdded (serviceDescription, appSettingUrlKey, appSettingBaseUrl);
135 internal void OnServiceDescriptionAdded (ServiceDescription serviceDescription, string appSettingUrlKey, string appSettingBaseUrl)
137 serviceDescriptions.Add (serviceDescription);
138 ImportInfo info = new ImportInfo (serviceDescription, appSettingUrlKey, appSettingBaseUrl);
139 importInfo.Add (info);
141 if (serviceDescription.Types != null)
142 schemas.Add (serviceDescription.Types.Schemas);
145 #if !MONOTOUCH
146 public ServiceDescriptionImportWarnings Import (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit)
148 foreach (ProtocolImporter importer in GetSupportedImporters ()) {
149 if ((protocolName != null && protocolName.Length != 0) && String.Compare (importer.ProtocolName, protocolName, true) != 0)
150 continue;
151 if (importer.Import (this, codeNamespace, importInfo))
152 return importer.Warnings;
154 throw new Exception ("None of the supported bindings was found");
157 ArrayList GetSupportedImporters ()
159 ArrayList list = new ArrayList ();
160 list.Add (new SoapProtocolImporter ());
161 #if NET_2_0
162 list.Add (new Soap12ProtocolImporter ());
163 #endif
164 list.Add (new HttpGetProtocolImporter ());
165 list.Add (new HttpPostProtocolImporter ());
166 return list;
168 #endif
170 #if NET_2_0 && !MONOTOUCH
172 [MonoTODO] // where to use Verbose and Extensions in options?
173 public static StringCollection GenerateWebReferences (
174 WebReferenceCollection webReferences,
175 CodeDomProvider codeGenerator,
176 CodeCompileUnit codeCompileUnit,
177 WebReferenceOptions options)
179 StringCollection allWarnings = new StringCollection ();
180 ImportContext context = new ImportContext (new CodeIdentifiers(), true);
182 foreach (WebReference reference in webReferences)
184 ServiceDescriptionImporter importer = new ServiceDescriptionImporter ();
185 if (codeGenerator != null)
186 importer.CodeGenerator = codeGenerator;
187 importer.CodeGenerationOptions = options.CodeGenerationOptions;
188 importer.Context = context;
189 importer.Style = options.Style;
190 importer.ProtocolName = reference.ProtocolName;
192 importer.AddReference (reference);
194 reference.Warnings = importer.Import (reference.ProxyCode, codeCompileUnit);
195 reference.SetValidationWarnings (context.Warnings);
196 foreach (string s in context.Warnings)
197 allWarnings.Add (s);
199 context.Warnings.Clear ();
202 return allWarnings;
205 internal void AddReference (WebReference reference)
207 foreach (object doc in reference.Documents.Values)
209 if (doc is ServiceDescription) {
210 ServiceDescription service = (ServiceDescription) doc;
211 ImportInfo info = new ImportInfo (service, reference);
212 importInfo.Add (info);
213 serviceDescriptions.Add (service);
215 if (service.Types != null)
216 schemas.Add (service.Types.Schemas);
218 else if (doc is XmlSchema) {
219 schemas.Add ((XmlSchema) doc);
224 #endif
226 #endregion
229 internal class ImportInfo
231 string _appSettingUrlKey;
232 string _appSettingBaseUrl;
233 ServiceDescription _serviceDescription;
235 public WebReference _reference;
237 public ImportInfo (ServiceDescription serviceDescription, string appSettingUrlKey, string appSettingBaseUrl)
239 _serviceDescription = serviceDescription;
240 _appSettingUrlKey = appSettingUrlKey;
241 _appSettingBaseUrl = appSettingBaseUrl;
244 public ImportInfo (ServiceDescription serviceDescription, WebReference reference)
246 _reference = reference;
247 _serviceDescription = serviceDescription;
250 public WebReference Reference {
251 get { return _reference; }
254 public ServiceDescription ServiceDescription {
255 get { return _serviceDescription; }
258 public string AppSettingUrlKey {
259 get {
260 if (_reference != null) return _reference.AppSettingUrlKey;
261 else return _appSettingUrlKey;
263 set {
264 _appSettingUrlKey = value;
268 public string AppSettingBaseUrl {
269 get {
270 if (_reference != null) return _reference.AppSettingBaseUrl;
271 else return _appSettingBaseUrl;
273 set {
274 _appSettingBaseUrl = value;