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
36 + " -n, -namespace:ns The namespace of the generated code, default\n"
\r
37 + " namespace if none.\n"
\r
38 + " -nologo Surpress the startup logo.\n"
\r
39 + " -o, -out:filename The target file for generated code.\n"
\r
40 + " -p, -password:pwd Password used to contact the server.\n"
\r
41 + " -protocol:protocol Protocol to implement. Allowed: Soap (default),\n"
\r
42 + " HttpGet or HttpPost.\n"
\r
43 + " -fields Generate fields instead of properties in data\n"
\r
45 + " -server Generate server instead of client proxy code.\n"
\r
46 + " -u, -username:username Username used to contact the server.\n"
\r
47 + " -proxy:url Address of the proxy.\n"
\r
48 + " -pu, -proxyusername:username Username used to contact the proxy.\n"
\r
49 + " -pp, -proxypassword:pwd Password used to contact the proxy.\n"
\r
50 + " -pd, -proxydomain:domain Domain of username for proxy authentication.\n"
\r
51 + " -urlkey, -appsettingurlkey:key Configuration key that contains the default\n"
\r
52 + " url for the generated WS proxy.\n"
\r
53 + " -baseurl, -appsettingbaseurl:url Base url to use when constructing the\n"
\r
55 + " -sample:[binding/]operation Display a sample SOAP request and response.\n"
\r
56 + " -? Display this message\n"
\r
58 + "Options can be of the forms -option, --option or /option\n";
\r
60 ArrayList descriptions = new ArrayList ();
\r
61 ArrayList schemas = new ArrayList ();
\r
67 string proxyAddress;
\r
69 string proxyPassword;
\r
70 string proxyUsername;
\r
75 string applicationSignature;
\r
76 string appSettingURLKey;
\r
77 string appSettingBaseURL;
\r
78 string language = "CS";
\r
81 string protocol = "Soap";
\r
82 ServiceDescriptionImportStyle style;
\r
83 CodeGenerationOptions options = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;
\r
86 StringCollection urls = new StringCollection ();
\r
90 /// Application entry point.
\r
93 public static int Main(string[] args)
\r
95 Driver d = new Driver();
\r
101 applicationSignature = ProductId;
\r
104 int Run (string[] args)
\r
108 // parse command line arguments
\r
109 foreach (string argument in args)
\r
110 ImportArgument(argument);
\r
112 if (noLogo == false)
\r
113 Console.WriteLine(ProductId);
\r
115 if (help || urls.Count == 0)
\r
117 Console.WriteLine(UsageMessage);
\r
121 CodeCompileUnit codeUnit = new CodeCompileUnit();
\r
122 CodeNamespace proxyCode = GetCodeNamespace();
\r
123 codeUnit.Namespaces.Add (proxyCode);
\r
125 WebReferenceCollection references = new WebReferenceCollection ();
\r
126 foreach (string murl in urls)
\r
128 DiscoveryClientProtocol dcc = CreateClient ();
\r
131 if (!url.StartsWith ("http://") && !url.StartsWith ("https://") && !url.StartsWith ("file://"))
\r
132 url = "file://" + Path.GetFullPath (url);
\r
134 dcc.DiscoverAny (url);
\r
137 WebReference reference = new WebReference (dcc.Documents, proxyCode, protocol, appSettingURLKey, appSettingBaseURL);
\r
138 references.Add (reference);
\r
140 if (sampleSoap != null)
\r
141 ConsoleSampleGenerator.Generate (descriptions, schemas, sampleSoap, protocol);
\r
144 if (sampleSoap != null)
\r
147 // generate the code
\r
148 if (GenerateCode (references, codeUnit))
\r
153 catch (Exception exception)
\r
155 Console.WriteLine("Error: {0}", exception.Message);
\r
157 // Supress this except for when debug is enabled
\r
158 Console.WriteLine("Stack:\n {0}", exception.StackTrace);
\r
165 /// Generate code for the specified ServiceDescription.
\r
168 public bool GenerateCode (WebReferenceCollection references, CodeCompileUnit codeUnit)
\r
170 bool hasWarnings = false;
\r
172 CodeDomProvider provider = GetProvider();
\r
173 ICodeGenerator generator = provider.CreateGenerator();
\r
175 StringCollection validationWarnings;
\r
176 validationWarnings = ServiceDescriptionImporter.GenerateWebReferences (references, options, style, generator, codeUnit, verbose);
\r
178 for (int n=0; n<references.Count; n++)
\r
180 WebReference wr = references [n];
\r
182 BasicProfileViolationCollection violations = new BasicProfileViolationCollection ();
\r
183 if (!WebServicesInteroperability.CheckConformance (WsiClaims.BP10, wr, violations)) {
\r
184 wr.Warnings |= ServiceDescriptionImportWarnings.WsiConformance;
\r
187 if (wr.Warnings != 0)
\r
189 if (!hasWarnings) {
\r
190 WriteText ("", 0, 0);
\r
191 WriteText ("There where some warnings while generating the code:", 0, 0);
\r
194 WriteText ("", 0, 0);
\r
195 WriteText (urls[n], 2, 2);
\r
197 if ((wr.Warnings & ServiceDescriptionImportWarnings.WsiConformance) > 0) {
\r
198 WriteText ("- This web reference does not conform to WS-I Basic Profile v1.0", 4, 6);
\r
199 foreach (BasicProfileViolation vio in violations) {
\r
200 WriteText (vio.NormativeStatement + ": " + vio.Details, 8, 8);
\r
201 foreach (string ele in vio.Elements)
\r
202 WriteText ("* " + ele, 10, 12);
\r
206 if ((wr.Warnings & ServiceDescriptionImportWarnings.NoCodeGenerated) > 0)
\r
207 WriteText ("- WARNING: No proxy class was generated", 4, 6);
\r
208 if ((wr.Warnings & ServiceDescriptionImportWarnings.NoMethodsGenerated) > 0)
\r
209 WriteText ("- WARNING: The proxy class generated includes no methods", 4, 6);
\r
210 if ((wr.Warnings & ServiceDescriptionImportWarnings.OptionalExtensionsIgnored) > 0)
\r
211 WriteText ("- WARNING: At least one optional extension has been ignored", 4, 6);
\r
212 if ((wr.Warnings & ServiceDescriptionImportWarnings.RequiredExtensionsIgnored) > 0)
\r
213 WriteText ("- WARNING: At least one necessary extension has been ignored", 4, 6);
\r
214 if ((wr.Warnings & ServiceDescriptionImportWarnings.UnsupportedBindingsIgnored) > 0)
\r
215 WriteText ("- WARNING: At least one binding is of an unsupported type and has been ignored", 4, 6);
\r
216 if ((wr.Warnings & ServiceDescriptionImportWarnings.UnsupportedOperationsIgnored) > 0)
\r
217 WriteText ("- WARNING: At least one operation is of an unsupported type and has been ignored", 4, 6);
\r
219 hasWarnings = true;
\r
223 if (hasWarnings) WriteText ("",0,0);
\r
225 string filename = outFilename;
\r
226 bool hasBindings = false;
\r
228 foreach (object doc in references[0].Documents.Values)
\r
230 ServiceDescription desc = doc as ServiceDescription;
\r
231 if (desc == null) continue;
\r
233 if (desc.Services.Count > 0 && filename == null)
\r
234 filename = desc.Services[0].Name + "." + provider.FileExtension;
\r
236 if (desc.Bindings.Count > 0 || desc.Services.Count > 0)
\r
237 hasBindings = true;
\r
240 if (filename == null)
\r
241 filename = "output." + provider.FileExtension;
244 WriteText ("Writing file '" + filename + "'", 0, 0);
\r
245 StreamWriter writer = new StreamWriter(filename);
\r
247 CodeGeneratorOptions compilerOptions = new CodeGeneratorOptions();
\r
248 generator.GenerateCodeFromCompileUnit (codeUnit, writer, compilerOptions);
\r
252 return hasWarnings;
\r
257 /// Create the CodeNamespace with the generator's signature commented in.
\r
260 CodeNamespace GetCodeNamespace()
\r
262 CodeNamespace codeNamespace = new CodeNamespace(ns);
\r
264 if (applicationSignature != null)
\r
266 codeNamespace.Comments.Add(new CodeCommentStatement("\n This source code was auto-generated by " + applicationSignature + "\n"));
\r
269 return codeNamespace;
\r
275 void WriteCodeUnit(CodeCompileUnit codeUnit, string serviceName)
\r
277 CodeDomProvider provider = GetProvider();
\r
278 ICodeGenerator generator = provider.CreateGenerator();
\r
279 CodeGeneratorOptions options = new CodeGeneratorOptions();
\r
282 if (outFilename != null)
\r
283 filename = outFilename;
\r
285 filename = serviceName + "." + provider.FileExtension;
\r
287 Console.WriteLine ("Writing file '{0}'", filename);
\r
288 StreamWriter writer = new StreamWriter(filename);
\r
289 generator.GenerateCodeFromCompileUnit(codeUnit, writer, options);
\r
295 /// Fetch the Code Provider for the language specified by the 'language' members.
\r
298 private CodeDomProvider GetProvider()
\r
300 // FIXME these should be loaded dynamically using reflection
\r
301 CodeDomProvider provider;
\r
303 switch (language.ToUpper())
\r
306 provider = new CSharpCodeProvider();
\r
310 provider = new Microsoft.VisualBasic.VBCodeProvider();
\r
314 throw new Exception("Unknow language");
\r
324 /// Interperet the command-line arguments and configure the relavent components.
\r
327 void ImportArgument(string argument)
\r
329 string optionValuePair;
\r
331 if (argument.StartsWith("--"))
\r
333 optionValuePair = argument.Substring(2);
\r
335 else if (argument.StartsWith("/") || argument.StartsWith("-"))
\r
337 optionValuePair = argument.Substring(1);
\r
341 urls.Add (argument);
\r
348 int indexOfEquals = optionValuePair.IndexOf(':');
\r
349 if (indexOfEquals > 0)
\r
351 option = optionValuePair.Substring(0, indexOfEquals);
\r
352 value = optionValuePair.Substring(indexOfEquals + 1);
\r
356 option = optionValuePair;
\r
362 case "appsettingurlkey":
\r
364 appSettingURLKey = value;
\r
367 case "appsettingbaseurl":
\r
369 appSettingBaseURL = value;
\r
393 outFilename = value;
\r
406 proxyAddress = value;
\r
409 case "proxydomain":
\r
411 proxyDomain = value;
\r
414 case "proxypassword":
\r
416 proxyPassword = value;
\r
419 case "proxyusername":
\r
421 proxyUsername = value;
\r
425 style = ServiceDescriptionImportStyle.Server;
\r
438 options &= ~CodeGenerationOptions.GenerateProperties;
\r
442 sampleSoap = value;
\r
450 if (argument.StartsWith ("/") && argument.IndexOfAny (Path.InvalidPathChars) == -1) {
\r
451 urls.Add (argument);
\r
455 throw new Exception("Unknown option " + option);
\r
459 DiscoveryClientProtocol CreateClient ()
\r
461 DiscoveryClientProtocol dcc = new DiscoveryClientProtocol ();
\r
463 if (username != null || password != null || domain != null)
\r
465 NetworkCredential credentials = new NetworkCredential();
\r
467 if (username != null)
\r
468 credentials.UserName = username;
\r
470 if (password != null)
\r
471 credentials.Password = password;
\r
473 if (domain != null)
\r
474 credentials.Domain = domain;
\r
476 dcc.Credentials = credentials;
\r
479 if (proxyAddress != null)
\r
481 WebProxy proxy = new WebProxy (proxyAddress);
\r
482 if (proxyUsername != null || proxyPassword != null || proxyDomain != null)
\r
484 NetworkCredential credentials = new NetworkCredential();
\r
486 if (proxyUsername != null)
\r
487 credentials.UserName = proxyUsername;
\r
489 if (proxyPassword != null)
\r
490 credentials.Password = proxyPassword;
\r
492 if (proxyDomain != null)
\r
493 credentials.Domain = proxyDomain;
\r
495 proxy.Credentials = credentials;
\r
502 static void WriteText (string text, int initialLeftMargin, int leftMargin)
\r
505 int margin = initialLeftMargin;
\r
509 Console.WriteLine ();
\r
513 while (n < text.Length)
\r
516 int lastWhite = -1;
\r
518 while (col < maxCols && n < text.Length) {
\r
519 if (char.IsWhiteSpace (text[n]))
\r
525 if (lastWhite == -1 || col < maxCols)
\r
527 else if (col >= maxCols)
\r
530 Console.WriteLine (new String (' ', margin) + text.Substring (sn, lastWhite - sn));
\r
531 margin = leftMargin;
\r