KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > diff > ListDocumentedElements


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.diff;
34
35 import java.io.*;
36 import java.util.*;
37
38 import com.sun.javadoc.*;
39
40 public class ListDocumentedElements {
41     private static String JavaDoc tagName = null;
42     private static Collection validValues = new HashSet();
43     private static Collection invalidValues = new HashSet();
44     private static PrintWriter out = new PrintWriter(System.out);
45     
46     public static boolean start(RootDoc root) {
47         process(root.specifiedPackages());
48         process(root.classes());
49         out.close();
50         return true;
51     }
52
53     public static int optionLength(String JavaDoc option) {
54         int result = 0;
55
56         if (option.equals("-tag")) {
57             result = 2;
58         } else if (option.equals("-valid")) {
59             result = 2;
60         } else if (option.equals("-invalid")) {
61             result = 2;
62         } else if (option.equals("-out")) {
63             result = 2;
64         }
65
66         return result;
67     }
68
69     public static boolean validOptions(String JavaDoc options[][], DocErrorReporter reporter) {
70         boolean valid = true;
71         
72         for (int i=0; valid && i<options.length; i++) {
73             if (options[i][0].equals("-tag")) {
74                 if (tagName == null) {
75                     tagName = options[i][1];
76                 } else {
77                     reporter.printError("Only one -tag option allowed.");
78                     valid = false;
79                 }
80             } else if (options[i][0].equals("-valid")) {
81                 validValues.add(options[i][1]);
82             } else if (options[i][0].equals("-invalid")) {
83                 invalidValues.add(options[i][1]);
84             } else if (options[i][0].equals("-out")) {
85                 try {
86                     out = new PrintWriter(new FileWriter(options[i][1]));
87                 } catch (IOException ex) {
88                     reporter.printError("Could not open output file \"" + options[i][1] + "\": " + ex);
89                     valid = false;
90                 }
91             }
92         }
93
94         valid = valid && tagName != null;
95         
96         if (!valid) {
97             reporter.printError("Usage: javadoc -tag mytag [-valid value]* [-invalid value]* -doclet ListPublicElements ...");
98         }
99         
100         return valid;
101     }
102     
103     private static void process(PackageDoc[] docs) {
104         for (int i = 0; i < docs.length; ++i) {
105             process(docs[i]);
106         }
107     }
108     
109     private static void process(PackageDoc doc) {
110         out.print(doc.name());
111         out.println(" [P]");
112     }
113     
114     private static void process(ProgramElementDoc[] docs) {
115         for (int i = 0; i < docs.length; ++i) {
116             process(docs[i]);
117         }
118     }
119
120     private static void process(ProgramElementDoc doc) {
121         boolean isVisible = !doc.name().equals("<clinit>");
122
123         Tag[] tags = doc.tags(tagName);
124
125         if (isVisible) {
126             if (!validValues.isEmpty()) {
127                 // If it contains at least one valid value, then it will be visible
128
isVisible = false;
129                 for (int i = 0; i < tags.length; ++i) {
130                     if (validValues.contains(tags[i].text())) {
131                         isVisible = true;
132                     }
133                 }
134             } else if (!invalidValues.isEmpty()) {
135                 // Else if it contains at least one invalid value, then it will not be visible
136
for (int i = 0; i < tags.length; ++i) {
137                     if (invalidValues.contains(tags[i].text())) {
138                         isVisible = false;
139                     }
140                 }
141             }
142         }
143
144         if (isVisible) {
145             out.print(doc.qualifiedName());
146             if (doc instanceof ConstructorDoc) {
147                 out.print(".");
148                 out.print(doc.name());
149             }
150             if (doc instanceof ExecutableMemberDoc) {
151                 out.print(((ExecutableMemberDoc) doc).signature());
152             }
153
154             if (doc instanceof ClassDoc) {
155                 out.println(" [C]");
156                 process(((ClassDoc) doc).fields());
157                 process(((ClassDoc) doc).constructors());
158                 process(((ClassDoc) doc).methods());
159                 process(((ClassDoc) doc).innerClasses());
160             } else {
161                 out.println(" [F]");
162             }
163         }
164     }
165 }
166
Popular Tags