KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > tools > ajdoc > Ajdoc


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  */

22 package org.aspectj.tools.ajdoc;
23
24 import org.aspectj.compiler.base.CompilerErrors;
25 import org.aspectj.compiler.base.ExitRequestException;
26 import org.aspectj.compiler.base.InternalCompilerError;
27
28 import com.sun.javadoc.RootDoc;
29
30 import java.io.FileNotFoundException JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.PrintWriter JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.List JavaDoc;
35
36 /**
37  * Front-end for ajdoc.
38  *
39  */

40 public class Ajdoc extends AjdocCompiler {
41
42     private final static int VERBOSE = 1;
43     private final static int WARNINGS = 4;
44    
45     private DocletProxy docletProxy;
46     private RootDocMaker rootDocMaker;
47     private String JavaDoc source = null;
48     private String JavaDoc extdirs = null;
49     private String JavaDoc locale = null;
50     private String JavaDoc encoding = null;
51     private String JavaDoc sourcepath = null;
52     private String JavaDoc classpath = null;
53     private String JavaDoc bootclasspath = null;
54     private int verbosity = WARNINGS;
55     private List JavaDoc filenamesAndPackages = new ArrayList JavaDoc();
56     private List JavaDoc options = new ArrayList JavaDoc();
57
58     public Ajdoc(String JavaDoc programName) {
59         super(programName);
60     }
61
62     public Ajdoc() {
63         this("ajdoc");
64     }
65
66     /**
67      * Programmatic entry into this compiler that
68      * uses the error printer to catch internal errors.
69      *
70      * @param args Command line arguments.
71      * @return <code>0</code> for a successful document.
72      */

73     public int execute(String JavaDoc[] args) {
74         try {
75             return doc(args) && err.getNumErrors() == 0 ? 0 : 1;
76         } catch (ExitRequestException exit) {
77             return exit.getValue();
78         } catch (CompilerErrors err) {
79             return err.errors; // report error already printed by ajc
80
} catch (InternalCompilerError error) { // cf ajc.Main
81
handleInternalError(error.uncaughtThrowable);
82             error.showWhere(new PrintWriter JavaDoc(System.err));
83             return -2;
84         } catch (Exception JavaDoc e) {
85             e.printStackTrace(System.err);
86             err.internalError("internal_msg", e);
87             return 1;
88         }
89     }
90
91     /** copied from ajc.Main */
92     public void handleInternalError(Throwable JavaDoc uncaughtThrowable) {
93         System.err.println("An internal error occured in Ajdoc invocation of AspectJ-"
94                            +getCompiler().getVersion());
95         System.err.println(uncaughtThrowable.toString());
96         uncaughtThrowable.printStackTrace(System.err);
97         System.err.println();
98     }
99
100     /**
101      * Programmatic entry into this compiler that
102      * doesn't use the error printer to catch internal errors.
103      *
104      * @param args Command line arguments.
105      * @return <code>true</code> for a succesful run.
106      */

107     public boolean doc(String JavaDoc[] args) {
108
109         long start = System.currentTimeMillis();
110
111         if (args == null) {
112             err.error("internal_error", "Arguments cannot be null");
113             return false;
114         }
115
116         try {
117             args = expandAndCreateDoclet(args);
118         } catch (FileNotFoundException JavaDoc e) {
119             err.error("file_not_found_exception", e.getMessage());
120             return false;
121         } catch (IOException JavaDoc e) {
122             err.error("io_exception", e.getMessage());
123             return false;
124         }
125         
126         for (int i = 0; i < args.length;) {
127             String JavaDoc arg = args[i++];
128             if (arg.equals("-overview")) {
129                 set(args, i++);
130             } else if (arg.equals("-public")) {
131                 set(arg);
132                 if (filter != null) {
133                     err.error("argument_already_seen", arg);
134                 } else {
135                     setFilter(AccessChecker.PUBLIC, arg);
136                 }
137             } else if (arg.equals("-protected")) {
138                 set(arg);
139                 if (filter != null) {
140                     err.error("argument_already_seen", arg);
141                 } else {
142                     setFilter(AccessChecker.PROTECTED, arg);
143                 }
144             } else if (arg.equals("-package")) {
145                 set(arg);
146                 if (filter != null) {
147                     err.error("argument_already_seen", arg);
148                 } else {
149                     setFilter(AccessChecker.PACKAGE, arg);
150                 }
151             } else if (arg.equals("-private")) {
152                 set(arg);
153                 if (filter != null) {
154                     err.error("argument_already_seen", arg);
155                 } else {
156                     setFilter(AccessChecker.PRIVATE, arg);
157                 }
158             } else if (arg.equals("-help")) {
159                 usage(0);
160             } else if (arg.equals("-sourcepath")) {
161                 if (sourcepath != null) {
162                     usage("argument_already_seen", arg);
163                 }
164                 sourcepath = set(args, i++);
165             }else if (arg.equals("-classpath")) {
166                 if (classpath != null) {
167                     usage("argument_already_seen", arg);
168                 }
169                 classpath = set(args, i++);
170             }else if (arg.equals("-bootclasspath")) {
171                 if (bootclasspath != null) {
172                     usage("argument_already_seen", arg);
173                 }
174                 bootclasspath = set(args, i++);
175             }else if (arg.equals("-source")) {
176                 if (source != null) {
177                     usage("argument_already_seen", arg);
178                 }
179                 source = set(args, i++);
180             } else if (arg.equals("-extdirs")) {
181                 if (extdirs != null) {
182                     usage("argument_already_seen", arg);
183                 }
184                 extdirs = set(args, i++);
185             } else if (arg.equals("-verbose")) {
186                 set(arg);
187                 verbosity |= VERBOSE;
188             } else if (arg.equals("-locale")) {
189                 if (locale != null) {
190                     usage("argument_already_seen", arg);
191                 }
192                 set(args, i++);
193             } else if (arg.equals("-encoding")) {
194                 if (encoding != null) {
195                     usage("argument_already_seen", arg);
196                 }
197                 encoding = set(args, i++);
198             } else if (arg.equals("-compiler")) {
199                 err.warning("usage_help", "-compiler option ignored");
200             } else if (arg.equals("-debug")) {
201                 err.warning("usage_help", "-debug option disabled");
202             } else if (arg.startsWith("-J")) { // todo unsupported?
203
if (arg.length() == 2) continue;
204                 String JavaDoc rest = arg.substring(2);
205                 int ieq = rest.indexOf('=');
206                 String JavaDoc key, val;
207                 if (ieq != -1) {
208                     key = rest.substring(0, ieq);
209                     val = rest.substring(ieq+1);
210                 } else {
211                     key = rest;
212                     val = "";
213                 }
214                 System.setProperty(key, val);
215             } else if (arg.startsWith("-")) {
216                 int optionLength = docletProxy.optionLength(arg);
217                 if (optionLength < 0) {
218                     exit();
219                 } else if (optionLength == 0) {
220                     usage("invalid_flag", arg);
221                 } else if (optionLength > args.length) {
222                     usage("requires_argument", arg, optionLength+"");
223                 } else {
224                     List JavaDoc iargs = new ArrayList JavaDoc();
225                     iargs.add(arg);
226                     for (int j = 0; j < optionLength-1; j++) {
227                         iargs.add(args[i++]);
228                     }
229                     set((String JavaDoc[])iargs.toArray(new String JavaDoc[iargs.size()]));
230                 }
231             } else {
232                 filenamesAndPackages.add(arg);
233             }
234         }
235         if (locale == null) {
236             locale = "";
237         }
238         if (sourcepath == null) {
239             sourcepath = ".";
240         }
241         try {
242             if (!docletProxy.validOptions(options, err)) {
243                 exit(1);
244             }
245         } catch (IOException JavaDoc e) {
246             e.printStackTrace(System.err);
247             err.internalError("internal_msg", e);
248             return false;
249         }
250         if (filenamesAndPackages.size() < 1) {
251             usage("No_packages_or_classes_specified");
252             return false;
253         }
254         RootDoc rootDoc = null;
255         boolean good = true;
256         try {
257             rootDoc = makeRootDoc(sourcepath,
258                                   classpath,
259                                   bootclasspath,
260                                   extdirs,
261                                   verbosity,
262                                   encoding,
263                                   locale,
264                                   source,
265                                   filenamesAndPackages,
266                                   options,
267                                   err,
268                                   programName,
269                                   getFilter());
270         } catch (CannotMakeRootDocException e) {
271             err.error("cant_create_root_doc_ex", "AjdocCompiler", e.getMessage());
272             exit(1);
273             good = false;
274         }
275         good &= rootDoc != null;
276         if (good) {
277             err.notice("generating_docs");
278             try {
279                 good &= docletProxy.start(rootDoc);
280             } catch (IOException JavaDoc e) {
281                 e.printStackTrace(System.err);
282                 err.internalError("internal_msg", e);
283                 return false;
284             }
285         }
286         if ((verbosity & VERBOSE) != 0) {
287             err.notice("done_in", Long.toString(System.currentTimeMillis()-start));
288         }
289         return good;
290     }
291     
292
293     private void usage(String JavaDoc key, String JavaDoc s0, String JavaDoc s1) {
294         err.error(key, s0, s1);
295         usage(1);
296     }
297
298     private void usage(String JavaDoc key, String JavaDoc s0) {
299         usage(key,s0,"");
300     }
301
302     private void usage(String JavaDoc key) {
303         usage(key,"");
304     }
305
306     private void usage() {
307         err.notice("usage_help", programName);
308         if (docletProxy != null) { docletProxy.optionLength("-help"); }
309     }
310     
311     private void usage(int exit) {
312         usage();
313         exit(exit);
314     }
315
316     protected String JavaDoc[] expandAndCreateDoclet(String JavaDoc[] args) throws IOException JavaDoc {
317         List JavaDoc list = new ArrayList JavaDoc();
318         docletProxy = DocletProxy.DEFAULT;
319         for (int i = 0; i < args.length;) {
320             String JavaDoc arg = args[i++];
321             if (arg == null || arg.length() < 1) continue;
322             if (arg.charAt(0) == '@') {
323                 expandAtFile(arg.substring(1), list);
324             } else if (arg.equals("-argfile")) {
325                 if (check(args, i)) {
326                     expandAtFile(args[i++], list);
327                 }
328             } else if (arg.equals("-doclet")) {
329                 err.warning("usage_help", "-doclet option ignored");
330             } else if (arg.equals("-docletpath")) {
331                 err.warning("usage_help", "-docletpath option ignored");
332             } else if (arg.equals("-standard")) {
333                 docletProxy = DocletProxy.STANDARD;
334             } else {
335                 list.add(arg);
336             }
337         }
338         return (String JavaDoc[])list.toArray(new String JavaDoc[list.size()]);
339     }
340
341     private static boolean check(String JavaDoc[] args, int i) {
342         return i >= 0 && i < args.length;
343     }
344
345     private String JavaDoc set(String JavaDoc[] args, int i) {
346         String JavaDoc arg = null;
347         if (check(args, i)) {
348             arg = args[i];
349             set(args[i-1], arg);
350         } else {
351             err.internalError("internal_error",
352                               new ArrayIndexOutOfBoundsException JavaDoc(i));
353         }
354         return arg;
355     }
356
357     private void set(String JavaDoc opt) {
358         set(new String JavaDoc[]{opt});
359     }
360
361     private void set(String JavaDoc opt, String JavaDoc arg) {
362         set(new String JavaDoc[]{opt, arg});
363     }
364
365     private void set(String JavaDoc[] opts) {
366         options.add(opts);
367     }
368
369     protected void internalCompile(List JavaDoc filenames) {
370         super.internalCompile(filenames);
371     }
372
373     private final void exit() {
374         exit(0);
375     }
376     
377     private void exit(int exit) {
378         throw new ExitRequestException(exit);
379     }
380
381     private static String JavaDoc classname(Object JavaDoc o) {
382         return o != null ? o.getClass().getName() : "null";
383     }
384 }
385
Popular Tags