Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: raining/client/ClientParser.java


1   /*
2   * $Author: rahul_kumar $
3   * $Id: ClientParser.java,v 1.3 2003/10/04 07:53:07 rahul_kumar Exp $
4    */
5   
6   package raining.client;
7   import raining.core.*;
8   import java.util.*;
9   import java.io.*;
10  import org.apache.commons.cli.*;
11  
12  
13  /**
14  * A generic commandline parser for client programs. Extend this to add
15  * more options.
16   */
17  
18  public class ClientParser  {
19  
20  
21      /** constructor that takes the argument list and name of program.
22       * Name of program will be used when printing help etc.
23       */
24      public ClientParser(String args[], String program){
25          parser = new PosixParser();
26          options = new Options();
27          this.args = args;
28          this.program = program;
29      }
30      CommandLineParser parser = null;
31      //CommandLineParser parser = new PosixParser();
32      //Options options = new Options();
33      Options options = null;
34      CommandLine line = null;
35  
36  
37      String urlarray[] = null;
38      String proxyServer = null;
39      int proxyPort = 8080;
40      String proxyAuth = null;
41  //    String proxyBase64 = null;
42      String filename = null;
43      boolean useProxy = false;
44      String args[] = null;
45      String program = null;
46  
47  
48      public Options getOptions(){
49          return options;
50      }
51      public CommandLine getCommandLine(){
52          return line;
53      }
54      public CommandLineParser getParser(){
55          return parser;
56      }
57      /** add the options and parse.
58       * usually this will be invoked after the constructor.
59       */
60      public void parse (){
61          addOptions();
62          parseLine();
63      }
64  
65      /** Add default options that ClientParser needs. Override to add
66       * more or replace.
67       */
68      public void addOptions(){
69              options.addOption( "f", "file", true, "take URL's from this file." );
70              options.addOption( "u", "use-proxy", false, "use a proxy server." );
71              options.addOption( "h", "host", true, "proxy host IP or name." );
72              options.addOption( "p", "port", true, "port of proxy host." );
73              options.addOption( "a", "authstring", true, "proxy server authorization string" );
74              //options.addOption( "b", "base64", true, "proxy server auth string as base64" );
75      }
76      public void parseLine (){
77          try {
78              // parse the command line arguments
79              line = parser.parse( options, args );     
80              Option[] opts = line.getOptions();
81  
82              if( line.hasOption( 'u' ) ) {
83                  // print the value of block-size
84                  useProxy = true;
85                  proxyServer = line.getOptionValue( 'h' ) ;
86                  try {
87                      proxyPort = Integer.parseInt(line.getOptionValue( 'p' )) ;
88                  } catch (Exception exc) { System.err.println( ">>> 55 EXC:"+ exc.toString()); } 
89                  proxyAuth = line.getOptionValue( 'a' ) ;
90                  //proxyBase64 = line.getOptionValue( 'b' ) ;
91  
92              }
93              if( line.hasOption( 'f' ) ) {
94                  filename = line.getOptionValue( 'f' ) ;
95                  //TODO populate URLARRAY
96                  urlarray = getFileContentsAsArray( filename );
97                  if (urlarray == null){
98                      System.err.println(  "Error in file contents of:"+ filename);
99                  }
100             }
101             else
102                 urlarray = line.getArgs();
103 
104             /* process any other options that may have been overridden
105              */
106             for( int i = 0; i < opts.length; i++ ){ 
107                 processOpt(opts[i], opts[i].getOpt(), 
108                         opts[i].getLongOpt(), 
109                         line.getOptionValue(opts[i].getOpt()) );
110             }
111             if (args.length == 0 || urlarray.length == 0)
112             {
113                 // automatically generate the help statement
114                 printhelp(options);
115                 //System.exit(-1);
116             }
117         }
118         catch( ParseException exp ) {
119             System.out.println( "Unexpected exception:" + exp.getMessage() );
120             HelpFormatter formatter = new HelpFormatter();
121             formatter.printHelp( program, options );
122         }
123     }
124 
125     public void printhelp (Options options){
126         HelpFormatter formatter = new HelpFormatter();
127         formatter.printHelp( "java "+program+" <options> [url list]", options );
128         System.err.println(  " You must either enter a file name or URL's ");
129         System.err.println(  "  e.g ");
130         System.err.println(  "  java "+program+" http://www.python.org/");
131         System.err.println(  "  java "+program+" -f file.txt           ");
132         System.err.println(  "  java "+program+" -u -h web-cache.bhartitelesoft.com -p 8080 -a sanjay.kumar:kumar http://www.gnu.org/");
133     }
134     public Option[] getProcessedOptions(){
135         return line.getOptions();
136     }
137     /** instance of a class used to process command line options.
138      */
139     OptionProcessor proc = null;
140     public void setOptionProcessor(OptionProcessor proc){
141         this.proc = proc;
142     }
143 
144     public void processOpt( Option obj ,String opt, String longOpt, String value){
145         char optc = opt.charAt(0);
146         //if (optc == 'x')
147         System.out.println(  "processOpt got "+optc+" and " + longOpt + "=>"+value);
148         if (proc != null)
149             proc.processOpt(obj, opt, longOpt, value);
150     }
151 
152 
153 
154 
155     public static void main (String args[]){ 
156         ClientParser clp = new ClientParser(args, "ClientParser");
157         clp.parse();
158         System.out.println(  clp.getUrlArray());
159         //System.out.println(  clp.getProxyBase64());
160         System.out.println(  clp.getProxyServer());
161 
162     }
163     public static String[] getFileContentsAsArray (String filename){
164         try {
165             BufferedReader br = new BufferedReader( new FileReader(filename)); 
166             java.util.ArrayList al = new java.util.ArrayList();
167             String line;
168             while ( (line = br.readLine())!=null){
169                 al.add (line.trim());
170             }
171             if (al.size()>0){
172                 String ret[] = new String[al.size()];
173                 ret = (String[]) al.toArray(ret);
174                 return ret;
175             }
176         } catch (Exception exc) { System.err.println( "EXC:"+ exc.toString()); exc.printStackTrace(); } 
177         return null;
178     }
179 
180 
181     public String getProxyServer() {  return proxyServer;} 
182     public String[] getUrlArray() {  return urlarray;} 
183     public int getProxyPort() {  return proxyPort;} 
184     public String getProxyAuth() {  return proxyAuth;} 
185 //    public String getProxyBase64() {  return proxyBase64;} 
186     public String getFilename() {  return filename;} 
187     public boolean getUseProxy() {  return useProxy;} 
188 
189 
190 } // end of class
191 
192 
193