001: /*
002: * Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.doclets.internal.toolkit.util;
027:
028: import com.sun.javadoc.*;
029: import java.util.*;
030:
031: /**
032: * This class acts as an artificial PackageDoc for classes specified
033: * on the command line when running Javadoc. For example, if you
034: * specify several classes from package java.lang, this class will catalog
035: * those classes so that we can retrieve all of the classes from a particular
036: * package later.
037: *
038: * This code is not part of an API.
039: * It is implementation that is subject to change.
040: * Do not use it as an API
041: *
042: * @author Jamie Ho
043: * @since 1.4
044: */
045:
046: public class ClassDocCatalog {
047:
048: /**
049: * Stores the set of packages that the classes specified on the command line
050: * belong to. Note that the default package is "".
051: */
052: private Set packageSet;
053:
054: /**
055: * Stores all classes for each package
056: */
057: private Map allClasses;
058:
059: /**
060: * Stores ordinary classes (excluding Exceptions and Errors) for each
061: * package
062: */
063: private Map ordinaryClasses;
064:
065: /**
066: * Stores exceptions for each package
067: */
068: private Map exceptions;
069:
070: /**
071: * Stores enums for each package.
072: */
073: private Map enums;
074:
075: /**
076: * Stores annotation types for each package.
077: */
078: private Map annotationTypes;
079:
080: /**
081: * Stores errors for each package
082: */
083: private Map errors;
084:
085: /**
086: * Stores interfaces for each package
087: */
088: private Map interfaces;
089:
090: /**
091: * Construct a new ClassDocCatalog.
092: *
093: * @param classdocs the array of ClassDocs to catalog
094: */
095: public ClassDocCatalog(ClassDoc[] classdocs) {
096: init();
097: for (int i = 0; i < classdocs.length; i++) {
098: addClassDoc(classdocs[i]);
099: }
100: }
101:
102: /**
103: * Construct a new ClassDocCatalog.
104: *
105: */
106: public ClassDocCatalog() {
107: init();
108: }
109:
110: private void init() {
111: allClasses = new HashMap();
112: ordinaryClasses = new HashMap();
113: exceptions = new HashMap();
114: enums = new HashMap();
115: annotationTypes = new HashMap();
116: errors = new HashMap();
117: interfaces = new HashMap();
118: packageSet = new HashSet();
119: }
120:
121: /**
122: * Add the given class to the catalog.
123: * @param classdoc the ClassDoc to add to the catelog.
124: */
125: public void addClassDoc(ClassDoc classdoc) {
126: if (classdoc == null) {
127: return;
128: }
129: addClass(classdoc, allClasses);
130: if (classdoc.isOrdinaryClass()) {
131: addClass(classdoc, ordinaryClasses);
132: } else if (classdoc.isException()) {
133: addClass(classdoc, exceptions);
134: } else if (classdoc.isEnum()) {
135: addClass(classdoc, enums);
136: } else if (classdoc.isAnnotationType()) {
137: addClass(classdoc, annotationTypes);
138: } else if (classdoc.isError()) {
139: addClass(classdoc, errors);
140: } else if (classdoc.isInterface()) {
141: addClass(classdoc, interfaces);
142: }
143: }
144:
145: /**
146: * Add the given class to the given map.
147: * @param classdoc the ClassDoc to add to the catelog.
148: * @param map the Map to add the ClassDoc to.
149: */
150: private void addClass(ClassDoc classdoc, Map map) {
151:
152: PackageDoc pkg = classdoc.containingPackage();
153: if (pkg.isIncluded()) {
154: //No need to catalog this class since it's package is
155: //included on the command line
156: return;
157: }
158: String key = Util.getPackageName(pkg);
159: Set s = (Set) map.get(key);
160: if (s == null) {
161: packageSet.add(key);
162: s = new HashSet();
163: }
164: s.add(classdoc);
165: map.put(key, s);
166:
167: }
168:
169: private ClassDoc[] getArray(Map m, String key) {
170: Set s = (Set) m.get(key);
171: if (s == null) {
172: return new ClassDoc[] {};
173: } else {
174: return (ClassDoc[]) s.toArray(new ClassDoc[] {});
175: }
176: }
177:
178: /**
179: * Return all of the classes specified on the command-line that
180: * belong to the given package.
181: * @param packageDoc the package to return the classes for.
182: */
183: public ClassDoc[] allClasses(PackageDoc pkgDoc) {
184: return pkgDoc.isIncluded() ? pkgDoc.allClasses() : getArray(
185: allClasses, Util.getPackageName(pkgDoc));
186: }
187:
188: /**
189: * Return all of the classes specified on the command-line that
190: * belong to the given package.
191: * @param packageName the name of the package specified on the
192: * command-line.
193: */
194: public ClassDoc[] allClasses(String packageName) {
195: return getArray(allClasses, packageName);
196: }
197:
198: /**
199: * Return the array of package names that this catalog stores
200: * ClassDocs for.
201: */
202: public String[] packageNames() {
203: return (String[]) packageSet.toArray(new String[] {});
204: }
205:
206: /**
207: * Return true if the given package is known to this catalog.
208: * @param packageName the name to check.
209: * @return true if this catalog has any information about
210: * classes in the given package.
211: */
212: public boolean isKnownPackage(String packageName) {
213: return packageSet.contains(packageName);
214: }
215:
216: /**
217: * Return all of the errors specified on the command-line
218: * that belong to the given package.
219: * @param packageName the name of the package specified on the
220: * command-line.
221: */
222: public ClassDoc[] errors(String packageName) {
223: return getArray(errors, packageName);
224: }
225:
226: /**
227: * Return all of the exceptions specified on the command-line
228: * that belong to the given package.
229: * @param packageName the name of the package specified on the
230: * command-line.
231: */
232: public ClassDoc[] exceptions(String packageName) {
233: return getArray(exceptions, packageName);
234: }
235:
236: /**
237: * Return all of the enums specified on the command-line
238: * that belong to the given package.
239: * @param packageName the name of the package specified on the
240: * command-line.
241: */
242: public ClassDoc[] enums(String packageName) {
243: return getArray(enums, packageName);
244: }
245:
246: /**
247: * Return all of the annotation types specified on the command-line
248: * that belong to the given package.
249: * @param packageName the name of the package specified on the
250: * command-line.
251: */
252: public ClassDoc[] annotationTypes(String packageName) {
253: return getArray(annotationTypes, packageName);
254: }
255:
256: /**
257: * Return all of the interfaces specified on the command-line
258: * that belong to the given package.
259: * @param packageName the name of the package specified on the
260: * command-line.
261: */
262: public ClassDoc[] interfaces(String packageName) {
263: return getArray(interfaces, packageName);
264: }
265:
266: /**
267: * Return all of the ordinary classes specified on the command-line
268: * that belong to the given package.
269: * @param packageName the name of the package specified on the
270: * command-line.
271: */
272: public ClassDoc[] ordinaryClasses(String packageName) {
273: return getArray(ordinaryClasses, packageName);
274: }
275: }
|