duke@1: /*
jjg@1358: * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1: *
duke@1: * This code is free software; you can redistribute it and/or modify it
duke@1: * under the terms of the GNU General Public License version 2 only, as
ohair@554: * published by the Free Software Foundation. Oracle designates this
duke@1: * particular file as subject to the "Classpath" exception as provided
ohair@554: * by Oracle in the LICENSE file that accompanied this code.
duke@1: *
duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1: * version 2 for more details (a copy is included in the LICENSE file that
duke@1: * accompanied this code).
duke@1: *
duke@1: * You should have received a copy of the GNU General Public License version
duke@1: * 2 along with this work; if not, write to the Free Software Foundation,
duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1: *
ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554: * or visit www.oracle.com if you need additional information or have any
ohair@554: * questions.
duke@1: */
duke@1:
duke@1: package com.sun.tools.doclets.internal.toolkit.util;
duke@1:
bpatel@995: import java.util.*;
duke@1: import com.sun.javadoc.*;
bpatel@995: import com.sun.tools.doclets.internal.toolkit.Configuration;
duke@1:
duke@1: /**
duke@1: * This class acts as an artificial PackageDoc for classes specified
duke@1: * on the command line when running Javadoc. For example, if you
duke@1: * specify several classes from package java.lang, this class will catalog
duke@1: * those classes so that we can retrieve all of the classes from a particular
duke@1: * package later.
duke@1: *
jjg@1359: *
This is NOT part of any supported API.
jjg@1359: * If you write code that depends on this, you do so at your own risk.
jjg@1359: * This code and its internal interfaces are subject to change or
jjg@1359: * deletion without notice.
duke@1: *
duke@1: * @author Jamie Ho
duke@1: * @since 1.4
duke@1: */
duke@1:
duke@1: public class ClassDocCatalog {
duke@1:
duke@1: /**
duke@1: * Stores the set of packages that the classes specified on the command line
duke@1: * belong to. Note that the default package is "".
duke@1: */
jjg@74: private Set packageSet;
duke@1:
duke@1:
duke@1: /**
duke@1: * Stores all classes for each package
duke@1: */
jjg@74: private Map> allClasses;
duke@1:
duke@1: /**
duke@1: * Stores ordinary classes (excluding Exceptions and Errors) for each
duke@1: * package
duke@1: */
jjg@74: private Map> ordinaryClasses;
duke@1:
duke@1: /**
duke@1: * Stores exceptions for each package
duke@1: */
jjg@74: private Map> exceptions;
duke@1:
duke@1: /**
duke@1: * Stores enums for each package.
duke@1: */
jjg@74: private Map> enums;
duke@1:
duke@1: /**
duke@1: * Stores annotation types for each package.
duke@1: */
jjg@74: private Map> annotationTypes;
duke@1:
duke@1: /**
duke@1: * Stores errors for each package
duke@1: */
jjg@74: private Map> errors;
duke@1:
duke@1: /**
duke@1: * Stores interfaces for each package
duke@1: */
jjg@74: private Map> interfaces;
duke@1:
bpatel@995: private Configuration configuration;
bpatel@995:
duke@1: /**
duke@1: * Construct a new ClassDocCatalog.
duke@1: *
duke@1: * @param classdocs the array of ClassDocs to catalog
duke@1: */
bpatel@995: public ClassDocCatalog (ClassDoc[] classdocs, Configuration config) {
duke@1: init();
bpatel@995: this.configuration = config;
duke@1: for (int i = 0; i < classdocs.length; i++) {
duke@1: addClassDoc(classdocs[i]);
duke@1: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * Construct a new ClassDocCatalog.
duke@1: *
duke@1: */
duke@1: public ClassDocCatalog () {
duke@1: init();
duke@1: }
duke@1:
duke@1: private void init() {
jjg@74: allClasses = new HashMap>();
jjg@74: ordinaryClasses = new HashMap>();
jjg@74: exceptions = new HashMap>();
jjg@74: enums = new HashMap>();
jjg@74: annotationTypes = new HashMap>();
jjg@74: errors = new HashMap>();
jjg@74: interfaces = new HashMap>();
jjg@74: packageSet = new HashSet();
duke@1: }
duke@1:
duke@1: /**
duke@1: * Add the given class to the catalog.
duke@1: * @param classdoc the ClassDoc to add to the catelog.
duke@1: */
duke@1: public void addClassDoc(ClassDoc classdoc) {
duke@1: if (classdoc == null) {
duke@1: return;
duke@1: }
duke@1: addClass(classdoc, allClasses);
duke@1: if (classdoc.isOrdinaryClass()) {
duke@1: addClass(classdoc, ordinaryClasses);
duke@1: } else if (classdoc.isException()) {
duke@1: addClass(classdoc, exceptions);
duke@1: } else if (classdoc.isEnum()) {
duke@1: addClass(classdoc, enums);
duke@1: } else if (classdoc.isAnnotationType()) {
duke@1: addClass(classdoc, annotationTypes);
duke@1: } else if (classdoc.isError()) {
duke@1: addClass(classdoc, errors);
duke@1: } else if (classdoc.isInterface()) {
duke@1: addClass(classdoc, interfaces);
duke@1: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * Add the given class to the given map.
duke@1: * @param classdoc the ClassDoc to add to the catelog.
duke@1: * @param map the Map to add the ClassDoc to.
duke@1: */
jjg@74: private void addClass(ClassDoc classdoc, Map> map) {
duke@1:
duke@1: PackageDoc pkg = classdoc.containingPackage();
bpatel@995: if (pkg.isIncluded() || (configuration.nodeprecated && Util.isDeprecated(pkg))) {
bpatel@995: //No need to catalog this class if it's package is
bpatel@995: //included on the command line or if -nodeprecated option is set
bpatel@995: // and the containing package is marked as deprecated.
duke@1: return;
duke@1: }
duke@1: String key = Util.getPackageName(pkg);
jjg@74: Set s = map.get(key);
duke@1: if (s == null) {
duke@1: packageSet.add(key);
jjg@74: s = new HashSet();
duke@1: }
duke@1: s.add(classdoc);
duke@1: map.put(key, s);
duke@1:
duke@1: }
duke@1:
jjg@74: private ClassDoc[] getArray(Map> m, String key) {
jjg@74: Set s = m.get(key);
duke@1: if (s == null) {
duke@1: return new ClassDoc[] {};
duke@1: } else {
jjg@74: return s.toArray(new ClassDoc[] {});
duke@1: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * Return all of the classes specified on the command-line that
duke@1: * belong to the given package.
jjg@1358: * @param pkgDoc the package to return the classes for.
duke@1: */
duke@1: public ClassDoc[] allClasses(PackageDoc pkgDoc) {
duke@1: return pkgDoc.isIncluded() ?
duke@1: pkgDoc.allClasses() :
duke@1: getArray(allClasses, Util.getPackageName(pkgDoc));
duke@1: }
duke@1:
duke@1: /**
duke@1: * Return all of the classes specified on the command-line that
duke@1: * belong to the given package.
duke@1: * @param packageName the name of the package specified on the
duke@1: * command-line.
duke@1: */
duke@1: public ClassDoc[] allClasses(String packageName) {
duke@1: return getArray(allClasses, packageName);
duke@1: }
duke@1:
duke@1: /**
duke@1: * Return the array of package names that this catalog stores
duke@1: * ClassDocs for.
duke@1: */
duke@1: public String[] packageNames() {
jjg@74: return packageSet.toArray(new String[] {});
duke@1: }
duke@1:
duke@1: /**
duke@1: * Return true if the given package is known to this catalog.
duke@1: * @param packageName the name to check.
duke@1: * @return true if this catalog has any information about
duke@1: * classes in the given package.
duke@1: */
duke@1: public boolean isKnownPackage(String packageName) {
duke@1: return packageSet.contains(packageName);
duke@1: }
duke@1:
duke@1:
duke@1: /**
duke@1: * Return all of the errors specified on the command-line
duke@1: * that belong to the given package.
duke@1: * @param packageName the name of the package specified on the
duke@1: * command-line.
duke@1: */
duke@1: public ClassDoc[] errors(String packageName) {
duke@1: return getArray(errors, packageName);
duke@1: }
duke@1:
duke@1: /**
duke@1: * Return all of the exceptions specified on the command-line
duke@1: * that belong to the given package.
duke@1: * @param packageName the name of the package specified on the
duke@1: * command-line.
duke@1: */
duke@1: public ClassDoc[] exceptions(String packageName) {
duke@1: return getArray(exceptions, packageName);
duke@1: }
duke@1:
duke@1: /**
duke@1: * Return all of the enums specified on the command-line
duke@1: * that belong to the given package.
duke@1: * @param packageName the name of the package specified on the
duke@1: * command-line.
duke@1: */
duke@1: public ClassDoc[] enums(String packageName) {
duke@1: return getArray(enums, packageName);
duke@1: }
duke@1:
duke@1: /**
duke@1: * Return all of the annotation types specified on the command-line
duke@1: * that belong to the given package.
duke@1: * @param packageName the name of the package specified on the
duke@1: * command-line.
duke@1: */
duke@1: public ClassDoc[] annotationTypes(String packageName) {
duke@1: return getArray(annotationTypes, packageName);
duke@1: }
duke@1:
duke@1: /**
duke@1: * Return all of the interfaces specified on the command-line
duke@1: * that belong to the given package.
duke@1: * @param packageName the name of the package specified on the
duke@1: * command-line.
duke@1: */
duke@1: public ClassDoc[] interfaces(String packageName) {
duke@1: return getArray(interfaces, packageName);
duke@1: }
duke@1:
duke@1: /**
duke@1: * Return all of the ordinary classes specified on the command-line
duke@1: * that belong to the given package.
duke@1: * @param packageName the name of the package specified on the
duke@1: * command-line.
duke@1: */
duke@1: public ClassDoc[] ordinaryClasses(String packageName) {
duke@1: return getArray(ordinaryClasses, packageName);
duke@1: }
duke@1: }