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
131 DiscoveryClientProtocol dcc = CreateClient ();
\r
133 foreach (string murl in urls)
\r
137 if (!url.StartsWith ("http://") && !url.StartsWith ("https://") && !url.StartsWith ("file://"))
\r
138 url = new Uri (Path.GetFullPath (url)).ToString ();
\r
140 dcc.DiscoverAny (url);
\r
145 WebReference reference = new WebReference (dcc.Documents, proxyCode, protocol, appSettingURLKey, appSettingBaseURL);
\r
146 references.Add (reference);
\r
148 if (sampleSoap != null)
\r
149 ConsoleSampleGenerator.Generate (descriptions, schemas, sampleSoap, protocol);
\r
151 if (sampleSoap != null)
\r
154 // generate the code
\r
155 GenerateCode (references, codeUnit);
\r
158 catch (Exception exception)
\r
160 Console.WriteLine("Error: {0}", exception.Message);
\r
162 // Supress this except for when debug is enabled
\r
163 Console.WriteLine("Stack:\n {0}", exception.StackTrace);
\r
170 /// Generate code for the specified ServiceDescription.
\r
173 public bool GenerateCode (WebReferenceCollection references, CodeCompileUnit codeUnit)
\r
175 bool hasWarnings = false;
\r
177 CodeDomProvider provider = GetProvider();
\r
179 StringCollection validationWarnings;
\r
180 WebReferenceOptions opts = new WebReferenceOptions ();
\r
181 opts.CodeGenerationOptions = options;
\r
182 opts.Style = style;
\r
183 opts.Verbose = verbose;
\r
184 validationWarnings = ServiceDescriptionImporter.GenerateWebReferences (references, provider, codeUnit, opts);
\r
186 for (int n=0; n<references.Count; n++)
\r
188 WebReference wr = references [n];
\r
190 BasicProfileViolationCollection violations = new BasicProfileViolationCollection ();
\r
191 if (String.Compare (protocol, "SOAP", StringComparison.OrdinalIgnoreCase) == 0 && !WebServicesInteroperability.CheckConformance (WsiProfiles.BasicProfile1_1, wr, violations)) {
\r
192 wr.Warnings |= ServiceDescriptionImportWarnings.WsiConformance;
\r
195 if (wr.Warnings != 0)
\r
197 if (!hasWarnings) {
\r
198 WriteText ("", 0, 0);
\r
199 WriteText ("There were some warnings while generating the code:", 0, 0);
\r
202 WriteText ("", 0, 0);
\r
203 WriteText (urls[n], 2, 2);
\r
205 if ((wr.Warnings & ServiceDescriptionImportWarnings.WsiConformance) > 0) {
\r
206 WriteText ("- This web reference does not conform to WS-I Basic Profile v1.1", 4, 6);
\r
207 foreach (BasicProfileViolation vio in violations) {
\r
208 WriteText (vio.NormativeStatement + ": " + vio.Details, 8, 8);
\r
209 foreach (string ele in vio.Elements)
\r
210 WriteText ("* " + ele, 10, 12);
\r
214 if ((wr.Warnings & ServiceDescriptionImportWarnings.NoCodeGenerated) > 0)
\r
215 WriteText ("- WARNING: No proxy class was generated", 4, 6);
\r
216 if ((wr.Warnings & ServiceDescriptionImportWarnings.NoMethodsGenerated) > 0)
\r
217 WriteText ("- WARNING: The proxy class generated includes no methods", 4, 6);
\r
218 if ((wr.Warnings & ServiceDescriptionImportWarnings.OptionalExtensionsIgnored) > 0)
\r
219 WriteText ("- WARNING: At least one optional extension has been ignored", 4, 6);
\r
220 if ((wr.Warnings & ServiceDescriptionImportWarnings.RequiredExtensionsIgnored) > 0)
\r
221 WriteText ("- WARNING: At least one necessary extension has been ignored", 4, 6);
\r
222 if ((wr.Warnings & ServiceDescriptionImportWarnings.UnsupportedBindingsIgnored) > 0)
\r
223 WriteText ("- WARNING: At least one binding is of an unsupported type and has been ignored", 4, 6);
\r
224 if ((wr.Warnings & ServiceDescriptionImportWarnings.UnsupportedOperationsIgnored) > 0)
\r
225 WriteText ("- WARNING: At least one operation is of an unsupported type and has been ignored", 4, 6);
\r
227 hasWarnings = true;
\r
231 if (hasWarnings) WriteText ("",0,0);
\r
233 string filename = outFilename;
\r
234 bool hasBindings = false;
\r
236 foreach (object doc in references[0].Documents.Values)
\r
238 ServiceDescription desc = doc as ServiceDescription;
\r
239 if (desc == null) continue;
\r
241 if (desc.Services.Count > 0 && filename == null)
\r
242 filename = desc.Services[0].Name + "." + provider.FileExtension;
\r
244 if (desc.Bindings.Count > 0 || desc.Services.Count > 0)
\r
245 hasBindings = true;
\r
248 if (filename == null)
\r
249 filename = "output." + provider.FileExtension;
\r
252 WriteText ("Writing file '" + filename + "'", 0, 0);
\r
253 StreamWriter writer = new StreamWriter(filename);
\r
255 CodeGeneratorOptions compilerOptions = new CodeGeneratorOptions();
\r
256 provider.GenerateCodeFromCompileUnit (codeUnit, writer, compilerOptions);
\r
260 return hasWarnings;
\r
265 /// Create the CodeNamespace with the generator's signature commented in.
\r
268 CodeNamespace GetCodeNamespace()
\r
270 CodeNamespace codeNamespace = new CodeNamespace(ns);
\r
272 if (applicationSignature != null)
\r
274 codeNamespace.Comments.Add(new CodeCommentStatement("\n This source code was auto-generated by " + applicationSignature + "\n"));
\r
277 return codeNamespace;
\r
283 void WriteCodeUnit(CodeCompileUnit codeUnit, string serviceName)
\r
285 CodeDomProvider provider = GetProvider();
\r
286 ICodeGenerator generator = provider.CreateGenerator();
\r
287 CodeGeneratorOptions options = new CodeGeneratorOptions();
\r
290 if (outFilename != null)
\r
291 filename = outFilename;
\r
293 filename = serviceName + "." + provider.FileExtension;
\r
295 Console.WriteLine ("Writing file '{0}'", filename);
\r
296 StreamWriter writer = new StreamWriter(filename);
\r
297 generator.GenerateCodeFromCompileUnit(codeUnit, writer, options);
\r
303 /// Fetch the Code Provider for the language specified by the 'language' members.
\r
306 private CodeDomProvider GetProvider()
\r
308 CodeDomProvider provider;
\r
311 switch (language.ToUpper ()) {
\r
313 provider = new CSharpCodeProvider ();
\r
316 provider = new Microsoft.VisualBasic.VBCodeProvider ();
\r
319 type = Type.GetType("Boo.Lang.CodeDom.BooCodeProvider, Boo.Lang.CodeDom, Version=1.0.0.0, Culture=neutral, PublicKeyToken=32c39770e9a21a67");
\r
321 return (CodeDomProvider) Activator.CreateInstance (type);
\r
323 throw new Exception ("Boo.Lang.CodeDom.BooCodeProvider not available");
\r
326 type = Type.GetType(language);
\r
327 if (type != null) {
\r
328 return (CodeDomProvider) Activator.CreateInstance (type);
\r
330 throw new Exception ("Unknown language");
\r
339 /// Interperet the command-line arguments and configure the relavent components.
\r
342 void ImportArgument(string argument)
\r
344 string optionValuePair;
\r
346 if (argument.StartsWith("--"))
\r
348 optionValuePair = argument.Substring(2);
\r
350 else if (argument.StartsWith("/") || argument.StartsWith("-"))
\r
352 optionValuePair = argument.Substring(1);
\r
356 urls.Add (argument);
\r
363 int indexOfEquals = optionValuePair.IndexOf(':');
\r
364 if (indexOfEquals > 0)
\r
366 option = optionValuePair.Substring(0, indexOfEquals);
\r
367 value = optionValuePair.Substring(indexOfEquals + 1);
\r
371 option = optionValuePair;
\r
377 case "appsettingurlkey":
\r
379 appSettingURLKey = value;
\r
382 case "appsettingbaseurl":
\r
384 appSettingBaseURL = value;
\r
408 outFilename = value;
\r
421 proxyAddress = value;
\r
424 case "proxydomain":
\r
426 proxyDomain = value;
\r
429 case "proxypassword":
\r
431 proxyPassword = value;
\r
434 case "proxyusername":
\r
436 proxyUsername = value;
\r
440 style = ServiceDescriptionImportStyle.Server;
\r
453 options &= ~CodeGenerationOptions.GenerateProperties;
\r
457 sampleSoap = value;
\r
465 if (argument.StartsWith ("/") && argument.IndexOfAny (Path.InvalidPathChars) == -1) {
\r
466 urls.Add (argument);
\r
470 throw new Exception("Unknown option " + option);
\r
474 DiscoveryClientProtocol CreateClient ()
\r
476 DiscoveryClientProtocol dcc = new DiscoveryClientProtocol ();
\r
478 if (username != null || password != null || domain != null)
\r
480 NetworkCredential credentials = new NetworkCredential();
\r
482 if (username != null)
\r
483 credentials.UserName = username;
\r
485 if (password != null)
\r
486 credentials.Password = password;
\r
488 if (domain != null)
\r
489 credentials.Domain = domain;
\r
491 dcc.Credentials = credentials;
\r
494 if (proxyAddress != null)
\r
496 WebProxy proxy = new WebProxy (proxyAddress);
\r
497 if (proxyUsername != null || proxyPassword != null || proxyDomain != null)
\r
499 NetworkCredential credentials = new NetworkCredential();
\r
501 if (proxyUsername != null)
\r
502 credentials.UserName = proxyUsername;
\r
504 if (proxyPassword != null)
\r
505 credentials.Password = proxyPassword;
\r
507 if (proxyDomain != null)
\r
508 credentials.Domain = proxyDomain;
\r
510 proxy.Credentials = credentials;
\r
517 static void WriteText (string text, int initialLeftMargin, int leftMargin)
\r
520 int margin = initialLeftMargin;
\r
524 Console.WriteLine ();
\r
528 while (n < text.Length)
\r
531 int lastWhite = -1;
\r
533 while (col < maxCols && n < text.Length) {
\r
534 if (char.IsWhiteSpace (text[n]))
\r
540 if (lastWhite == -1 || col < maxCols)
\r
542 else if (col >= maxCols)
\r
545 Console.WriteLine (new String (' ', margin) + text.Substring (sn, lastWhite - sn));
\r
546 margin = leftMargin;
\r