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
14 using System.Xml.Serialization;
\r
15 using System.Xml.Schema;
\r
16 using System.Collections;
\r
17 using System.Collections.Specialized;
\r
18 using System.CodeDom;
\r
19 using System.CodeDom.Compiler;
\r
22 using System.Web.Services.Description;
\r
23 using System.Web.Services.Discovery;
\r
24 using System.Web.Services;
\r
26 using Microsoft.CSharp;
\r
28 namespace Mono.WebServices
\r
32 string ProductId = "Web Services Description Language Utility\nMono Framework v" + Environment.Version;
\r
33 const string UsageMessage =
\r
34 "wsdl [options] {path | URL} {path | URL} ...\n\n"
\r
35 + " -d, -domain:domain Domain of username for server authentication.\n"
\r
36 + " -l, -language:language Language of generated code. Allowed CS (default)\n"
\r
37 + " and VB. You can also specify the fully qualified\n"
\r
38 + " name of a class that implements the\n"
\r
39 + " System.CodeDom.Compiler.CodeDomProvider Class.\n"
\r
40 + " -n, -namespace:ns The namespace of the generated code, default\n"
\r
41 + " namespace if none.\n"
\r
42 + " -nologo Surpress the startup logo.\n"
\r
43 + " -o, -out:filename The target file for generated code.\n"
\r
44 + " -p, -password:pwd Password used to contact the server.\n"
\r
45 + " -protocol:protocol Protocol to implement. Allowed: Soap (default),\n"
\r
46 + " HttpGet or HttpPost.\n"
\r
47 + " -fields Generate fields instead of properties in data\n"
\r
49 + " -server Generate server instead of client proxy code.\n"
\r
50 + " -u, -username:username Username used to contact the server.\n"
\r
51 + " -proxy:url Address of the proxy.\n"
\r
52 + " -pu, -proxyusername:username Username used to contact the proxy.\n"
\r
53 + " -pp, -proxypassword:pwd Password used to contact the proxy.\n"
\r
54 + " -pd, -proxydomain:domain Domain of username for proxy authentication.\n"
\r
55 + " -urlkey, -appsettingurlkey:key Configuration key that contains the default\n"
\r
56 + " url for the generated WS proxy.\n"
\r
57 + " -baseurl, -appsettingbaseurl:url Base url to use when constructing the\n"
\r
59 + " -sample:[binding/]operation Display a sample SOAP request and response.\n"
\r
60 + " -? Display this message\n"
\r
62 + "Options can be of the forms -option, --option or /option\n";
\r
64 ArrayList descriptions = new ArrayList ();
\r
65 ArrayList schemas = new ArrayList ();
\r
71 string proxyAddress;
\r
73 string proxyPassword;
\r
74 string proxyUsername;
\r
79 string applicationSignature;
\r
80 string appSettingURLKey;
\r
81 string appSettingBaseURL;
\r
82 string language = "CS";
\r
85 string protocol = "Soap";
\r
86 ServiceDescriptionImportStyle style;
\r
87 CodeGenerationOptions options = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;
\r
90 StringCollection urls = new StringCollection ();
\r
94 /// Application entry point.
\r
97 public static int Main(string[] args)
\r
99 Driver d = new Driver();
\r
100 return d.Run(args);
\r
105 applicationSignature = ProductId;
\r
108 int Run (string[] args)
\r
112 // parse command line arguments
\r
113 foreach (string argument in args)
\r
114 ImportArgument(argument);
\r
116 if (noLogo == false)
\r
117 Console.WriteLine(ProductId);
\r
119 if (help || urls.Count == 0)
\r
121 Console.WriteLine(UsageMessage);
\r
125 CodeCompileUnit codeUnit = new CodeCompileUnit();
\r
126 CodeNamespace proxyCode = GetCodeNamespace();
\r
127 codeUnit.Namespaces.Add (proxyCode);
\r
129 WebReferenceCollection references = new WebReferenceCollection ();
\r
130 foreach (string murl in urls)
\r
132 DiscoveryClientProtocol dcc = CreateClient ();
\r
135 if (!url.StartsWith ("http://") && !url.StartsWith ("https://") && !url.StartsWith ("file://"))
\r
136 url = new Uri (Path.GetFullPath (url)).ToString ();
\r
138 dcc.DiscoverAny (url);
\r
141 WebReference reference = new WebReference (dcc.Documents, proxyCode, protocol, appSettingURLKey, appSettingBaseURL);
\r
142 references.Add (reference);
\r
144 if (sampleSoap != null)
\r
145 ConsoleSampleGenerator.Generate (descriptions, schemas, sampleSoap, protocol);
\r
148 if (sampleSoap != null)
\r
151 // generate the code
\r
152 if (GenerateCode (references, codeUnit))
\r
157 catch (Exception exception)
\r
159 Console.WriteLine("Error: {0}", exception.Message);
\r
161 // Supress this except for when debug is enabled
\r
162 Console.WriteLine("Stack:\n {0}", exception.StackTrace);
\r
169 /// Generate code for the specified ServiceDescription.
\r
172 public bool GenerateCode (WebReferenceCollection references, CodeCompileUnit codeUnit)
\r
174 bool hasWarnings = false;
\r
176 CodeDomProvider provider = GetProvider();
\r
177 ICodeGenerator generator = provider.CreateGenerator();
\r
179 StringCollection validationWarnings;
\r
180 validationWarnings = ServiceDescriptionImporter.GenerateWebReferences (references, options, style, generator, codeUnit, verbose);
\r
182 for (int n=0; n<references.Count; n++)
\r
184 WebReference wr = references [n];
\r
186 BasicProfileViolationCollection violations = new BasicProfileViolationCollection ();
\r
187 if (!WebServicesInteroperability.CheckConformance (WsiClaims.BP10, wr, violations)) {
\r
188 wr.Warnings |= ServiceDescriptionImportWarnings.WsiConformance;
\r
191 if (wr.Warnings != 0)
\r
193 if (!hasWarnings) {
\r
194 WriteText ("", 0, 0);
\r
195 WriteText ("There where some warnings while generating the code:", 0, 0);
\r
198 WriteText ("", 0, 0);
\r
199 WriteText (urls[n], 2, 2);
\r
201 if ((wr.Warnings & ServiceDescriptionImportWarnings.WsiConformance) > 0) {
\r
202 WriteText ("- This web reference does not conform to WS-I Basic Profile v1.0", 4, 6);
\r
203 foreach (BasicProfileViolation vio in violations) {
\r
204 WriteText (vio.NormativeStatement + ": " + vio.Details, 8, 8);
\r
205 foreach (string ele in vio.Elements)
\r
206 WriteText ("* " + ele, 10, 12);
\r
210 if ((wr.Warnings & ServiceDescriptionImportWarnings.NoCodeGenerated) > 0)
\r
211 WriteText ("- WARNING: No proxy class was generated", 4, 6);
\r
212 if ((wr.Warnings & ServiceDescriptionImportWarnings.NoMethodsGenerated) > 0)
\r
213 WriteText ("- WARNING: The proxy class generated includes no methods", 4, 6);
\r
214 if ((wr.Warnings & ServiceDescriptionImportWarnings.OptionalExtensionsIgnored) > 0)
\r
215 WriteText ("- WARNING: At least one optional extension has been ignored", 4, 6);
\r
216 if ((wr.Warnings & ServiceDescriptionImportWarnings.RequiredExtensionsIgnored) > 0)
\r
217 WriteText ("- WARNING: At least one necessary extension has been ignored", 4, 6);
\r
218 if ((wr.Warnings & ServiceDescriptionImportWarnings.UnsupportedBindingsIgnored) > 0)
\r
219 WriteText ("- WARNING: At least one binding is of an unsupported type and has been ignored", 4, 6);
\r
220 if ((wr.Warnings & ServiceDescriptionImportWarnings.UnsupportedOperationsIgnored) > 0)
\r
221 WriteText ("- WARNING: At least one operation is of an unsupported type and has been ignored", 4, 6);
\r
223 hasWarnings = true;
\r
227 if (hasWarnings) WriteText ("",0,0);
\r
229 string filename = outFilename;
\r
230 bool hasBindings = false;
\r
232 foreach (object doc in references[0].Documents.Values)
\r
234 ServiceDescription desc = doc as ServiceDescription;
\r
235 if (desc == null) continue;
\r
237 if (desc.Services.Count > 0 && filename == null)
\r
238 filename = desc.Services[0].Name + "." + provider.FileExtension;
\r
240 if (desc.Bindings.Count > 0 || desc.Services.Count > 0)
\r
241 hasBindings = true;
\r
244 if (filename == null)
\r
245 filename = "output." + provider.FileExtension;
\r
248 WriteText ("Writing file '" + filename + "'", 0, 0);
\r
249 StreamWriter writer = new StreamWriter(filename);
\r
251 CodeGeneratorOptions compilerOptions = new CodeGeneratorOptions();
\r
252 generator.GenerateCodeFromCompileUnit (codeUnit, writer, compilerOptions);
\r
256 return hasWarnings;
\r
261 /// Create the CodeNamespace with the generator's signature commented in.
\r
264 CodeNamespace GetCodeNamespace()
\r
266 CodeNamespace codeNamespace = new CodeNamespace(ns);
\r
268 if (applicationSignature != null)
\r
270 codeNamespace.Comments.Add(new CodeCommentStatement("\n This source code was auto-generated by " + applicationSignature + "\n"));
\r
273 return codeNamespace;
\r
279 void WriteCodeUnit(CodeCompileUnit codeUnit, string serviceName)
\r
281 CodeDomProvider provider = GetProvider();
\r
282 ICodeGenerator generator = provider.CreateGenerator();
\r
283 CodeGeneratorOptions options = new CodeGeneratorOptions();
\r
286 if (outFilename != null)
\r
287 filename = outFilename;
\r
289 filename = serviceName + "." + provider.FileExtension;
\r
291 Console.WriteLine ("Writing file '{0}'", filename);
\r
292 StreamWriter writer = new StreamWriter(filename);
\r
293 generator.GenerateCodeFromCompileUnit(codeUnit, writer, options);
\r
299 /// Fetch the Code Provider for the language specified by the 'language' members.
\r
302 private CodeDomProvider GetProvider()
\r
304 CodeDomProvider provider;
\r
306 switch (language.ToUpper ()) {
\r
308 provider = new CSharpCodeProvider ();
\r
311 provider = new Microsoft.VisualBasic.VBCodeProvider ();
\r
314 Type type = Type.GetType(language);
\r
315 if (type != null) {
\r
316 return (CodeDomProvider) Activator.CreateInstance (type);
\r
318 throw new Exception ("Unknown language");
\r
327 /// Interperet the command-line arguments and configure the relavent components.
\r
330 void ImportArgument(string argument)
\r
332 string optionValuePair;
\r
334 if (argument.StartsWith("--"))
\r
336 optionValuePair = argument.Substring(2);
\r
338 else if (argument.StartsWith("/") || argument.StartsWith("-"))
\r
340 optionValuePair = argument.Substring(1);
\r
344 urls.Add (argument);
\r
351 int indexOfEquals = optionValuePair.IndexOf(':');
\r
352 if (indexOfEquals > 0)
\r
354 option = optionValuePair.Substring(0, indexOfEquals);
\r
355 value = optionValuePair.Substring(indexOfEquals + 1);
\r
359 option = optionValuePair;
\r
365 case "appsettingurlkey":
\r
367 appSettingURLKey = value;
\r
370 case "appsettingbaseurl":
\r
372 appSettingBaseURL = value;
\r
396 outFilename = value;
\r
409 proxyAddress = value;
\r
412 case "proxydomain":
\r
414 proxyDomain = value;
\r
417 case "proxypassword":
\r
419 proxyPassword = value;
\r
422 case "proxyusername":
\r
424 proxyUsername = value;
\r
428 style = ServiceDescriptionImportStyle.Server;
\r
441 options &= ~CodeGenerationOptions.GenerateProperties;
\r
445 sampleSoap = value;
\r
453 if (argument.StartsWith ("/") && argument.IndexOfAny (Path.InvalidPathChars) == -1) {
\r
454 urls.Add (argument);
\r
458 throw new Exception("Unknown option " + option);
\r
462 DiscoveryClientProtocol CreateClient ()
\r
464 DiscoveryClientProtocol dcc = new DiscoveryClientProtocol ();
\r
466 if (username != null || password != null || domain != null)
\r
468 NetworkCredential credentials = new NetworkCredential();
\r
470 if (username != null)
\r
471 credentials.UserName = username;
\r
473 if (password != null)
\r
474 credentials.Password = password;
\r
476 if (domain != null)
\r
477 credentials.Domain = domain;
\r
479 dcc.Credentials = credentials;
\r
482 if (proxyAddress != null)
\r
484 WebProxy proxy = new WebProxy (proxyAddress);
\r
485 if (proxyUsername != null || proxyPassword != null || proxyDomain != null)
\r
487 NetworkCredential credentials = new NetworkCredential();
\r
489 if (proxyUsername != null)
\r
490 credentials.UserName = proxyUsername;
\r
492 if (proxyPassword != null)
\r
493 credentials.Password = proxyPassword;
\r
495 if (proxyDomain != null)
\r
496 credentials.Domain = proxyDomain;
\r
498 proxy.Credentials = credentials;
\r
505 static void WriteText (string text, int initialLeftMargin, int leftMargin)
\r
508 int margin = initialLeftMargin;
\r
512 Console.WriteLine ();
\r
516 while (n < text.Length)
\r
519 int lastWhite = -1;
\r
521 while (col < maxCols && n < text.Length) {
\r
522 if (char.IsWhiteSpace (text[n]))
\r
528 if (lastWhite == -1 || col < maxCols)
\r
530 else if (col >= maxCols)
\r
533 Console.WriteLine (new String (' ', margin) + text.Substring (sn, lastWhite - sn));
\r
534 margin = leftMargin;
\r