[xbuild] Fix bug #674337.
[mono.git] / mcs / tools / soapsuds / soapsuds.cs
1 // 
2 // soapsuds.cs
3 //
4 // Author:
5 //   Lluis Sanchez Gual (lluis@ximian.com)
6 //
7 // Copyright (C) 2003 Ximian, Inc.
8 //
9
10 using System;
11 using System.Reflection;
12 using System.Collections;
13 using System.Runtime.Remoting.MetadataServices;
14 using System.Net;
15 using System.IO;
16
17 public class Driver
18 {
19         static void Main (string[] args)
20         {
21                 Runner run = new Runner (args);
22                 AppDomain domain = AppDomain.CreateDomain ("runner", null, Directory.GetCurrentDirectory(), "", false);
23                 domain.DoCallBack (new CrossAppDomainDelegate (run.Main));
24         }
25 }
26
27 [Serializable]
28 class Runner
29 {
30         static bool logo = true;
31         static string inputUrl = null;
32         static string inputTypes = null;
33         static string inputSchema = null;
34         static string inputAssembly = null;
35         static string inputDirectory = null;
36
37         static string serviceEndpoint = null;
38         static string outputSchema = null;
39         static string outputDirectory = null;
40         static string outputAssembly = null;
41         static bool outputCode = false;
42
43         static bool wrappedProxy = true;
44         static string proxyNamespace = null;
45         static string strongNameFile = null;
46
47         static string userName = null;
48         static string password = null;
49         static string domain = null;
50         static string httpProxyName = null;
51         static string httpProxyPort = null;
52         
53         string[] args;
54         
55         public Runner (string[] args)
56         {
57                 this.args = args;
58         }
59         
60         public void Main ()
61         {
62                 try
63                 {
64                         ReadParameters (args);
65                         
66                         if (logo) 
67                                 WriteLogo ();
68                         
69                         if (args.Length == 0 || args[0] == "--help")
70                         {
71                                 WriteHelp ();
72                                 return;
73                         }
74                         
75                         ArrayList types = new ArrayList ();
76                         Assembly assembly = null;
77                         
78                         if (inputAssembly != null)
79                         {
80                                 assembly = Assembly.LoadFile (inputAssembly);
81                                 foreach (Type t in assembly.GetTypes ())
82                                         types.Add (new ServiceType (t, serviceEndpoint));
83                         }
84                         
85                         if (inputTypes != null)
86                         {
87                                 string[] ts = inputTypes.Split (';');
88                                 foreach (string type in ts)
89                                 {
90                                         Type t = null;
91                                         string url = null;
92
93                                         string[] typeParts = type.Split (',');
94                                         if (typeParts.Length == 1)
95                                                 throw new Exception ("Type assembly not specified");
96
97                                         if (typeParts.Length >= 2)
98                                         {
99                                                 t = Type.GetType (typeParts[0] + ", " + typeParts [1]);
100                                                 if (typeParts.Length > 2)
101                                                         url = typeParts [2];
102                                         }
103                                         types.Add (new ServiceType (t, url));
104                                 }
105                         }
106                         
107                         ArrayList writtenFiles = new ArrayList ();
108                         MemoryStream schemaStream = null;
109                         
110                         if (types.Count > 0)
111                         {
112                                 schemaStream = new MemoryStream ();
113                                 MetaData.ConvertTypesToSchemaToStream ((ServiceType[]) types.ToArray (typeof(ServiceType)), SdlType.Wsdl, schemaStream);
114                         }
115                         
116                         if (inputUrl != null)
117                         {
118                                 if (schemaStream != null) throw new Exception ("Only one type source can be specified");
119                                 schemaStream = new MemoryStream ();
120                                 MetaData.RetrieveSchemaFromUrlToStream (inputUrl, schemaStream);
121                         }
122                         
123                         if (inputSchema != null)
124                         {
125                                 if (schemaStream != null) throw new Exception ("Only one type source can be specified");
126                                 schemaStream = new MemoryStream ();
127                                 
128                                 FileStream fs = new FileStream (inputSchema, FileMode.Open, FileAccess.Read);
129                                 byte[] buffer = new byte [1024*5];
130                                 int nr = 0;
131                                 while ((nr = fs.Read (buffer, 0, buffer.Length)) > 0)
132                                         schemaStream.Write (buffer, 0, nr);
133                         }
134                         
135                         if (outputSchema != null)
136                         {
137                                 if (schemaStream == null) throw new Exception ("No input schema or assembly has been specified");
138                                 
139                                 schemaStream.Position = 0;
140                                 MetaData.SaveStreamToFile (schemaStream, outputSchema);
141                                 Console.WriteLine ("Written file " + outputSchema);
142                         }
143                         
144                         if (outputCode)
145                         {
146                                 if (schemaStream == null) throw new Exception ("No input schema or assembly has been specified");
147                                 
148                                 schemaStream.Position = 0;
149                                 MetaData.ConvertSchemaStreamToCodeSourceStream (wrappedProxy, outputDirectory, schemaStream, writtenFiles, null, null);
150                         }
151                         
152                         if (outputAssembly != null)
153                         {
154                                 if (schemaStream == null) throw new Exception ("No input schema or assembly has been specified");
155                                 
156                                 schemaStream.Position = 0;
157                                 if (outputCode)
158                                         MetaData.ConvertCodeSourceStreamToAssemblyFile (writtenFiles, outputAssembly, strongNameFile);
159                                 else
160                                 {
161                                         MetaData.ConvertSchemaStreamToCodeSourceStream (wrappedProxy, outputDirectory, schemaStream, writtenFiles, null, null);
162                                         MetaData.ConvertCodeSourceStreamToAssemblyFile (writtenFiles, outputAssembly, strongNameFile);
163                                         foreach (string file in writtenFiles)
164                                                 File.Delete (file);
165                                         writtenFiles.Clear ();
166                                 }
167                                 writtenFiles.Add (outputAssembly);
168                         }
169                         
170                         foreach (string fn in writtenFiles)
171                                 Console.WriteLine ("Written file " + fn);
172                 }
173                 catch (Exception ex)
174                 {
175                         Console.WriteLine ("ERROR: " + ex.Message);
176                         if (ex.GetType() != typeof(Exception))
177                                 Console.WriteLine (ex);
178                 }
179                 Console.WriteLine ();
180         }
181
182         static void WriteLogo ()
183         {
184                 Console.WriteLine ("Mono SOAPSUDS Tool");
185                 Console.WriteLine ();
186         }
187         
188         static void WriteHelp ()
189         {
190                 Console.WriteLine ("Usage: soapsuds [inputs] [outputs] [options]");
191                 Console.WriteLine ();
192                 Console.WriteLine ("Inputs:");
193                 Console.WriteLine ("   -url urltoschema:url             Url from which to retrieve the schema");
194                 Console.WriteLine ("   -types:type1,assembly[,serviceEndpoint][;type2,assembly,...] ");
195                 Console.WriteLine ("                                    List of types from which to generate");
196                 Console.WriteLine ("                                    a schema or proxy");
197                 Console.WriteLine ("   -ia -inputassemblyfile:assembly  Assembly that contains the types to export");
198                 Console.WriteLine ("   -is -inputschemafile:schemafile  Schema from which to generate proxy classes");
199                 Console.WriteLine ();
200                 Console.WriteLine ("Input Options:");
201                 Console.WriteLine ("   -id -inputdirectory:directory    Directory where DLLs are located");
202                 Console.WriteLine ("   -se -serviceendpoint:url         Url of the service to be placed in the");
203                 Console.WriteLine ("                                    WSDL document");
204                 Console.WriteLine ();
205                 Console.WriteLine ("Outputs:");
206                 Console.WriteLine ("   -oa -outputassemblyfile:assembly Generate an assembly");
207                 Console.WriteLine ("   -os -outputschemafile:file       Generate a schema");
208                 Console.WriteLine ("   -gc -generatecode                Generate proxy source code");
209                 Console.WriteLine ();
210                 Console.WriteLine ("Output Options:");
211                 Console.WriteLine ("   -od -outputdirectory:directory   Directory where output will be generated");
212                 Console.WriteLine ("   -pn -proxynamespace:namespace    Namespace of the generated proxy");
213                 Console.WriteLine ("   -nowp -nowrappedproxy            Generate a wrapped proxy");
214                 Console.WriteLine ("   -wp -wrappedproxy                Generate a wrapped proxy");
215                 Console.WriteLine ("   -sn -strongnamefile:snfile       Strong name file");
216                 Console.WriteLine ();
217                 Console.WriteLine ("General Options:");
218                 Console.WriteLine ("   -u -username:name                User name for server authentication");
219                 Console.WriteLine ("   -p -password:pwd                 Password for server authentication");
220                 Console.WriteLine ("   -d -domain:domain                Domain of the server");
221                 Console.WriteLine ("   -hpn -httpProxyName:name         Name of http proxy");
222                 Console.WriteLine ("   -hpp -httpProxyPort:port         Port of http proxy");
223                 Console.WriteLine ("   -nologo                          Supress the startup logo");
224                 Console.WriteLine ();
225         }
226         
227         static void ReadParameters (string[] args)
228         {
229                 NetworkCredential cred = new NetworkCredential ();
230                 NetworkCredential proxyCred = new NetworkCredential ();
231                 WebProxy proxy = new WebProxy ();
232
233                 
234                 
235                 foreach (string arg in args)
236                 {
237                         if (!arg.StartsWith ("/") && !arg.StartsWith ("-"))
238                                 continue;
239                         
240                         string parg = arg.Substring (1);
241                         int i = parg.IndexOf (":");
242                         string param = null;
243                         if (i != -1) {
244                                 param = parg.Substring (i+1);
245                                 parg = parg.Substring (0,i);
246                         }
247                         
248                         switch (parg.ToLower ())
249                         {
250                                 case "nologo":
251                                         logo = false;
252                                         break;
253                                         
254                                 case "urltoschema": case "url":
255                                         inputUrl = param;
256                                         break;
257                                         
258                                 case "types":
259                                         inputTypes = param;
260                                         break;
261                                         
262                                 case "inputassemblyfile": case "ia":
263                                         inputAssembly = param;
264                                         break;
265                                         
266                                 case "outputassemblyfile": case "oa":
267                                         outputAssembly = param;
268                                         break;
269                                         
270                                 case "inputdirectory": case "id":
271                                         inputDirectory = param;
272                                         break;
273                                         
274                                 case "outputdirectory": case "od":
275                                         outputDirectory = param;
276                                         break;
277                                         
278                                 case "inputschemafile": case "is":
279                                         inputSchema = param;
280                                         break;
281                                         
282                                 case "outputschemafile": case "os":
283                                         outputSchema = param;
284                                         break;
285                                         
286                                 case "proxynamespace": case "pn":
287                                         proxyNamespace = param;
288                                         break;
289                                         
290                                 case "serviceendpoint": case "se":
291                                         serviceEndpoint = param;
292                                         break;
293                                         
294                                 case "strongnamefile": case "sn":
295                                         strongNameFile = param;
296                                         break;
297
298                                 case "nowrappedproxy": case "nowp":
299                                         wrappedProxy = false;
300                                         break;
301                                         
302                                 case "wrappedproxy": case "wp":
303                                         wrappedProxy = true;
304                                         break;
305                                         
306                                 case "generatecode": case "gc":
307                                         outputCode = true;
308                                         break;
309                                         
310                                 case "username": case "u":
311                                         userName = param;
312                                         break;
313                                         
314                                 case "password": case "p":
315                                         password = param;
316                                         break;
317                                         
318                                 case "domain": case "d":
319                                         domain = param;
320                                         break;
321                                         
322                                 case "httpProxyName": case "hpn":
323                                         httpProxyName = param;
324                                         break;
325                                         
326                                 case "httpProxyPort": case "hpp":
327                                         httpProxyPort = param;
328                                         break;
329                         }
330                 }
331         }
332 }