001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.util.commandline;
034:
035: import java.io.PrintStream;
036: import java.util.Hashtable;
037: import java.util.Iterator;
038: import java.util.Vector;
039:
040: //<<TODO:NAMING>> MOve the command package under ui [Jon Aquino]
041:
042: /**
043: * A class to parse Unix (and DOS/Win)-style application command-lines.
044: */
045: public class CommandLine {
046: Hashtable optSpecs = new Hashtable();
047: Vector optVec = new Vector(); // used to store options in order of entry
048: char optionChar; // the char that indicates an option. Default is '/', which is
049:
050: // NT Standard, but this causes problems on Unix systems, so '-' should
051: // be used for cross-platform apps
052:
053: public CommandLine() {
054: this ('/');
055: }
056:
057: public CommandLine(char optionCh) {
058: optionChar = optionCh;
059: }
060:
061: public void addOptionSpec(OptionSpec optSpec) {
062: String name = optSpec.getName();
063:
064: // should check for duplicate option names here
065: optSpecs.put(name.toLowerCase(), optSpec);
066: optVec.add(optSpec);
067: }
068:
069: OptionSpec getOptionSpec(String name) {
070: if (optSpecs.containsKey(name.toLowerCase())) {
071: return (OptionSpec) optSpecs.get(name.toLowerCase());
072: }
073:
074: return null;
075: }
076:
077: public Option getOption(String name) {
078: OptionSpec spec = getOptionSpec(name);
079:
080: if (spec == null) {
081: return null;
082: }
083:
084: return spec.getOption(0);
085: }
086:
087: public Iterator getOptions(String name) {
088: OptionSpec spec = getOptionSpec(name);
089:
090: return spec.getOptions();
091: }
092:
093: public boolean hasOption(String name) {
094: OptionSpec spec = getOptionSpec(name);
095:
096: if (spec == null) {
097: return false;
098: }
099:
100: return spec.hasOption();
101: }
102:
103: /**
104: * adds an option for an <B>existing</B> option spec
105: */
106: void addOption(Option opt) {
107: String name = opt.getName();
108: ((OptionSpec) optSpecs.get(name.toLowerCase())).addOption(opt);
109: }
110:
111: public void printDoc(PrintStream out) {
112: OptionSpec os = null;
113: out.println("Options:");
114:
115: for (Iterator i = optVec.iterator(); i.hasNext();) {
116: os = (OptionSpec) i.next();
117:
118: String name = optionChar + os.getName();
119:
120: if (os.getName() == OptionSpec.OPTION_FREE_ARGS) {
121: name = "(free)";
122: }
123:
124: out.println(" " + name + " " + os.getArgDesc() + " - "
125: + os.getDocDesc());
126: }
127: }
128:
129: public void parse(String[] args) throws ParseException {
130: String noOptMsg;
131: String optName;
132: Vector params = new Vector();
133: int i = 0;
134: int paramStart;
135:
136: while (i < args.length) {
137: if (args[i].charAt(0) == optionChar) {
138: optName = args[i].substring(1);
139: noOptMsg = "Invalid option: " + args[i];
140: paramStart = i + 1;
141: } else {
142: optName = OptionSpec.OPTION_FREE_ARGS;
143: noOptMsg = "Invalid option: " + args[i];
144: paramStart = i;
145: }
146:
147: OptionSpec optSpec = getOptionSpec(optName);
148:
149: if (optSpec == null) {
150: throw new ParseException(noOptMsg);
151: }
152:
153: int expectedArgCount = optSpec.getAllowedArgs();
154:
155: // parse option args
156: parseParams(args, params, paramStart, expectedArgCount);
157:
158: Option opt = optSpec.parse((String[]) params
159: .toArray(new String[0]));
160:
161: // check for number of allowed instances here
162: addOption(opt);
163: i++;
164: i += params.size();
165: }
166: }
167:
168: void parseParams(String[] args, Vector params, int i,
169: int expectedArgCount) {
170: params.clear();
171:
172: int count = 0;
173: int expected = expectedArgCount;
174:
175: if (expectedArgCount == OptionSpec.NARGS_ZERO_OR_ONE) {
176: expected = 1;
177: }
178:
179: if (expectedArgCount == OptionSpec.NARGS_ZERO_OR_MORE) {
180: expected = 999999999;
181: }
182:
183: if (expectedArgCount == OptionSpec.NARGS_ONE_OR_MORE) {
184: expected = 999999999;
185: }
186:
187: while ((i < args.length) && (count < expected)
188: && (args[i].charAt(0) != optionChar)) {
189: params.addElement(args[i++]);
190: count++;
191: }
192: }
193: }
|