2010-05-19 Jb Evain <jbevain@novell.com>
[mcs.git] / tools / soapsuds / soapsuds.cs
blob11f11d9ec37445c60b2c3ed74e6fcfaf66ebef2e
1 //
2 // soapsuds.cs
3 //
4 // Author:
5 // Lluis Sanchez Gual (lluis@ximian.com)
6 //
7 // Copyright (C) 2003 Ximian, Inc.
8 //
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;
17 public class Driver
19 static void Main (string[] args)
21 Runner run = new Runner (args);
22 AppDomain domain = AppDomain.CreateDomain ("runner", null, Directory.GetCurrentDirectory(), "", false);
23 domain.DoCallBack (new CrossAppDomainDelegate (run.Main));
27 [Serializable]
28 class Runner
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;
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;
43 static bool wrappedProxy = true;
44 static string proxyNamespace = null;
45 static string strongNameFile = null;
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;
53 string[] args;
55 public Runner (string[] args)
57 this.args = args;
60 public void Main ()
62 try
64 ReadParameters (args);
66 if (logo)
67 WriteLogo ();
69 if (args.Length == 0 || args[0] == "--help")
71 WriteHelp ();
72 return;
75 ArrayList types = new ArrayList ();
76 Assembly assembly = null;
78 if (inputAssembly != null)
80 assembly = Assembly.LoadFile (inputAssembly);
81 foreach (Type t in assembly.GetTypes ())
82 types.Add (new ServiceType (t, serviceEndpoint));
85 if (inputTypes != null)
87 string[] ts = inputTypes.Split (';');
88 foreach (string type in ts)
90 Type t = null;
91 string url = null;
93 string[] typeParts = type.Split (',');
94 if (typeParts.Length == 1)
95 throw new Exception ("Type assembly not specified");
97 if (typeParts.Length >= 2)
99 t = Type.GetType (typeParts[0] + ", " + typeParts [1]);
100 if (typeParts.Length > 2)
101 url = typeParts [2];
103 types.Add (new ServiceType (t, url));
107 ArrayList writtenFiles = new ArrayList ();
108 MemoryStream schemaStream = null;
110 if (types.Count > 0)
112 schemaStream = new MemoryStream ();
113 MetaData.ConvertTypesToSchemaToStream ((ServiceType[]) types.ToArray (typeof(ServiceType)), SdlType.Wsdl, schemaStream);
116 if (inputUrl != null)
118 if (schemaStream != null) throw new Exception ("Only one type source can be specified");
119 schemaStream = new MemoryStream ();
120 MetaData.RetrieveSchemaFromUrlToStream (inputUrl, schemaStream);
123 if (inputSchema != null)
125 if (schemaStream != null) throw new Exception ("Only one type source can be specified");
126 schemaStream = new MemoryStream ();
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);
135 if (outputSchema != null)
137 if (schemaStream == null) throw new Exception ("No input schema or assembly has been specified");
139 schemaStream.Position = 0;
140 MetaData.SaveStreamToFile (schemaStream, outputSchema);
141 Console.WriteLine ("Written file " + outputSchema);
144 if (outputCode)
146 if (schemaStream == null) throw new Exception ("No input schema or assembly has been specified");
148 schemaStream.Position = 0;
149 MetaData.ConvertSchemaStreamToCodeSourceStream (wrappedProxy, outputDirectory, schemaStream, writtenFiles, null, null);
152 if (outputAssembly != null)
154 if (schemaStream == null) throw new Exception ("No input schema or assembly has been specified");
156 schemaStream.Position = 0;
157 if (outputCode)
158 MetaData.ConvertCodeSourceStreamToAssemblyFile (writtenFiles, outputAssembly, strongNameFile);
159 else
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 ();
167 writtenFiles.Add (outputAssembly);
170 foreach (string fn in writtenFiles)
171 Console.WriteLine ("Written file " + fn);
173 catch (Exception ex)
175 Console.WriteLine ("ERROR: " + ex.Message);
176 if (ex.GetType() != typeof(Exception))
177 Console.WriteLine (ex);
179 Console.WriteLine ();
182 static void WriteLogo ()
184 Console.WriteLine ("Mono SOAPSUDS Tool");
185 Console.WriteLine ();
188 static void WriteHelp ()
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 ();
227 static void ReadParameters (string[] args)
229 NetworkCredential cred = new NetworkCredential ();
230 NetworkCredential proxyCred = new NetworkCredential ();
231 WebProxy proxy = new WebProxy ();
235 foreach (string arg in args)
237 if (!arg.StartsWith ("/") && !arg.StartsWith ("-"))
238 continue;
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);
248 switch (parg.ToLower ())
250 case "nologo":
251 logo = false;
252 break;
254 case "urltoschema": case "url":
255 inputUrl = param;
256 break;
258 case "types":
259 inputTypes = param;
260 break;
262 case "inputassemblyfile": case "ia":
263 inputAssembly = param;
264 break;
266 case "outputassemblyfile": case "oa":
267 outputAssembly = param;
268 break;
270 case "inputdirectory": case "id":
271 inputDirectory = param;
272 break;
274 case "outputdirectory": case "od":
275 outputDirectory = param;
276 break;
278 case "inputschemafile": case "is":
279 inputSchema = param;
280 break;
282 case "outputschemafile": case "os":
283 outputSchema = param;
284 break;
286 case "proxynamespace": case "pn":
287 proxyNamespace = param;
288 break;
290 case "serviceendpoint": case "se":
291 serviceEndpoint = param;
292 break;
294 case "strongnamefile": case "sn":
295 strongNameFile = param;
296 break;
298 case "nowrappedproxy": case "nowp":
299 wrappedProxy = false;
300 break;
302 case "wrappedproxy": case "wp":
303 wrappedProxy = true;
304 break;
306 case "generatecode": case "gc":
307 outputCode = true;
308 break;
310 case "username": case "u":
311 userName = param;
312 break;
314 case "password": case "p":
315 password = param;
316 break;
318 case "domain": case "d":
319 domain = param;
320 break;
322 case "httpProxyName": case "hpn":
323 httpProxyName = param;
324 break;
326 case "httpProxyPort": case "hpp":
327 httpProxyPort = param;
328 break;