2 /// MonoWSDL.cs -- a WSDL to proxy code generator.
\r
4 /// Author: Erik LeBel (eriklebel@yahoo.ca)
\r
5 /// Lluis Sanchez (lluis@ximian.com)
\r
7 /// Copyright (C) 2003, Erik LeBel,
\r
12 using System.Xml.Schema;
\r
13 using System.Collections;
\r
14 using System.CodeDom;
\r
15 using System.CodeDom.Compiler;
\r
18 using System.Web.Services.Description;
\r
19 using System.Web.Services.Discovery;
\r
21 using Microsoft.CSharp;
\r
23 namespace Mono.WebServices
\r
27 /// Source code generator.
\r
30 class SourceGenerator
\r
32 string applicationSiganture = null;
\r
33 string appSettingURLKey = null;
\r
34 string appSettingBaseURL = null;
\r
35 string language = "CS";
\r
37 string outFilename = null;
\r
38 string protocol = "Soap";
\r
39 bool server = false;
\r
44 public string Language
\r
47 set { language = value; }
\r
53 public string Namespace
\r
60 /// The file to contain the generated code.
\r
63 public string Filename
\r
65 set { outFilename = value; }
\r
71 public string Protocol
\r
74 set { protocol = value; }
\r
75 get { return protocol; }
\r
81 public string ApplicationSignature
\r
83 set { applicationSiganture = value; }
\r
89 public string AppSettingURLKey
\r
91 set { appSettingURLKey = value; }
\r
97 public string AppSettingBaseURL
\r
99 set { appSettingBaseURL = value; }
\r
107 set { server = value; }
\r
112 /// Generate code for the specified ServiceDescription.
\r
115 public bool GenerateCode (ArrayList descriptions, ArrayList schemas)
\r
117 // FIXME iterate over each serviceDescription.Services?
\r
118 CodeNamespace codeNamespace = GetCodeNamespace();
\r
119 CodeCompileUnit codeUnit = new CodeCompileUnit();
\r
120 bool hasWarnings = false;
\r
122 codeUnit.Namespaces.Add(codeNamespace);
\r
124 ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
\r
125 importer.ProtocolName = protocol;
\r
127 importer.Style = ServiceDescriptionImportStyle.Server;
\r
129 foreach (ServiceDescription sd in descriptions)
\r
130 importer.AddServiceDescription(sd, appSettingURLKey, appSettingBaseURL);
\r
132 foreach (XmlSchema sc in schemas)
\r
133 importer.Schemas.Add (sc);
\r
135 ServiceDescriptionImportWarnings warnings = importer.Import(codeNamespace, codeUnit);
\r
138 if ((warnings & ServiceDescriptionImportWarnings.NoCodeGenerated) > 0)
\r
139 Console.WriteLine ("WARNING: No proxy class was generated");
\r
140 if ((warnings & ServiceDescriptionImportWarnings.NoMethodsGenerated) > 0)
\r
141 Console.WriteLine ("WARNING: The proxy class generated includes no methods");
\r
142 if ((warnings & ServiceDescriptionImportWarnings.OptionalExtensionsIgnored) > 0)
\r
143 Console.WriteLine ("WARNING: At least one optional extension has been ignored");
\r
144 if ((warnings & ServiceDescriptionImportWarnings.RequiredExtensionsIgnored) > 0)
\r
145 Console.WriteLine ("WARNING: At least one necessary extension has been ignored");
\r
146 if ((warnings & ServiceDescriptionImportWarnings.UnsupportedBindingsIgnored) > 0)
\r
147 Console.WriteLine ("WARNING: At least one binding is of an unsupported type and has been ignored");
\r
148 if ((warnings & ServiceDescriptionImportWarnings.UnsupportedOperationsIgnored) > 0)
\r
149 Console.WriteLine ("WARNING: At least one operation is of an unsupported type and has been ignored");
\r
150 hasWarnings = true;
\r
153 ServiceDescription rootDesc = null;
154 foreach (ServiceDescription desc in descriptions) {
155 if (desc.Services.Count > 0) {
161 if (rootDesc != null)
\r
163 string serviceName = rootDesc.Services[0].Name;
\r
164 WriteCodeUnit(codeUnit, serviceName);
\r
167 return hasWarnings;
\r
172 /// Create the CodeNamespace with the generator's signature commented in.
\r
175 CodeNamespace GetCodeNamespace()
\r
177 CodeNamespace codeNamespace = new CodeNamespace(ns);
\r
179 if (applicationSiganture != null)
\r
181 codeNamespace.Comments.Add(new CodeCommentStatement("\n This source code was auto-generated by " + applicationSiganture + "\n"));
\r
184 return codeNamespace;
\r
190 void WriteCodeUnit(CodeCompileUnit codeUnit, string serviceName)
\r
192 CodeDomProvider provider = GetProvider();
\r
193 ICodeGenerator generator = provider.CreateGenerator();
\r
194 CodeGeneratorOptions options = new CodeGeneratorOptions();
\r
197 if (outFilename != null)
\r
198 filename = outFilename;
\r
200 filename = serviceName + "." + provider.FileExtension;
\r
202 Console.WriteLine ("Writing file '{0}'", filename);
\r
203 StreamWriter writer = new StreamWriter(filename);
\r
204 generator.GenerateCodeFromCompileUnit(codeUnit, writer, options);
\r
210 /// Fetch the Code Provider for the language specified by the 'language' members.
\r
213 private CodeDomProvider GetProvider()
\r
215 // FIXME these should be loaded dynamically using reflection
\r
216 CodeDomProvider provider;
\r
218 switch (language.ToUpper())
\r
221 provider = new CSharpCodeProvider();
\r
225 provider = new Microsoft.VisualBasic.VBCodeProvider();
\r
229 throw new Exception("Unknow language");
\r
238 /// monoWSDL's main application driver. Reads the command-line arguments and dispatch the
\r
239 /// appropriate handlers.
\r
242 public class Driver
\r
244 const string ProductId = "Mono Web Services Description Language Utility";
\r
245 const string UsageMessage =
\r
246 "wsdl [options] {path | URL} \n\n"
\r
247 + " -d, -domain:domain Domain of username for server authentication.\n"
\r
248 + " -l, -language:language Language of generated code. Allowed CS (default)\n"
\r
250 + " -n, -namespace:ns The namespace of the generated code, default\n"
\r
251 + " namespace if none.\n"
\r
252 + " -nologo Surpress the startup logo.\n"
\r
253 + " -o, -out:filename The target file for generated code.\n"
\r
254 + " -p, -password:pwd Password used to contact the server.\n"
\r
255 + " -protocol:protocol Protocol to implement. Allowed: Soap (default),\n"
\r
256 + " HttpGet or HttpPost.\n"
\r
257 + " -server Generate server instead of client proxy code.\n"
\r
258 + " -u, -username:username Username used to contact the server.\n"
\r
259 + " -proxy:url Address of the proxy.\n"
\r
260 + " -pu, -proxyusername:username Username used to contact the proxy.\n"
\r
261 + " -pp, -proxypassword:pwd Password used to contact the proxy.\n"
\r
262 + " -pd, -proxydomain:domain Domain of username for proxy authentication.\n"
\r
263 + " -urlkey, -appsettingurlkey:key Configuration key that contains the default\n"
\r
264 + " url for the generated WS proxy.\n"
\r
265 + " -baseurl, -appsettingbaseurl:url Base url to use when constructing the\n"
\r
266 + " service url.\n"
\r
267 + " -sample:[binding/]operation Display a sample SOAP request and response.\n"
\r
268 + " -? Display this message\n"
\r
270 + "Options can be of the forms -option, --option or /option\n";
\r
272 SourceGenerator generator = null;
\r
274 ArrayList descriptions = new ArrayList ();
\r
275 ArrayList schemas = new ArrayList ();
\r
277 bool noLogo = false;
\r
279 bool hasURL = false;
\r
280 string sampleSoap = null;
\r
282 string proxyAddress = null;
\r
283 string proxyDomain = null;
\r
284 string proxyPassword = null;
\r
285 string proxyUsername = null;
\r
294 /// Initialize the document retrieval component and the source code generator.
\r
299 generator = new SourceGenerator();
\r
300 generator.ApplicationSignature = ProductId;
\r
305 /// Interperet the command-line arguments and configure the relavent components.
\r
308 void ImportArgument(string argument)
\r
310 string optionValuePair;
\r
312 if (argument.StartsWith("--"))
\r
314 optionValuePair = argument.Substring(2);
\r
316 else if (argument.StartsWith("/") || argument.StartsWith("-"))
\r
318 optionValuePair = argument.Substring(1);
\r
330 int indexOfEquals = optionValuePair.IndexOf(':');
\r
331 if (indexOfEquals > 0)
\r
333 option = optionValuePair.Substring(0, indexOfEquals);
\r
334 value = optionValuePair.Substring(indexOfEquals + 1);
\r
338 option = optionValuePair;
\r
344 case "appsettingurlkey":
\r
346 generator.AppSettingURLKey = value;
\r
349 case "appsettingbaseurl":
\r
351 generator.AppSettingBaseURL = value;
\r
361 generator.Language = value;
\r
366 generator.Namespace = value;
\r
375 generator.Filename = value;
\r
384 generator.Protocol = value;
\r
388 proxyAddress = value;
\r
391 case "proxydomain":
\r
393 proxyDomain = value;
\r
396 case "proxypassword":
\r
398 proxyPassword = value;
\r
401 case "proxyusername":
\r
403 proxyUsername = value;
\r
407 generator.Server = true;
\r
416 sampleSoap = value;
\r
424 throw new Exception("Unknown option " + option);
\r
428 DiscoveryClientProtocol CreateClient ()
\r
430 DiscoveryClientProtocol dcc = new DiscoveryClientProtocol ();
\r
432 if (username != null || password != null || domain != null)
\r
434 NetworkCredential credentials = new NetworkCredential();
\r
436 if (username != null)
\r
437 credentials.UserName = username;
\r
439 if (password != null)
\r
440 credentials.Password = password;
\r
442 if (domain != null)
\r
443 credentials.Domain = domain;
\r
445 dcc.Credentials = credentials;
\r
448 if (proxyAddress != null)
\r
450 WebProxy proxy = new WebProxy (proxyAddress);
\r
451 if (proxyUsername != null || proxyPassword != null || proxyDomain != null)
\r
453 NetworkCredential credentials = new NetworkCredential();
\r
455 if (proxyUsername != null)
\r
456 credentials.UserName = proxyUsername;
\r
458 if (proxyPassword != null)
\r
459 credentials.Password = proxyPassword;
\r
461 if (proxyDomain != null)
\r
462 credentials.Domain = proxyDomain;
\r
464 proxy.Credentials = credentials;
\r
473 /// Driver's main control flow:
\r
474 /// - parse arguments
\r
475 /// - report required messages
\r
476 /// - terminate if no input
\r
477 /// - report errors
\r
480 int Run(string[] args)
\r
484 // parse command line arguments
\r
485 foreach (string argument in args)
\r
487 ImportArgument(argument);
\r
490 if (noLogo == false)
\r
491 Console.WriteLine(ProductId);
\r
493 if (help || !hasURL)
\r
495 Console.WriteLine(UsageMessage);
\r
499 DiscoveryClientProtocol dcc = CreateClient ();
\r
501 dcc.DiscoverAny (url);
\r
504 foreach (object doc in dcc.Documents.Values)
\r
506 if (doc is ServiceDescription)
\r
507 descriptions.Add ((ServiceDescription)doc);
\r
508 else if (doc is XmlSchema)
\r
509 schemas.Add ((XmlSchema)doc);
\r
512 if (descriptions.Count == 0)
\r
513 throw new Exception ("No WSDL document was found at the url " + url);
\r
515 if (sampleSoap != null)
\r
517 ConsoleSampleGenerator.Generate (descriptions, schemas, sampleSoap, generator.Protocol);
\r
521 // generate the code
\r
522 if (generator.GenerateCode (descriptions, schemas))
\r
527 catch (Exception exception)
\r
529 Console.WriteLine("Error: {0}", exception.Message);
\r
530 // FIXME: surpress this except for when debug is enabled
\r
531 //Console.WriteLine("Stack:\n {0}", exception.StackTrace);
\r
538 /// Application entry point.
\r
541 public static int Main(string[] args)
\r
543 Driver d = new Driver();
\r
544 return d.Run(args);
\r