1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jdo.impl.enhancer;
18
19 import java.io.PrintWriter;
20
21 import java.util.List;
22 import java.util.Iterator;
23 import java.util.ArrayList;
24
25
26 /***
27 * Set of options used by the JDO enhancer and its test programs.
28 *
29 * @author Martin Zaun
30 */
31 public class ClassArgOptions
32 extends GenericOptions
33 {
34 /***
35 * Tests if a filename has suffix <code>".class"</code> (ignoring case).
36 *
37 * @param filename the name of the file
38 * @return true if filename has a class file suffix
39 */
40 static private boolean isClassFileName(String filename)
41 {
42 return filename.toLowerCase().endsWith(".class");
43 }
44
45 /***
46 * Tests if a filename has suffix <code>".jar"</code> or
47 * <code>".zip"</code> (ignoring case).
48 *
49 * @param filename the name of the file
50 * @return true if filename has an archive file suffix
51 */
52 static private boolean isArchiveFileName(String filename)
53 {
54 final String s = filename.toLowerCase();
55 return (s.endsWith(".jar") || s.endsWith(".zip"));
56 }
57
58
59
60 /***
61 * The source path option.
62 */
63 public final StringOption sourcePath
64 = createStringOption("sourcepath", "s",
65 "<path> : path for lookup of class files");
66
67 /***
68 * The list of class name arguments.
69 */
70 public final List classNames = new ArrayList();
71
72 /***
73 * The list of class file name arguments.
74 */
75 public final List classFileNames = new ArrayList();
76
77 /***
78 * The list of archive file name arguments.
79 */
80 public final List archiveFileNames = new ArrayList();
81
82 /***
83 * Creates an instance.
84 */
85 public ClassArgOptions(PrintWriter out,
86 PrintWriter err)
87 {
88 super(out, err);
89 }
90
91
92
93 /***
94 * Print a usage message to System.err.
95 */
96 public void printUsageHeader()
97 {
98 printlnErr("Usage: <options>.. <arguments>..");
99 printlnErr(indent
100 + "-s <path> <classname>..");
101 printlnErr(indent
102 + " <classfile>..");
103
104
105
106 }
107
108 /***
109 * Print a usage message to System.err.
110 */
111 public void printArgumentUsage()
112 {
113 printlnErr(indent
114 + "<classname> the fully qualified name of a Java class");
115 printlnErr(indent
116 + "<classfile> the name of a .class file");
117 printlnErr(indent
118 + "<archivefile> the name of a .zip or .jar file");
119 }
120
121 /***
122 * Print arguments.
123 */
124 public void printArguments()
125 {
126 println();
127 println(argumentsHeader);
128 printListArgument("classNames", classNames);
129 printListArgument("classFileNames", classFileNames);
130 printListArgument("archiveFileNames", archiveFileNames);
131 }
132
133 /***
134 * Print argument of list type.
135 */
136 public void printListArgument(String name, List list)
137 {
138 print(indent);
139 final StringBuffer s = new StringBuffer();
140 for (Iterator i = list.iterator(); i.hasNext();) {
141 s.append(" " + i.next());
142 }
143 println(name + " = {" + s.toString() + " }");
144 println();
145 }
146
147 /***
148 * Check options and arguments.
149 */
150 public int check()
151 {
152 int res;
153 if ((res = super.check()) != OK) {
154 return res;
155 }
156
157
158 for (Iterator names = arguments.iterator(); names.hasNext();) {
159 final String name = (String)names.next();
160 if (isClassFileName(name)) {
161 classFileNames.add(name);
162 } else if (isArchiveFileName(name)) {
163 archiveFileNames.add(name);
164 } else {
165 classNames.add(name);
166 }
167 }
168
169 if (verbose.value) {
170 printAll();
171 }
172
173
174 final int argTypes = ((classNames.isEmpty() ? 0 : 1)
175 + (classFileNames.isEmpty() ? 0 : 1)
176 + (archiveFileNames.isEmpty() ? 0 : 1));
177 if (argTypes == 0) {
178 printUsageError("No class arguments: specify classes either by class name, class file, or archive file");
179 return USAGE_ERROR;
180 }
181 if (argTypes > 1) {
182 printUsageError("Mixed class arguments: specify classes by either class name, class file, or archive file");
183 return USAGE_ERROR;
184 }
185
186
187 if (sourcePath.value == null && !classNames.isEmpty()) {
188 printUsageError("No source-path specified for lookup of classes");
189 return USAGE_ERROR;
190 }
191 if (sourcePath.value != null && classNames.isEmpty()) {
192 printUsageError("No source-path can be specified with class or archive files");
193 return USAGE_ERROR;
194 }
195
196
197 if (!archiveFileNames.isEmpty()) {
198 printUsageError("Sorry, support for archive files currently disabled");
199 return USAGE_ERROR;
200 }
201
202 return OK;
203 }
204
205
206
207 /***
208 * Tests the class.
209 */
210 static public void main(String[] args)
211 {
212 final PrintWriter out = new PrintWriter(System.out, true);
213 out.println("--> ClassArgOptions.main()");
214 final ClassArgOptions options = new ClassArgOptions(out, out);
215 out.println(" options.process() ...");
216 int res = options.process(args);
217 out.println(" return value: " + res);
218 out.println("<-- ClassArgOptions.main()");
219 }
220 }