001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.toolkit;
025:
026: import java.util.Collection;
027: import java.util.Enumeration;
028: import java.util.Hashtable;
029: import java.util.LinkedList;
030: import java.util.StringTokenizer;
031: import java.util.Vector;
032:
033: /**
034: * This class simplifies command line parameter handling.
035: *
036: * <p>
037: * At first you have to specify the valid arguments by calling the {@link
038: * #addValidArgument(String,int)} method for each valid argument. After that, call the {@link
039: * #readArguments(String[])} method with the parameters of you main method for telling the command
040: * line instance the actual parameters. You can then get a value of a specified parameter by
041: * calling the {@link #getValue(String,int)} method.
042: * </p>
043: *
044: * <p>
045: * Example: This code would work for an application with the following command line syntax:
046: * <pre>java myTool [-r] [-name Prename Surename] [-recordfile filename] filename</pre>
047: * <br>
048: * <pre>
049: * try {
050: * CommandLine commandLine = new CommandLine();
051: * commandLine.addValidArgument ("r", 0);
052: * commandLine.addValidArgument ("name", 2);
053: * commandLine.addValidArgument ("recordfile", 1);
054: *
055: * commandLine.readArguments (args);
056: * if (commandLine.hasArgument ("name")) {
057: * String prename = commandLine.getValue ("name", 0);
058: * String surname = commandLine.getValue ("name", 1);
059: * ...
060: * }
061: * String filename = commandLine.getTail();
062: * ...
063: * } catch (CommandLineException c) {
064: * ...
065: * }
066: * </pre>
067: * </p>
068: *
069: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
070: * @version 0.9
071: */
072: public class CommandLine {
073: /** Hashtable of the valid arguments. */
074: private Hashtable argTable;
075:
076: /** Hashtable of the actual arguments with its values. */
077: private Hashtable arguments;
078:
079: /** The tail of the command line (part after the options). */
080: private Collection tail;
081:
082: /**
083: * Creates a new command line instance.
084: */
085: public CommandLine() {
086: super ();
087: argTable = new Hashtable();
088: arguments = new Hashtable();
089: tail = new LinkedList();
090: }
091:
092: /**
093: * Adds a valid argument with a specified number of values to the list of valid arguments.
094: *
095: * @param argName the name of the argument
096: * @param numberOfValues the number of values (0 for an argument without values).
097: */
098: public void addValidArgument(String argName, int numberOfValues) {
099: argTable.put(argName, new Integer(numberOfValues));
100: }
101:
102: /**
103: * Delivers the arguments of the main method to the command line instance
104: *
105: * @param args the arguments of the main method
106: *
107: * @throws CommandLineException if the command line is not ok
108: */
109: public void readArguments(String[] args)
110: throws CommandLineException {
111: int i = 0;
112: String arg;
113: String[] values;
114: int numberOfValues;
115:
116: while ((i < args.length) && args[i].startsWith("-")) {
117: arg = args[i].substring(1);
118: i++;
119:
120: if (!argTable.containsKey(arg)) {
121: throw new CommandLineException("Argument -" + arg
122: + " not known.");
123: } else {
124: numberOfValues = ((Integer) argTable.get(arg))
125: .intValue();
126: values = new String[numberOfValues];
127:
128: for (int j = 0; j < numberOfValues; j++) {
129: if (i < args.length) {
130: values[j] = args[i];
131: i++;
132: } else {
133: throw new CommandLineException("Argument -"
134: + arg
135: + " needs "
136: + ((numberOfValues == 1) ? "a value"
137: : (numberOfValues + " values"))
138: + "!");
139: }
140: }
141:
142: arguments.put(arg, values);
143: }
144: }
145:
146: for (; i < args.length; i++) {
147: tail.add(args[i]);
148: }
149: }
150:
151: /**
152: * Returns if the command line contains the specified argument.
153: *
154: * @param arg the name of the argument
155: *
156: * @return <code>true</code> or <code>false</code> :-)
157: */
158: public boolean hasArgument(String arg) {
159: return arguments.containsKey(arg);
160: }
161:
162: /**
163: * Returns the value of a specified argument at a specified position.
164: *
165: * @param arg the name of the argument
166: * @param number the number of the value (beginning with 0)
167: *
168: * @return the value
169: *
170: * @throws CommandLineException if the name of the position is not valid
171: */
172: public String getValue(String arg, int number)
173: throws CommandLineException {
174: try {
175: if (hasArgument(arg)) {
176: return ((String[]) arguments.get(arg))[number];
177: } else {
178: throw new CommandLineException("There is no argument -"
179: + arg);
180: }
181: } catch (ArrayIndexOutOfBoundsException a) {
182: throw new CommandLineException("Argument -" + arg
183: + " has less than " + number + " values!");
184: }
185: }
186:
187: /**
188: * Returns the tail of the command line as collection.
189: *
190: * @return DOCUMENT ME!
191: */
192: public Collection getTail() {
193: return tail;
194: }
195:
196: /**
197: * Converts a parameter line (as String) to an array of Strings. The conversion is very simple
198: * at the moment. The String is just splitted at the blanks.
199: *
200: * @param parameterLine the parameters as String
201: *
202: * @return the parameters, as elements of a String array
203: */
204: public static String[] toParameterArray(String parameterLine) {
205: StringTokenizer st = new StringTokenizer(parameterLine);
206: Vector v = new Vector();
207:
208: while (st.hasMoreTokens()) {
209: v.add(st.nextToken());
210: }
211:
212: String[] result = new String[v.size()];
213:
214: for (int i = 0; i < v.size(); i++) {
215: result[i] = (String) v.get(i);
216: }
217:
218: return result;
219: }
220:
221: /**
222: * Returns the command line instance as string representation.
223: *
224: * @return the string
225: */
226: public String toString() {
227: String result = "";
228: String arg;
229: int numberOfValues;
230:
231: Enumeration e = arguments.keys();
232:
233: while (e.hasMoreElements()) {
234: arg = (String) e.nextElement();
235: result += arg;
236: numberOfValues = ((Integer) argTable.get(arg)).intValue();
237:
238: if (numberOfValues > 0) {
239: result += ": ";
240:
241: for (int i = 0; i < numberOfValues; i++) {
242: result += (getValue(arg, i) + " ");
243: }
244: }
245:
246: result += "\n";
247: }
248:
249: result += "\n";
250: result += ("Tail: " + getTail() + "\n");
251:
252: return result;
253: }
254: }
|