001: package net.sf.mockcreator.utils;
002:
003: import java.io.IOException;
004: import java.io.PrintStream;
005: import java.util.ArrayList;
006: import java.util.Enumeration;
007: import java.util.LinkedList;
008: import java.util.List;
009: import java.util.StringTokenizer;
010: import java.util.jar.JarEntry;
011: import java.util.jar.JarFile;
012:
013: /**
014: * TODO: usage?
015: * TODO: tests?
016: */
017: public class ArgumentParser {
018:
019: public static void help(PrintStream out) {
020: out.println("Available options are:");
021: out
022: .println("-o dir output directory (default is current dir)");
023: out
024: .println("-d dirs java style separated list of source directories, like foo:bar:baz or foo;bar;baz");
025: out.println("-p pkg target package (default is autodetect)");
026: out
027: .println("-u update; generate mock even if up-to-date");
028: out
029: .println("-f N generate also fake superclasses ('dumbies') up to N depth ('all' means infinite depth)");
030: out.println("No option is required though.");
031: }
032:
033: private String[] args;
034: private List directories = new ArrayList();
035: private int firstInterfacePos = 0;
036:
037: private List classNames = new ArrayList();
038: private String outputPath = ".";
039: private String pkg = "";
040: private boolean skipUptodate = true;
041: private int fakeDepth = 0;
042:
043: public ArgumentParser(String[] args) throws IOException {
044: if (args == null || args.length == 0)
045: return;
046: this .args = args;
047:
048: outputPath = getOptionValue("-o");
049: if (outputPath == null || outputPath.equals("")) {
050: outputPath = ".";
051: }
052:
053: pkg = getOptionValue("-p");
054: if (pkg == null) {
055: pkg = "";
056: }
057:
058: String depth = getOptionValue("-f");
059: if (depth != null) {
060: if ("all".equals(depth)) {
061: fakeDepth = Integer.MAX_VALUE;
062: } else {
063: fakeDepth = Integer.parseInt(depth);
064: }
065: }
066:
067: skipUptodate = !getStandaloneBooleanOption("-u");
068:
069: getDirectories();
070: checkUnknownOptions();
071: getInterfaces();
072: }
073:
074: private void checkUnknownOptions() {
075: for (int n = firstInterfacePos; n < args.length; ++n) {
076: if (args[n] != null && args[n].startsWith("-"))
077: throw new IllegalArgumentException("unknown option:"
078: + args[n]);
079: }
080: }
081:
082: private void getInterfaces() throws IOException {
083: for (int n = firstInterfacePos; n < args.length; ++n) {
084: if (args[n] != null) {
085: classNames.add(args[n]);
086: }
087: }
088:
089: for (int n = 0; n < firstInterfacePos; ++n) {
090: if (args[n] != null) {
091: throw new IllegalArgumentException(
092: "parameter in wrong position:" + args[n]);
093: }
094: }
095: }
096:
097: public List getClassNames() {
098: return classNames;
099: }
100:
101: public String getOutputPath() {
102: return outputPath;
103: }
104:
105: public boolean getSkipUptodate() {
106: return skipUptodate;
107: }
108:
109: public List getSourceDirectories() {
110: return directories;
111: }
112:
113: public String getTargetPackage() {
114: return pkg;
115: }
116:
117: public int getFakeDepth() {
118: return fakeDepth;
119: }
120:
121: private boolean getStandaloneBooleanOption(String option) {
122: for (int idx = 0; idx < args.length; idx++) {
123: String argument = args[idx];
124: if (argument == null)
125: continue;
126:
127: if (argument.equals(option)) {
128: args[idx] = null;
129: firstInterfacePos = Math
130: .max(firstInterfacePos, idx + 1);
131: return true;
132: } else if (argument.startsWith(option)) {
133: throw new IllegalArgumentException("option " + option
134: + " shall not have parameters");
135: }
136: }
137:
138: return false;
139: }
140:
141: private String getOptionValue(String option) {
142: String val = null;
143:
144: for (int idx = 0; idx < args.length; idx++) {
145: String argument = args[idx];
146: if (argument == null)
147: continue;
148:
149: if (option.equals(argument)) {
150: args[idx] = null;
151: idx++;
152: if (idx >= args.length)
153: throw new IllegalArgumentException(
154: "no required parameter given for " + option);
155: val = args[idx];
156: args[idx] = null;
157: firstInterfacePos = Math
158: .max(firstInterfacePos, idx + 1);
159: } else if (argument.startsWith(option)) {
160: val = argument.substring(option.length());
161: args[idx] = null;
162: firstInterfacePos = Math
163: .max(firstInterfacePos, idx + 1);
164: }
165: }
166:
167: return val;
168: }
169:
170: private void getDirectories() {
171: directories = new LinkedList();
172:
173: String dirs = getOptionValue("-d");
174:
175: if (dirs != null) {
176: StringTokenizer dt = new StringTokenizer(dirs, ":;");
177:
178: while (dt.hasMoreTokens()) {
179: String directory = dt.nextToken();
180: directories.add(directory);
181: }
182: }
183: }
184: }
|