Sat, 01 Dec 2007 00:00:00 +0000
Initial load
1 /*
2 * Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
26 package com.sun.tools.doclets.internal.toolkit.util;
28 import com.sun.javadoc.*;
29 import java.util.*;
31 /**
32 * This class acts as an artificial PackageDoc for classes specified
33 * on the command line when running Javadoc. For example, if you
34 * specify several classes from package java.lang, this class will catalog
35 * those classes so that we can retrieve all of the classes from a particular
36 * package later.
37 *
38 * This code is not part of an API.
39 * It is implementation that is subject to change.
40 * Do not use it as an API
41 *
42 * @author Jamie Ho
43 * @since 1.4
44 */
46 public class ClassDocCatalog {
48 /**
49 * Stores the set of packages that the classes specified on the command line
50 * belong to. Note that the default package is "".
51 */
52 private Set packageSet;
55 /**
56 * Stores all classes for each package
57 */
58 private Map allClasses;
60 /**
61 * Stores ordinary classes (excluding Exceptions and Errors) for each
62 * package
63 */
64 private Map ordinaryClasses;
66 /**
67 * Stores exceptions for each package
68 */
69 private Map exceptions;
71 /**
72 * Stores enums for each package.
73 */
74 private Map enums;
76 /**
77 * Stores annotation types for each package.
78 */
79 private Map annotationTypes;
81 /**
82 * Stores errors for each package
83 */
84 private Map errors;
86 /**
87 * Stores interfaces for each package
88 */
89 private Map interfaces;
91 /**
92 * Construct a new ClassDocCatalog.
93 *
94 * @param classdocs the array of ClassDocs to catalog
95 */
96 public ClassDocCatalog (ClassDoc[] classdocs) {
97 init();
98 for (int i = 0; i < classdocs.length; i++) {
99 addClassDoc(classdocs[i]);
100 }
101 }
103 /**
104 * Construct a new ClassDocCatalog.
105 *
106 */
107 public ClassDocCatalog () {
108 init();
109 }
111 private void init() {
112 allClasses = new HashMap();
113 ordinaryClasses = new HashMap();
114 exceptions = new HashMap();
115 enums = new HashMap();
116 annotationTypes = new HashMap();
117 errors = new HashMap();
118 interfaces = new HashMap();
119 packageSet = new HashSet();
120 }
122 /**
123 * Add the given class to the catalog.
124 * @param classdoc the ClassDoc to add to the catelog.
125 */
126 public void addClassDoc(ClassDoc classdoc) {
127 if (classdoc == null) {
128 return;
129 }
130 addClass(classdoc, allClasses);
131 if (classdoc.isOrdinaryClass()) {
132 addClass(classdoc, ordinaryClasses);
133 } else if (classdoc.isException()) {
134 addClass(classdoc, exceptions);
135 } else if (classdoc.isEnum()) {
136 addClass(classdoc, enums);
137 } else if (classdoc.isAnnotationType()) {
138 addClass(classdoc, annotationTypes);
139 } else if (classdoc.isError()) {
140 addClass(classdoc, errors);
141 } else if (classdoc.isInterface()) {
142 addClass(classdoc, interfaces);
143 }
144 }
146 /**
147 * Add the given class to the given map.
148 * @param classdoc the ClassDoc to add to the catelog.
149 * @param map the Map to add the ClassDoc to.
150 */
151 private void addClass(ClassDoc classdoc, Map map) {
153 PackageDoc pkg = classdoc.containingPackage();
154 if (pkg.isIncluded()) {
155 //No need to catalog this class since it's package is
156 //included on the command line
157 return;
158 }
159 String key = Util.getPackageName(pkg);
160 Set s = (Set) map.get(key);
161 if (s == null) {
162 packageSet.add(key);
163 s = new HashSet();
164 }
165 s.add(classdoc);
166 map.put(key, s);
168 }
170 private ClassDoc[] getArray(Map m, String key) {
171 Set s = (Set) m.get(key);
172 if (s == null) {
173 return new ClassDoc[] {};
174 } else {
175 return (ClassDoc[]) s.toArray(new ClassDoc[] {});
176 }
177 }
179 /**
180 * Return all of the classes specified on the command-line that
181 * belong to the given package.
182 * @param packageDoc the package to return the classes for.
183 */
184 public ClassDoc[] allClasses(PackageDoc pkgDoc) {
185 return pkgDoc.isIncluded() ?
186 pkgDoc.allClasses() :
187 getArray(allClasses, Util.getPackageName(pkgDoc));
188 }
190 /**
191 * Return all of the classes specified on the command-line that
192 * belong to the given package.
193 * @param packageName the name of the package specified on the
194 * command-line.
195 */
196 public ClassDoc[] allClasses(String packageName) {
197 return getArray(allClasses, packageName);
198 }
200 /**
201 * Return the array of package names that this catalog stores
202 * ClassDocs for.
203 */
204 public String[] packageNames() {
205 return (String[]) packageSet.toArray(new String[] {});
206 }
208 /**
209 * Return true if the given package is known to this catalog.
210 * @param packageName the name to check.
211 * @return true if this catalog has any information about
212 * classes in the given package.
213 */
214 public boolean isKnownPackage(String packageName) {
215 return packageSet.contains(packageName);
216 }
219 /**
220 * Return all of the errors specified on the command-line
221 * that belong to the given package.
222 * @param packageName the name of the package specified on the
223 * command-line.
224 */
225 public ClassDoc[] errors(String packageName) {
226 return getArray(errors, packageName);
227 }
229 /**
230 * Return all of the exceptions specified on the command-line
231 * that belong to the given package.
232 * @param packageName the name of the package specified on the
233 * command-line.
234 */
235 public ClassDoc[] exceptions(String packageName) {
236 return getArray(exceptions, packageName);
237 }
239 /**
240 * Return all of the enums specified on the command-line
241 * that belong to the given package.
242 * @param packageName the name of the package specified on the
243 * command-line.
244 */
245 public ClassDoc[] enums(String packageName) {
246 return getArray(enums, packageName);
247 }
249 /**
250 * Return all of the annotation types specified on the command-line
251 * that belong to the given package.
252 * @param packageName the name of the package specified on the
253 * command-line.
254 */
255 public ClassDoc[] annotationTypes(String packageName) {
256 return getArray(annotationTypes, packageName);
257 }
259 /**
260 * Return all of the interfaces specified on the command-line
261 * that belong to the given package.
262 * @param packageName the name of the package specified on the
263 * command-line.
264 */
265 public ClassDoc[] interfaces(String packageName) {
266 return getArray(interfaces, packageName);
267 }
269 /**
270 * Return all of the ordinary classes specified on the command-line
271 * that belong to the given package.
272 * @param packageName the name of the package specified on the
273 * command-line.
274 */
275 public ClassDoc[] ordinaryClasses(String packageName) {
276 return getArray(ordinaryClasses, packageName);
277 }
278 }