KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > utils > Options


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.utils;
57
58 /**
59  * General purpose command line options parser.
60  * If this is used outside of Axis just remove the Axis specific sections.
61  *
62  * @author Doug Davis (dug@us.ibm.com)
63  */

64
65 import org.jboss.logging.Logger;
66
67 import java.net.MalformedURLException JavaDoc;
68 import java.net.URL JavaDoc;
69 import java.util.ArrayList JavaDoc;
70 import java.util.Vector JavaDoc;
71
72 public class Options
73 {
74    private static Logger log = Logger.getLogger(Options.class.getName());
75
76    String JavaDoc args[] = null;
77    Vector JavaDoc usedArgs = null;
78    URL JavaDoc defaultURL = null;
79
80    //////////////////////////////////////////////////////////////////////////
81
// SOASS (Start of Axis Specific Stuff)
82

83    // EOASS
84
//////////////////////////////////////////////////////////////////////////
85

86    /**
87     * Constructor - just pass in the <b>args</b> from the command line.
88     */

89    public Options(String JavaDoc _args[]) throws MalformedURLException JavaDoc
90    {
91       if (_args == null)
92       {
93          _args = new String JavaDoc[]{};
94       }
95       args = _args;
96       usedArgs = null;
97       defaultURL = new URL JavaDoc("http://localhost:8080/axis/servlet/AxisServlet");
98
99       ///////////////////////////////////////////////////////////////////////
100
// SOASS
101

102       /* Process these well known options first */
103       /******************************************/
104       try
105       {
106          getURL();
107       }
108       catch (MalformedURLException JavaDoc e)
109       {
110          log.error(Messages.getMessage("cantDoURL00"));
111          throw e;
112       }
113       getUser();
114       getPassword();
115
116       // EOASS
117
///////////////////////////////////////////////////////////////////////
118
}
119
120    public void setDefaultURL(String JavaDoc url) throws MalformedURLException JavaDoc
121    {
122       defaultURL = new URL JavaDoc(url);
123    }
124
125    public void setDefaultURL(URL JavaDoc url)
126    {
127       defaultURL = url;
128    }
129
130    /**
131     * Returns an int specifying the number of times that the flag was
132     * specified on the command line. Once this flag is looked for you
133     * must save the result because if you call it again for the same
134     * flag you'll get zero.
135     */

136    public int isFlagSet(char optChar)
137    {
138       int value = 0;
139       int loop;
140       int i;
141
142       for (loop = 0; usedArgs != null && loop < usedArgs.size(); loop++)
143       {
144          String JavaDoc arg = (String JavaDoc)usedArgs.elementAt(loop);
145          if (arg.charAt(0) != '-') continue;
146          for (i = 0; i < arg.length(); i++)
147             if (arg.charAt(i) == optChar) value++;
148       }
149
150       for (loop = 0; loop < args.length; loop++)
151       {
152          if (args[loop] == null || args[loop].length() == 0) continue;
153          if (args[loop].charAt(0) != '-') continue;
154          while (args[loop] != null &&
155                  (i = args[loop].indexOf(optChar)) != -1)
156          {
157             args[loop] = args[loop].substring(0, i) + args[loop].substring(i + 1);
158             if (args[loop].length() == 1)
159                args[loop] = null;
160             value++;
161             if (usedArgs == null) usedArgs = new Vector JavaDoc();
162             usedArgs.add("-" + optChar);
163          }
164       }
165       return (value);
166    }
167
168    /**
169     * Returns a string (or null) specifying the value for the passed
170     * option. If the option isn't there then null is returned. The
171     * option's value can be specified one of two ways:
172     * -x value
173     * -xvalue
174     * Note that: -ax value
175     * is not value (meaning flag 'a' followed by option 'x'.
176     * Options with values must be the first char after the '-'.
177     * If the option is specified more than once then the last one wins.
178     */

179    public String JavaDoc isValueSet(char optChar)
180    {
181       String JavaDoc value = null;
182       int loop;
183       int i;
184
185       for (loop = 0; usedArgs != null && loop < usedArgs.size(); loop++)
186       {
187          String JavaDoc arg = (String JavaDoc)usedArgs.elementAt(loop);
188          if (arg.charAt(0) != '-' || arg.charAt(1) != optChar)
189             continue;
190          value = arg.substring(2);
191          if (loop + 1 < usedArgs.size())
192             value = (String JavaDoc)usedArgs.elementAt(++loop);
193       }
194
195       for (loop = 0; loop < args.length; loop++)
196       {
197          if (args[loop] == null || args[loop].length() == 0) continue;
198          if (args[loop].charAt(0) != '-') continue;
199          i = args[loop].indexOf(optChar);
200          if (i != 1) continue;
201          if (i != args[loop].length() - 1)
202          {
203             // Not at end of arg, so use rest of arg as value
204
value = args[loop].substring(i + 1);
205             args[loop] = args[loop].substring(0, i);
206          }
207          else
208          {
209             // Remove the char from the current arg
210
args[loop] = args[loop].substring(0, i);
211
212             // Nothing after char so use next arg
213
if (loop + 1 < args.length && args[loop + 1] != null)
214             {
215                // Next arg is there and non-null
216
if (args[loop + 1].charAt(0) != '-')
217                {
218                   value = args[loop + 1];
219                   args[loop + 1] = null;
220                }
221             }
222             else
223             {
224                // Next is null or not there - do nothing
225
// value = null ;
226
}
227          }
228          if (args[loop].length() == 1)
229             args[loop] = null;
230          // For now, keep looping to get that last on there
231
// break ;
232
}
233       if (value != null)
234       {
235          if (usedArgs == null) usedArgs = new Vector JavaDoc();
236          usedArgs.add("-" + optChar);
237          if (value.length() > 0) usedArgs.add(value);
238       }
239       return (value);
240    }
241
242    /**
243     * This just returns a string with the unprocessed flags - mainly
244     * for error reporting - so you can report the unknown flags.
245     */

246    public String JavaDoc getRemainingFlags()
247    {
248       StringBuffer JavaDoc sb = null;
249       int loop;
250
251       for (loop = 0; loop < args.length; loop++)
252       {
253          if (args[loop] == null || args[loop].length() == 0) continue;
254          if (args[loop].charAt(0) != '-') continue;
255          if (sb == null) sb = new StringBuffer JavaDoc();
256          sb.append(args[loop].substring(1));
257       }
258       return (sb == null ? null : sb.toString());
259    }
260
261    /**
262     * This returns an array of unused args - these are the non-option
263     * args from the command line.
264     */

265    public String JavaDoc[] getRemainingArgs()
266    {
267       ArrayList JavaDoc al = null;
268       int loop;
269
270       for (loop = 0; loop < args.length; loop++)
271       {
272          if (args[loop] == null || args[loop].length() == 0) continue;
273          if (args[loop].charAt(0) == '-') continue;
274          if (al == null) al = new ArrayList JavaDoc();
275          al.add((String JavaDoc)args[loop]);
276       }
277       if (al == null) return (null);
278       String JavaDoc a[] = new String JavaDoc[al.size()];
279       for (loop = 0; loop < al.size(); loop++)
280          a[loop] = (String JavaDoc)al.get(loop);
281       return (a);
282    }
283
284    //////////////////////////////////////////////////////////////////////////
285
// SOASS
286
public String JavaDoc getURL() throws MalformedURLException JavaDoc
287    {
288       String JavaDoc tmp;
289       String JavaDoc host = null; // -h also -l (url)
290
String JavaDoc port = null; // -p
291
String JavaDoc servlet = null; // -s also -f (file)
292
String JavaDoc protocol = null;
293
294       URL JavaDoc url = null;
295
296       // Just in case...
297
org.jboss.axis.client.Call.initialize();
298
299       if ((tmp = isValueSet('l')) != null)
300       {
301          url = new URL JavaDoc(tmp);
302          host = url.getHost();
303          port = "" + url.getPort();
304          servlet = url.getFile();
305          protocol = url.getProtocol();
306       }
307
308       if ((tmp = isValueSet('f')) != null)
309       {
310          host = "";
311          port = "-1";
312          servlet = tmp;
313          protocol = "file";
314       }
315
316       tmp = isValueSet('h');
317       if (host == null) host = tmp;
318       tmp = isValueSet('p');
319       if (port == null) port = tmp;
320       tmp = isValueSet('s');
321       if (servlet == null) servlet = tmp;
322
323       if (host == null) host = defaultURL.getHost();
324       if (port == null) port = "" + defaultURL.getPort();
325       if (servlet == null)
326          servlet = defaultURL.getFile();
327       else if (servlet.length() > 0 && servlet.charAt(0) != '/')
328          servlet = "/" + servlet;
329
330       if (url == null)
331       {
332          if (protocol == null) protocol = defaultURL.getProtocol();
333          tmp = protocol + "://" + host;
334          if (port != null && !port.equals("-1")) tmp += ":" + port;
335          if (servlet != null) tmp += servlet;
336       }
337       else
338          tmp = url.toString();
339       log.debug(Messages.getMessage("return02", "getURL", tmp));
340       return (tmp);
341    }
342
343    public String JavaDoc getHost()
344    {
345       try
346       {
347          URL JavaDoc url = new URL JavaDoc(getURL());
348          return (url.getHost());
349       }
350       catch (Exception JavaDoc exp)
351       {
352          return ("localhost");
353       }
354    }
355
356    public int getPort()
357    {
358       try
359       {
360          URL JavaDoc url = new URL JavaDoc(getURL());
361          return (url.getPort());
362       }
363       catch (Exception JavaDoc exp)
364       {
365          return (-1);
366       }
367    }
368
369    public String JavaDoc getUser()
370    {
371       return (isValueSet('u'));
372    }
373
374    public String JavaDoc getPassword()
375    {
376       return (isValueSet('w'));
377    }
378    // EOASS
379
//////////////////////////////////////////////////////////////////////////
380
}
381
Popular Tags