KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > doclets > TokenTypesDoclet


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 Oliver Burn
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation; either
8
// version 2.1 of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
// Lesser General Public License for more details.
14
//
15
// You should have received a copy of the GNU Lesser General Public
16
// License along with this library; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
////////////////////////////////////////////////////////////////////////////////
19
package com.puppycrawl.tools.checkstyle.doclets;
20
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.PrintStream JavaDoc;
24
25 import com.sun.javadoc.ClassDoc;
26 import com.sun.javadoc.DocErrorReporter;
27 import com.sun.javadoc.FieldDoc;
28 import com.sun.javadoc.RootDoc;
29
30 /**
31  * Doclet which is used to write property file with short descriptions
32  * (first sentences) of TokenTypes' constants.
33  *
34  * @author o_sukhodolsky
35  */

36 public class TokenTypesDoclet
37 {
38     /** Command line option to specify file to write output of the doclet. */
39     private static final String JavaDoc DEST_FILE_OPT = "-destfile";
40
41     /**
42      * The doclet's starter method.
43      * @param aRoot <code>RootDoc</code> given to the doclet
44      * @exception FileNotFoundException will be thrown if the doclet
45      * will be unable to write to the specified file.
46      * @return true if the given <code>RootDoc</code> is processed.
47      */

48     public static boolean start(RootDoc aRoot) throws FileNotFoundException JavaDoc
49     {
50         final String JavaDoc fileName = getDestFileName(aRoot.options());
51         final FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(fileName);
52         final PrintStream JavaDoc ps = new PrintStream JavaDoc(fos);
53         final ClassDoc[] classes = aRoot.classes();
54         if ((classes.length != 1) || !classes[0].name().equals("TokenTypes")) {
55             final String JavaDoc message =
56                 "The doclet should be used for TokenTypes only";
57             throw new IllegalArgumentException JavaDoc(message);
58         }
59
60         final FieldDoc[] fields = classes[0].fields();
61         for (int j = 0; j < fields.length; j++) {
62             final FieldDoc field = fields[j];
63             if (field.isStatic() && field.isPublic() && field.isFinal()
64                 && "int".equals((field.type().qualifiedTypeName())))
65             {
66                 if (field.firstSentenceTags().length != 1) {
67                     final String JavaDoc message = "Should be only one tag.";
68                     throw new IllegalArgumentException JavaDoc(message);
69                 }
70                 ps.println(field.name() + "="
71                            + field.firstSentenceTags()[0].text());
72             }
73         }
74
75         ps.close();
76         return true;
77     }
78
79     /**
80      * Returns option length (how many parts are in option).
81      * @param aOption option name to process
82      * @return option length (how many parts are in option).
83      */

84     public static int optionLength(String JavaDoc aOption)
85     {
86         if (DEST_FILE_OPT.equals(aOption)) {
87             return 2;
88         }
89         return 0;
90     }
91
92     /**
93      * Checks that only valid options was specified.
94      * @param aOptions all parsed options
95      * @param aReporter the reporter to report errors.
96      * @return true if only valid options was specified
97      */

98     public static boolean validOptions(String JavaDoc aOptions[][],
99                                        DocErrorReporter aReporter)
100     {
101         boolean foundDestFileOption = false;
102         for (int i = 0; i < aOptions.length; i++) {
103             final String JavaDoc[] opt = aOptions[i];
104             if (DEST_FILE_OPT.equals(opt[0])) {
105                 if (foundDestFileOption) {
106                     aReporter.printError("Only one -destfile option allowed.");
107                     return false;
108                 }
109                 foundDestFileOption = true;
110             }
111         }
112         if (!foundDestFileOption) {
113             final String JavaDoc message =
114                 "Usage: javadoc -destfile file -doclet TokenTypesDoclet ...";
115             aReporter.printError(message);
116         }
117         return foundDestFileOption;
118     }
119
120     /**
121      * Reads destination file name.
122      * @param aOptions all specified options.
123      * @return destination file name
124      */

125     private static String JavaDoc getDestFileName(String JavaDoc[][] aOptions)
126     {
127         String JavaDoc fileName = null;
128         for (int i = 0; i < aOptions.length; i++) {
129             final String JavaDoc[] opt = aOptions[i];
130             if (DEST_FILE_OPT.equals(opt[0])) {
131                 fileName = opt[1];
132             }
133         }
134         return fileName;
135     }
136 }
137
Popular Tags