* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / tools / disco / disco.cs
1 // 
2 // disco.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.Net;
12 using System.Web.Services.Discovery;
13 using System.IO;
14
15 public class Driver
16 {
17         static bool save = true;
18         static bool logo = true;
19         static string url;
20         static string directory = ".";
21         static DiscoveryClientProtocol prot;
22         
23         static void Main (string[] args)
24         {
25                 try
26                 {
27                         ReadParameters (args);
28                         
29                         if (logo) 
30                                 WriteLogo ();
31                         
32                         if (args.Length == 0 || args[0] == "--help")
33                         {
34                                 WriteHelp ();
35                                 return;
36                         }
37                         
38                         if (url == null) throw new Exception ("URL to discover not provided");
39                         
40                         prot.DiscoverAny (url);
41                         prot.ResolveAll ();
42                         
43                         if (prot.References.Count > 0)
44                         {
45                                 Console.WriteLine ("Disco found documents at the following URLs:");
46                                 foreach (DiscoveryReference refe in prot.References.Values)
47                                 {
48                                         if (refe is ContractReference) Console.Write ("- WSDL document at  ");
49                                         else if (refe is DiscoveryDocumentReference) Console.Write ("- DISCO document at ");
50                                         else Console.Write ("- Xml Schema at    ");
51                                         Console.WriteLine (refe.Url);
52                                 }
53                         }
54                         else
55                                 Console.WriteLine ("Disco didn't find any document at the specified URL");
56                         
57                         if (save)
58                         {
59                                 DiscoveryClientResultCollection col = prot.WriteAll (directory, "results.discomap");
60                                 Console.WriteLine ();
61                                 Console.WriteLine ("The following files hold the content found at the corresponding URLs:");
62                                 foreach (DiscoveryClientResult res in col)
63                                         Console.WriteLine ("- " + res.Filename + " <- " + res.Url);
64                                 Console.WriteLine ();
65                                 Console.WriteLine ("The file " + Path.Combine (directory,"results.discomap") + " holds links to each of there files");
66                                 Console.WriteLine ();
67                         }                               
68                 }
69                 catch (Exception ex)
70                 {
71                         Console.WriteLine ("ERROR: " + ex.Message);
72                         Console.WriteLine (ex);
73                 }
74         }
75
76         static void WriteLogo ()
77         {
78                 Console.WriteLine ("Mono Web Service Discovery Tool");
79                 Console.WriteLine ();
80         }
81         
82         static void WriteHelp ()
83         {
84                 Console.WriteLine ("Usage: disco [options] url");
85                 Console.WriteLine ();
86                 Console.WriteLine ("Options:");
87                 Console.WriteLine ("   -nologo               Supress the startup logo");
88                 Console.WriteLine ("   -nosave               Do not save the discovered documents to disk.");
89                 Console.WriteLine ("                         The default is to save the documents.");
90                 Console.WriteLine ("   -o -out:directory     The directory where to save the discovered documents.");
91                 Console.WriteLine ("                         By default, documents are saved in the current");
92                 Console.WriteLine ("                         directory.");
93                 Console.WriteLine ("   -u -username:username  ");
94                 Console.WriteLine ("   -p -password:password  ");
95                 Console.WriteLine ("   -d -domain:domain     The credentials to use when connecting to the server.");
96                 Console.WriteLine ("   -proxy:url            The url of the proxy server to use for http requests.");
97                 Console.WriteLine ("   -proxyusername:name");
98                 Console.WriteLine ("   -proxypassword:pwd");
99                 Console.WriteLine ("   -proxydomin:domain    The credentials to use when connection to the proxy.");
100                 Console.WriteLine ();
101         }
102         
103         static void ReadParameters (string[] args)
104         {
105                 prot = new DiscoveryClientProtocol ();
106                 NetworkCredential cred = new NetworkCredential ();
107                 NetworkCredential proxyCred = new NetworkCredential ();
108                 WebProxy proxy = new WebProxy ();
109                 url = null;
110                 
111                 foreach (string arg in args)
112                 {
113                         if (arg.StartsWith ("/") || arg.StartsWith ("-"))
114                         {
115                                 string parg = arg.Substring (1);
116                                 int i = parg.IndexOf (":");
117                                 string param = null;
118                                 if (i != -1) {
119                                         param = parg.Substring (i+1);
120                                         parg = parg.Substring (0,i);
121                                 }
122                                 
123                                 switch (parg)
124                                 {
125                                         case "nologo":
126                                                 logo = false;
127                                                 break;
128                                                 
129                                         case "nosave":
130                                                 save = false;
131                                                 break;
132                                                 
133                                         case "out":     case "o":
134                                                 directory = param;
135                                                 break;
136                                                 
137                                         case "username": case "u":
138                                                 cred.UserName = param;
139                                                 break;
140                                                 
141                                         case "password": case "p":
142                                                 cred.Password = param;
143                                                 break;
144                                                 
145                                         case "domain": case "d":
146                                                 cred.Domain = param;
147                                                 break;
148                                                 
149                                         case "proxy":
150                                                 proxy.Address = new Uri (param);
151                                                 break;
152                                                 
153                                         case "proxyusername":
154                                                 proxyCred.UserName = param;
155                                                 break;
156                                                 
157                                         case "proxypassword":
158                                                 proxyCred.Password = param;
159                                                 break;
160                                                 
161                                         case "proxydomain":
162                                                 proxyCred.Domain = param;
163                                                 break;
164                                 }
165                                 
166                                 if (cred.UserName != null)
167                                         prot.Credentials = cred;
168                                         
169                                 if (proxyCred.UserName != null)
170                                         proxy.Credentials = proxyCred;
171                                         
172                                 if (proxy.Address != null)
173                                         prot.Proxy = proxy;
174                         }
175                         else
176                         {
177                                 url = arg;
178                         }
179                 }
180         }
181 }