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