KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > zeus > util > Arguments


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  */

19 package org.enhydra.zeus.util;
20
21 import java.util.Hashtable JavaDoc;
22
23 /**
24  * <p>
25  * <code>Arguments</code> is a Zeus utility class that provides
26  * for converting arguments passed to a class in the -param=value
27  * format into a <code>Hashtable</code> like container. This makes
28  * it possible to pass arguments in any order.
29  * </p>
30  *
31  * @author Brooke Hedrick
32  */

33 public class Arguments extends Hashtable JavaDoc {
34     
35     /**
36      * <p>Constructor to delegate instantiation.</p>
37      */

38     public Arguments() {
39         super();
40     }
41     
42     /**
43      * <p>
44      * This will take the arguments passed to a main method with the
45      * arguments specified as "-argument1=value -argument2=value" and
46      * place these in a <code>Hashtable</code> keyed by the argument name.
47      * </p>
48      *
49      * @param args <code>String[]</code> initial arguments to add.
50      */

51     public Arguments(String JavaDoc[] args) {
52         super();
53         setValues(args);
54     }
55     
56     /**
57      * <p>
58      * This will take a <code>String</code> as the argument name,
59      * and return the parameter value.
60      * </p>
61      *
62      * @param argument <code>String</code> argument to find.
63      * @return <code>String</code> - the value of the argument.
64      */

65     public String JavaDoc getValue(String JavaDoc argumentName) {
66         return (String JavaDoc)get(argumentName);
67     }
68     
69     /**
70      * <p>
71      * This will take a <code>String</code> as the argument name,
72      * and return true if an argument by that name exists, otherwise.
73      * it will return false
74      * </p>
75      *
76      * @param argumentName <code>String</code> argument to find.
77      * @return <code>boolean</code> - whether or not the argument exists.
78      */

79     public boolean hasValue(String JavaDoc argumentName) {
80         return (get(argumentName) != null);
81     }
82     
83     /**
84      * <p>
85      * This will take a <code>String</code> as the argument name,
86      * and another <code>String</code> as the argument value in.
87      * order to add a new argument/value to the list of arguments
88      * </p>
89      *
90      * @param argumentName <code>String</code> argument to add.
91      * @param argumentValue <code>String</code> value of argument.
92      */

93     public void setValue(String JavaDoc argumentName, String JavaDoc argumentValue) {
94         if (argumentName == null) {
95             throw new IllegalArgumentException JavaDoc("An Arguments object cannot " +
96                 "have a null argument name.");
97         }
98         put(argumentName, argumentValue);
99     }
100     
101     /**
102      * <p>
103      * This will take a <code>String[]</code> as an array of
104      * "-argument=value" pairs and add to/update the <code>Hashtable</code>
105      * containing the arguments.
106      * </p>
107      *
108      * @param args <code>String[]</code> argument pairs to add/update
109      */

110     public void setValues(String JavaDoc[] args) {
111         int equalsPosition = -1;
112
113         for (int i = 0; i < args.length; i++) {
114             String JavaDoc arg = args[i];
115             equalsPosition = arg.indexOf("=");
116
117             if ( equalsPosition == -1 ) {
118                 System.err.println("The argument you specified, '"
119                     + arg + "' doesn't contain an '='.\n"
120                     + "All arguments must be of the form 'foo=bar'.");
121                 System.exit(1);
122             }
123
124             put(arg.substring(1, equalsPosition), arg.substring(equalsPosition + 1));
125         }
126     }
127 }
128
Popular Tags