2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / tools / wsdl / MonoWSDL2.cs
1 ///\r
2 /// MonoWSDL.cs -- a WSDL to proxy code generator.\r
3 ///\r
4 /// Author: Erik LeBel (eriklebel@yahoo.ca)\r
5 ///             Lluis Sanchez (lluis@novell.com)\r
6 ///\r
7 /// Copyright (C) 2003, Erik LeBel,\r
8 ///\r
9 \r
10 using System;\r
11 using System.Xml;\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
18 using System.IO;\r
19 using System.Net;\r
20 using System.Web.Services.Description;\r
21 using System.Web.Services.Discovery;\r
22 using System.Web.Services;\r
23 \r
24 using Microsoft.CSharp;\r
25 \r
26 namespace Mono.WebServices\r
27 {\r
28         public class Driver\r
29         {\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.\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
44                         + "                                classes.\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
54                         + "                                service url.\n"\r
55                         + "   -sample:[binding/]operation  Display a sample SOAP request and response.\n"\r
56                         + "   -?                           Display this message\n"\r
57                         + "\n"\r
58                         + "Options can be of the forms  -option, --option or /option\n";\r
59                 \r
60                 ArrayList descriptions = new ArrayList ();\r
61                 ArrayList schemas = new ArrayList ();\r
62                 \r
63                 bool noLogo;\r
64                 bool help;\r
65                 string sampleSoap;\r
66                 \r
67                 string proxyAddress;\r
68                 string proxyDomain;\r
69                 string proxyPassword;\r
70                 string proxyUsername;\r
71                 string username;\r
72                 string password;\r
73                 string domain;\r
74                 \r
75                 string applicationSignature;\r
76                 string appSettingURLKey;\r
77                 string appSettingBaseURL;\r
78                 string language = "CS";\r
79                 string ns;\r
80                 string outFilename;\r
81                 string protocol = "Soap";\r
82                 ServiceDescriptionImportStyle style;\r
83                 CodeGenerationOptions options = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;\r
84                 bool verbose;\r
85                 \r
86                 StringCollection urls = new StringCollection ();\r
87 \r
88                 ///\r
89                 /// <summary>\r
90                 ///     Application entry point.\r
91                 /// </summary>\r
92                 ///\r
93                 public static int Main(string[] args)\r
94                 {\r
95                         Driver d = new Driver();\r
96                         return d.Run(args);\r
97                 }\r
98                 \r
99                 Driver()\r
100                 {\r
101                         applicationSignature = ProductId;\r
102                 }\r
103                 \r
104                 int Run (string[] args)\r
105                 {\r
106                         try\r
107                         {\r
108                                 // parse command line arguments\r
109                                 foreach (string argument in args)\r
110                                         ImportArgument(argument);\r
111                                 \r
112                                 if (noLogo == false)\r
113                                         Console.WriteLine(ProductId);\r
114                                 \r
115                                 if (help || urls.Count == 0)\r
116                                 {\r
117                                         Console.WriteLine(UsageMessage);\r
118                                         return 0;\r
119                                 }\r
120                                 \r
121                                 CodeCompileUnit codeUnit = new CodeCompileUnit();\r
122                                 CodeNamespace proxyCode = GetCodeNamespace();\r
123                                 codeUnit.Namespaces.Add (proxyCode);\r
124                                 \r
125                                 WebReferenceCollection references = new WebReferenceCollection ();\r
126                                 foreach (string murl in urls) \r
127                                 {\r
128                                         DiscoveryClientProtocol dcc = CreateClient ();\r
129 \r
130                                         string url = murl;\r
131                                         if (!url.StartsWith ("http://") && !url.StartsWith ("https://") && !url.StartsWith ("file://"))\r
132                                                 url = "file://" + Path.GetFullPath (url);\r
133 \r
134                                         dcc.DiscoverAny (url);\r
135                                         dcc.ResolveAll ();\r
136                                         \r
137                                         WebReference reference = new WebReference (dcc.Documents, proxyCode, protocol, appSettingURLKey, appSettingBaseURL);\r
138                                         references.Add (reference);\r
139                                         \r
140                                         if (sampleSoap != null)\r
141                                                 ConsoleSampleGenerator.Generate (descriptions, schemas, sampleSoap, protocol);\r
142                                 }\r
143                                 \r
144                                 if (sampleSoap != null)\r
145                                         return 0;\r
146                                         \r
147                                 // generate the code\r
148                                 if (GenerateCode (references, codeUnit))\r
149                                         return 1;\r
150                                 else\r
151                                         return 0;\r
152                         }\r
153                         catch (Exception exception)\r
154                         {\r
155                                 Console.WriteLine("Error: {0}", exception.Message);\r
156                                 \r
157                                 // Supress this except for when debug is enabled\r
158                                 Console.WriteLine("Stack:\n {0}", exception.StackTrace);\r
159                                 return 2;\r
160                         }\r
161                 }\r
162                 \r
163                 ///\r
164                 /// <summary>\r
165                 ///     Generate code for the specified ServiceDescription.\r
166                 /// </summary>\r
167                 ///\r
168                 public bool GenerateCode (WebReferenceCollection references, CodeCompileUnit codeUnit)\r
169                 {\r
170                         bool hasWarnings = false;\r
171                         \r
172                         CodeDomProvider provider = GetProvider();\r
173                         ICodeGenerator generator = provider.CreateGenerator();\r
174                                 \r
175                         StringCollection validationWarnings;\r
176                         validationWarnings = ServiceDescriptionImporter.GenerateWebReferences (references, options, style, generator, codeUnit, verbose);\r
177                         \r
178                         for (int n=0; n<references.Count; n++)\r
179                         {\r
180                                 WebReference wr  = references [n];\r
181                                 \r
182                                 BasicProfileViolationCollection violations = new BasicProfileViolationCollection ();\r
183                                 if (!WebServicesInteroperability.CheckConformance (WsiClaims.BP10, wr, violations)) {\r
184                                         wr.Warnings |= ServiceDescriptionImportWarnings.WsiConformance;\r
185                                 }\r
186                                 \r
187                                 if (wr.Warnings != 0)\r
188                                 {\r
189                                         if (!hasWarnings) {\r
190                                                 WriteText ("", 0, 0);\r
191                                                 WriteText ("There where some warnings while generating the code:", 0, 0);\r
192                                         }\r
193                                         \r
194                                         WriteText ("", 0, 0);\r
195                                         WriteText (urls[n], 2, 2);\r
196                                         \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
203                                                 }\r
204                                         }\r
205                                         \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
218                                                 \r
219                                         hasWarnings = true;\r
220                                 }\r
221                         }\r
222                         
223                         if (hasWarnings) WriteText ("",0,0);\r
224                                 \r
225                         string filename = outFilename;\r
226                         bool hasBindings = false;\r
227                         \r
228                         foreach (object doc in references[0].Documents.Values)\r
229                         {\r
230                                 ServiceDescription desc = doc as ServiceDescription;\r
231                                 if (desc == null) continue;\r
232                                 
233                                 if (desc.Services.Count > 0 && filename == null)\r
234                                         filename = desc.Services[0].Name + "." + provider.FileExtension;\r
235                                         
236                                 if (desc.Bindings.Count > 0 || desc.Services.Count > 0)\r
237                                         hasBindings = true;\r
238                         }\r
239                         \r
240                         if (filename == null)\r
241                                 filename = "output." + provider.FileExtension;
242                         \r
243                         if (hasBindings) {\r
244                                 WriteText ("Writing file '" + filename + "'", 0, 0);\r
245                                 StreamWriter writer = new StreamWriter(filename);\r
246                                 \r
247                                 CodeGeneratorOptions compilerOptions = new CodeGeneratorOptions();\r
248                                 generator.GenerateCodeFromCompileUnit (codeUnit, writer, compilerOptions);\r
249                                 writer.Close();\r
250                         }\r
251                         \r
252                         return hasWarnings;\r
253                 }\r
254                 \r
255                 ///\r
256                 /// <summary>\r
257                 ///     Create the CodeNamespace with the generator's signature commented in.\r
258                 /// </summary>\r
259                 ///\r
260                 CodeNamespace GetCodeNamespace()\r
261                 {\r
262                         CodeNamespace codeNamespace = new CodeNamespace(ns);\r
263                         \r
264                         if (applicationSignature != null)\r
265                         {\r
266                                 codeNamespace.Comments.Add(new CodeCommentStatement("\n This source code was auto-generated by " + applicationSignature + "\n"));\r
267                         }\r
268                         \r
269                         return codeNamespace;\r
270                 }\r
271                 \r
272                 ///\r
273                 /// <summary/>\r
274                 ///\r
275                 void WriteCodeUnit(CodeCompileUnit codeUnit, string serviceName)\r
276                 {\r
277                         CodeDomProvider provider = GetProvider();\r
278                         ICodeGenerator generator = provider.CreateGenerator();\r
279                         CodeGeneratorOptions options = new CodeGeneratorOptions();\r
280                         \r
281                         string filename;\r
282                         if (outFilename != null)\r
283                                 filename = outFilename;\r
284                         else\r
285                                 filename = serviceName  + "." + provider.FileExtension;\r
286                         \r
287                         Console.WriteLine ("Writing file '{0}'", filename);\r
288                         StreamWriter writer = new StreamWriter(filename);\r
289                         generator.GenerateCodeFromCompileUnit(codeUnit, writer, options);\r
290                         writer.Close();\r
291                 }\r
292                 \r
293                 ///\r
294                 /// <summary>\r
295                 ///     Fetch the Code Provider for the language specified by the 'language' members.\r
296                 /// </summary>\r
297                 ///\r
298                 private CodeDomProvider GetProvider()\r
299                 {\r
300                         // FIXME these should be loaded dynamically using reflection\r
301                         CodeDomProvider provider;\r
302                         \r
303                         switch (language.ToUpper())\r
304                         {\r
305                             case "CS":\r
306                                     provider = new CSharpCodeProvider();\r
307                                     break;\r
308                             \r
309                                 case "VB":\r
310                                         provider = new Microsoft.VisualBasic.VBCodeProvider();\r
311                                         break;\r
312                                         \r
313                             default:\r
314                                     throw new Exception("Unknow language");\r
315                         }\r
316 \r
317                         return provider;\r
318                 }\r
319                 \r
320 \r
321 \r
322                 ///\r
323                 /// <summary>\r
324                 ///     Interperet the command-line arguments and configure the relavent components.\r
325                 /// </summary>\r
326                 ///             \r
327                 void ImportArgument(string argument)\r
328                 {\r
329                         string optionValuePair;\r
330                         \r
331                         if (argument.StartsWith("--"))\r
332                         {\r
333                                 optionValuePair = argument.Substring(2);\r
334                         }\r
335                         else if (argument.StartsWith("/") || argument.StartsWith("-"))\r
336                         {\r
337                                 optionValuePair = argument.Substring(1);\r
338                         }\r
339                         else\r
340                         {\r
341                                 urls.Add (argument);\r
342                                 return;\r
343                         }\r
344                         \r
345                         string option;\r
346                         string value;\r
347                         \r
348                         int indexOfEquals = optionValuePair.IndexOf(':');\r
349                         if (indexOfEquals > 0)\r
350                         {\r
351                                 option = optionValuePair.Substring(0, indexOfEquals);\r
352                                 value = optionValuePair.Substring(indexOfEquals + 1);\r
353                         }\r
354                         else\r
355                         {\r
356                                 option = optionValuePair;\r
357                                 value = null;\r
358                         }\r
359                         \r
360                         switch (option)\r
361                         {\r
362                                 case "appsettingurlkey":\r
363                                 case "urlkey":\r
364                                     appSettingURLKey = value;\r
365                                     break;\r
366 \r
367                                 case "appsettingbaseurl":\r
368                                 case "baseurl":\r
369                                     appSettingBaseURL = value;\r
370                                     break;\r
371 \r
372                                 case "d":\r
373                                 case "domain":\r
374                                     domain = value;\r
375                                     break;\r
376 \r
377                                 case "l":\r
378                                 case "language":\r
379                                     language = value;\r
380                                     break;\r
381 \r
382                                 case "n":\r
383                                 case "namespace":\r
384                                     ns = value;\r
385                                     break;\r
386 \r
387                                 case "nologo":\r
388                                     noLogo = true;\r
389                                     break;\r
390 \r
391                                 case "o":\r
392                                 case "out":\r
393                                     outFilename = value;\r
394                                     break;\r
395 \r
396                                 case "p":\r
397                                 case "password":\r
398                                     password = value;\r
399                                     break;\r
400 \r
401                                 case "protocol":\r
402                                     protocol = value;\r
403                                     break;\r
404 \r
405                                 case "proxy":\r
406                                     proxyAddress = value;\r
407                                     break;\r
408 \r
409                                 case "proxydomain":\r
410                                 case "pd":\r
411                                     proxyDomain = value;\r
412                                     break;\r
413 \r
414                                 case "proxypassword":\r
415                                 case "pp":\r
416                                     proxyPassword = value;\r
417                                     break;\r
418 \r
419                                 case "proxyusername":\r
420                                 case "pu":\r
421                                     proxyUsername = value;\r
422                                     break;\r
423 \r
424                                 case "server":\r
425                                     style = ServiceDescriptionImportStyle.Server;\r
426                                     break;\r
427 \r
428                                 case "u":\r
429                                 case "username":\r
430                                     username = value;\r
431                                     break;\r
432                                         \r
433                                 case "verbose":\r
434                                         verbose = true;\r
435                                         break;\r
436                                         \r
437                                 case "fields":\r
438                                         options &= ~CodeGenerationOptions.GenerateProperties;\r
439                                         break;\r
440                                         \r
441                                 case "sample":\r
442                                         sampleSoap = value;\r
443                                         break;\r
444 \r
445                                 case "?":\r
446                                     help = true;\r
447                                     break;\r
448 \r
449                                 default:\r
450                                         if (argument.StartsWith ("/") && argument.IndexOfAny (Path.InvalidPathChars) == -1) {\r
451                                                 urls.Add (argument);\r
452                                                 break;\r
453                                         }\r
454                                         else\r
455                                             throw new Exception("Unknown option " + option);\r
456                         }\r
457                 }\r
458                 \r
459                 DiscoveryClientProtocol CreateClient ()\r
460                 {\r
461                         DiscoveryClientProtocol dcc = new DiscoveryClientProtocol ();\r
462                         \r
463                         if (username != null || password != null || domain != null)\r
464                         {\r
465                                 NetworkCredential credentials = new NetworkCredential();\r
466                                 \r
467                                 if (username != null)\r
468                                         credentials.UserName = username;\r
469                                 \r
470                                 if (password != null)\r
471                                         credentials.Password = password;\r
472                                 \r
473                                 if (domain != null)\r
474                                         credentials.Domain = domain;\r
475                                 \r
476                                 dcc.Credentials = credentials;\r
477                         }\r
478                         \r
479                         if (proxyAddress != null)\r
480                         {\r
481                                 WebProxy proxy = new WebProxy (proxyAddress);\r
482                                 if (proxyUsername != null || proxyPassword != null || proxyDomain != null)\r
483                                 {\r
484                                         NetworkCredential credentials = new NetworkCredential();\r
485                                         \r
486                                         if (proxyUsername != null)\r
487                                                 credentials.UserName = proxyUsername;\r
488                                         \r
489                                         if (proxyPassword != null)\r
490                                                 credentials.Password = proxyPassword;\r
491                                         \r
492                                         if (proxyDomain != null)\r
493                                                 credentials.Domain = proxyDomain;\r
494                                         \r
495                                         proxy.Credentials = credentials;\r
496                                 }\r
497                         }                       \r
498                         \r
499                         return dcc;\r
500                 }\r
501                 \r
502                 static void WriteText (string text, int initialLeftMargin, int leftMargin)\r
503                 {\r
504                         int n = 0;\r
505                         int margin = initialLeftMargin;\r
506                         int maxCols = 80;\r
507                         \r
508                         if (text == "") {\r
509                                 Console.WriteLine ();\r
510                                 return;\r
511                         }\r
512                         \r
513                         while (n < text.Length)\r
514                         {\r
515                                 int col = margin;\r
516                                 int lastWhite = -1;\r
517                                 int sn = n;\r
518                                 while (col < maxCols && n < text.Length) {\r
519                                         if (char.IsWhiteSpace (text[n]))\r
520                                                 lastWhite = n;\r
521                                         col++;\r
522                                         n++;\r
523                                 }\r
524                                 \r
525                                 if (lastWhite == -1 || col < maxCols)\r
526                                         lastWhite = n;\r
527                                 else if (col >= maxCols)\r
528                                         n = lastWhite + 1;\r
529                                 \r
530                                 Console.WriteLine (new String (' ', margin) + text.Substring (sn, lastWhite - sn));\r
531                                 margin = leftMargin;\r
532                         }\r
533                 }\r
534         }\r
535 }