2 /// MonoWSDL.cs -- a WSDL to proxy code generator.
\r
4 /// Author: Erik LeBel (eriklebel@yahoo.ca)
\r
5 /// Lluis Sanchez (lluis@novell.com)
\r
7 /// Copyright (C) 2003, Erik LeBel,
\r
12 using System.Xml.Serialization;
\r
13 using System.Xml.Schema;
\r
14 using System.Collections;
\r
15 using System.Collections.Specialized;
\r
16 using System.CodeDom;
\r
17 using System.CodeDom.Compiler;
\r
20 using System.Web.Services.Description;
\r
21 using System.Web.Services.Discovery;
\r
22 using System.Web.Services;
\r
24 using Microsoft.CSharp;
\r
26 namespace Mono.WebServices
\r
30 string ProductId = "Web Services Description Language Utility\nMono Framework v" + Environment.Version;
\r
31 const string UsageMessage =
\r
32 "wsdl [options] {path | URL} {path | URL} ...\n\n"
\r
33 + " -d, -domain:domain Domain of username for server authentication.\n"
\r
34 + " -l, -language:language Language of generated code. Allowed CS (default)\n"
\r
35 + " and VB. You can also specify the fully qualified\n"
\r
36 + " name of a class that implements the\n"
\r
37 + " System.CodeDom.Compiler.CodeDomProvider Class.\n"
\r
38 + " -n, -namespace:ns The namespace of the generated code, default\n"
\r
39 + " namespace if none.\n"
\r
40 + " -nologo Surpress the startup logo.\n"
\r
41 + " -o, -out:filename The target file for generated code.\n"
\r
42 + " -p, -password:pwd Password used to contact the server.\n"
\r
43 + " -protocol:protocol Protocol to implement. Allowed: Soap (default),\n"
\r
44 + " HttpGet or HttpPost.\n"
\r
45 + " -fields Generate fields instead of properties in data\n"
\r
47 + " -server Generate server instead of client proxy code.\n"
\r
48 + " -u, -username:username Username used to contact the server.\n"
\r
49 + " -proxy:url Address of the proxy.\n"
\r
50 + " -pu, -proxyusername:username Username used to contact the proxy.\n"
\r
51 + " -pp, -proxypassword:pwd Password used to contact the proxy.\n"
\r
52 + " -pd, -proxydomain:domain Domain of username for proxy authentication.\n"
\r
53 + " -urlkey, -appsettingurlkey:key Configuration key that contains the default\n"
\r
54 + " url for the generated WS proxy.\n"
\r
55 + " -baseurl, -appsettingbaseurl:url Base url to use when constructing the\n"
\r
57 + " -sample:[binding/]operation Display a sample SOAP request and response.\n"
\r
58 + " -? Display this message\n"
\r
60 + "Options can be of the forms -option, --option or /option\n";
\r
62 ArrayList descriptions = new ArrayList ();
\r
63 ArrayList schemas = new ArrayList ();
\r
69 string proxyAddress;
\r
71 string proxyPassword;
\r
72 string proxyUsername;
\r
77 string applicationSignature;
\r
78 string appSettingURLKey;
\r
79 string appSettingBaseURL;
\r
80 string language = "CS";
\r
83 string protocol = "Soap";
\r
84 ServiceDescriptionImportStyle style;
\r
85 CodeGenerationOptions options = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;
\r
88 StringCollection urls = new StringCollection ();
\r
92 /// Application entry point.
\r
95 public static int Main(string[] args)
\r
97 Driver d = new Driver();
\r
103 applicationSignature = ProductId;
\r
106 int Run (string[] args)
\r
110 // parse command line arguments
\r
111 foreach (string argument in args)
\r
112 ImportArgument(argument);
\r
114 if (noLogo == false)
\r
115 Console.WriteLine(ProductId);
\r
117 if (help || urls.Count == 0)
\r
119 Console.WriteLine(UsageMessage);
\r
123 CodeCompileUnit codeUnit = new CodeCompileUnit();
\r
124 CodeNamespace proxyCode = GetCodeNamespace();
\r
125 codeUnit.Namespaces.Add (proxyCode);
\r
127 WebReferenceCollection references = new WebReferenceCollection ();
\r
128 foreach (string murl in urls)
\r
130 DiscoveryClientProtocol dcc = CreateClient ();
\r
133 if (!url.StartsWith ("http://") && !url.StartsWith ("https://") && !url.StartsWith ("file://"))
\r
134 url = new Uri (Path.GetFullPath (url)).ToString ();
\r
136 dcc.DiscoverAny (url);
\r
139 WebReference reference = new WebReference (dcc.Documents, proxyCode, protocol, appSettingURLKey, appSettingBaseURL);
\r
140 references.Add (reference);
\r
142 if (sampleSoap != null)
\r
143 ConsoleSampleGenerator.Generate (descriptions, schemas, sampleSoap, protocol);
\r
146 if (sampleSoap != null)
\r
149 // generate the code
\r
150 if (GenerateCode (references, codeUnit))
\r
155 catch (Exception exception)
\r
157 Console.WriteLine("Error: {0}", exception.Message);
\r
159 // Supress this except for when debug is enabled
\r
160 Console.WriteLine("Stack:\n {0}", exception.StackTrace);
\r
167 /// Generate code for the specified ServiceDescription.
\r
170 public bool GenerateCode (WebReferenceCollection references, CodeCompileUnit codeUnit)
\r
172 bool hasWarnings = false;
\r
174 CodeDomProvider provider = GetProvider();
\r
175 ICodeGenerator generator = provider.CreateGenerator();
\r
177 StringCollection validationWarnings;
\r
178 validationWarnings = ServiceDescriptionImporter.GenerateWebReferences (references, options, style, generator, codeUnit, verbose);
\r
180 for (int n=0; n<references.Count; n++)
\r
182 WebReference wr = references [n];
\r
184 BasicProfileViolationCollection violations = new BasicProfileViolationCollection ();
\r
185 if (!WebServicesInteroperability.CheckConformance (WsiClaims.BP10, wr, violations)) {
\r
186 wr.Warnings |= ServiceDescriptionImportWarnings.WsiConformance;
\r
189 if (wr.Warnings != 0)
\r
191 if (!hasWarnings) {
\r
192 WriteText ("", 0, 0);
\r
193 WriteText ("There where some warnings while generating the code:", 0, 0);
\r
196 WriteText ("", 0, 0);
\r
197 WriteText (urls[n], 2, 2);
\r
199 if ((wr.Warnings & ServiceDescriptionImportWarnings.WsiConformance) > 0) {
\r
200 WriteText ("- This web reference does not conform to WS-I Basic Profile v1.0", 4, 6);
\r
201 foreach (BasicProfileViolation vio in violations) {
\r
202 WriteText (vio.NormativeStatement + ": " + vio.Details, 8, 8);
\r
203 foreach (string ele in vio.Elements)
\r
204 WriteText ("* " + ele, 10, 12);
\r
208 if ((wr.Warnings & ServiceDescriptionImportWarnings.NoCodeGenerated) > 0)
\r
209 WriteText ("- WARNING: No proxy class was generated", 4, 6);
\r
210 if ((wr.Warnings & ServiceDescriptionImportWarnings.NoMethodsGenerated) > 0)
\r
211 WriteText ("- WARNING: The proxy class generated includes no methods", 4, 6);
\r
212 if ((wr.Warnings & ServiceDescriptionImportWarnings.OptionalExtensionsIgnored) > 0)
\r
213 WriteText ("- WARNING: At least one optional extension has been ignored", 4, 6);
\r
214 if ((wr.Warnings & ServiceDescriptionImportWarnings.RequiredExtensionsIgnored) > 0)
\r
215 WriteText ("- WARNING: At least one necessary extension has been ignored", 4, 6);
\r
216 if ((wr.Warnings & ServiceDescriptionImportWarnings.UnsupportedBindingsIgnored) > 0)
\r
217 WriteText ("- WARNING: At least one binding is of an unsupported type and has been ignored", 4, 6);
\r
218 if ((wr.Warnings & ServiceDescriptionImportWarnings.UnsupportedOperationsIgnored) > 0)
\r
219 WriteText ("- WARNING: At least one operation is of an unsupported type and has been ignored", 4, 6);
\r
221 hasWarnings = true;
\r
225 if (hasWarnings) WriteText ("",0,0);
\r
227 string filename = outFilename;
\r
228 bool hasBindings = false;
\r
230 foreach (object doc in references[0].Documents.Values)
\r
232 ServiceDescription desc = doc as ServiceDescription;
\r
233 if (desc == null) continue;
\r
235 if (desc.Services.Count > 0 && filename == null)
\r
236 filename = desc.Services[0].Name + "." + provider.FileExtension;
\r
238 if (desc.Bindings.Count > 0 || desc.Services.Count > 0)
\r
239 hasBindings = true;
\r
242 if (filename == null)
\r
243 filename = "output." + provider.FileExtension;
246 WriteText ("Writing file '" + filename + "'", 0, 0);
\r
247 StreamWriter writer = new StreamWriter(filename);
\r
249 CodeGeneratorOptions compilerOptions = new CodeGeneratorOptions();
\r
250 generator.GenerateCodeFromCompileUnit (codeUnit, writer, compilerOptions);
\r
254 return hasWarnings;
\r
259 /// Create the CodeNamespace with the generator's signature commented in.
\r
262 CodeNamespace GetCodeNamespace()
\r
264 CodeNamespace codeNamespace = new CodeNamespace(ns);
\r
266 if (applicationSignature != null)
\r
268 codeNamespace.Comments.Add(new CodeCommentStatement("\n This source code was auto-generated by " + applicationSignature + "\n"));
\r
271 return codeNamespace;
\r
277 void WriteCodeUnit(CodeCompileUnit codeUnit, string serviceName)
\r
279 CodeDomProvider provider = GetProvider();
\r
280 ICodeGenerator generator = provider.CreateGenerator();
\r
281 CodeGeneratorOptions options = new CodeGeneratorOptions();
\r
284 if (outFilename != null)
\r
285 filename = outFilename;
\r
287 filename = serviceName + "." + provider.FileExtension;
\r
289 Console.WriteLine ("Writing file '{0}'", filename);
\r
290 StreamWriter writer = new StreamWriter(filename);
\r
291 generator.GenerateCodeFromCompileUnit(codeUnit, writer, options);
\r
297 /// Fetch the Code Provider for the language specified by the 'language' members.
\r
300 private CodeDomProvider GetProvider()
\r
302 CodeDomProvider provider;
\r
304 switch (language.ToUpper ()) {
\r
306 provider = new CSharpCodeProvider ();
\r
309 provider = new Microsoft.VisualBasic.VBCodeProvider ();
\r
312 Type type = Type.GetType(language);
\r
313 if (type != null) {
\r
314 return (CodeDomProvider) Activator.CreateInstance (type);
\r
316 throw new Exception ("Unknown language");
\r
325 /// Interperet the command-line arguments and configure the relavent components.
\r
328 void ImportArgument(string argument)
\r
330 string optionValuePair;
\r
332 if (argument.StartsWith("--"))
\r
334 optionValuePair = argument.Substring(2);
\r
336 else if (argument.StartsWith("/") || argument.StartsWith("-"))
\r
338 optionValuePair = argument.Substring(1);
\r
342 urls.Add (argument);
\r
349 int indexOfEquals = optionValuePair.IndexOf(':');
\r
350 if (indexOfEquals > 0)
\r
352 option = optionValuePair.Substring(0, indexOfEquals);
\r
353 value = optionValuePair.Substring(indexOfEquals + 1);
\r
357 option = optionValuePair;
\r
363 case "appsettingurlkey":
\r
365 appSettingURLKey = value;
\r
368 case "appsettingbaseurl":
\r
370 appSettingBaseURL = value;
\r
394 outFilename = value;
\r
407 proxyAddress = value;
\r
410 case "proxydomain":
\r
412 proxyDomain = value;
\r
415 case "proxypassword":
\r
417 proxyPassword = value;
\r
420 case "proxyusername":
\r
422 proxyUsername = value;
\r
426 style = ServiceDescriptionImportStyle.Server;
\r
439 options &= ~CodeGenerationOptions.GenerateProperties;
\r
443 sampleSoap = value;
\r
451 if (argument.StartsWith ("/") && argument.IndexOfAny (Path.InvalidPathChars) == -1) {
\r
452 urls.Add (argument);
\r
456 throw new Exception("Unknown option " + option);
\r
460 DiscoveryClientProtocol CreateClient ()
\r
462 DiscoveryClientProtocol dcc = new DiscoveryClientProtocol ();
\r
464 if (username != null || password != null || domain != null)
\r
466 NetworkCredential credentials = new NetworkCredential();
\r
468 if (username != null)
\r
469 credentials.UserName = username;
\r
471 if (password != null)
\r
472 credentials.Password = password;
\r
474 if (domain != null)
\r
475 credentials.Domain = domain;
\r
477 dcc.Credentials = credentials;
\r
480 if (proxyAddress != null)
\r
482 WebProxy proxy = new WebProxy (proxyAddress);
\r
483 if (proxyUsername != null || proxyPassword != null || proxyDomain != null)
\r
485 NetworkCredential credentials = new NetworkCredential();
\r
487 if (proxyUsername != null)
\r
488 credentials.UserName = proxyUsername;
\r
490 if (proxyPassword != null)
\r
491 credentials.Password = proxyPassword;
\r
493 if (proxyDomain != null)
\r
494 credentials.Domain = proxyDomain;
\r
496 proxy.Credentials = credentials;
\r
503 static void WriteText (string text, int initialLeftMargin, int leftMargin)
\r
506 int margin = initialLeftMargin;
\r
510 Console.WriteLine ();
\r
514 while (n < text.Length)
\r
517 int lastWhite = -1;
\r
519 while (col < maxCols && n < text.Length) {
\r
520 if (char.IsWhiteSpace (text[n]))
\r
526 if (lastWhite == -1 || col < maxCols)
\r
528 else if (col >= maxCols)
\r
531 Console.WriteLine (new String (' ', margin) + text.Substring (sn, lastWhite - sn));
\r
532 margin = leftMargin;
\r