001: /*
002: * Copyright 1998-2005 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.formats.html;
027:
028: import com.sun.tools.doclets.internal.toolkit.util.*;
029: import com.sun.javadoc.*;
030: import java.io.*;
031: import java.util.*;
032:
033: /**
034: * Generate package usage information.
035: *
036: * @author Robert G. Field
037: */
038: public class PackageUseWriter extends SubWriterHolderWriter {
039:
040: final PackageDoc pkgdoc;
041: final SortedMap usingPackageToUsedClasses = new TreeMap();
042:
043: /**
044: * Constructor.
045: *
046: * @param filename the file to be generated.
047: * @throws IOException
048: * @throws DocletAbortException
049: */
050: public PackageUseWriter(ConfigurationImpl configuration,
051: ClassUseMapper mapper, String filename, PackageDoc pkgdoc)
052: throws IOException {
053: super (configuration, DirectoryManager.getDirectoryPath(pkgdoc),
054: filename, DirectoryManager.getRelativePath(pkgdoc
055: .name()));
056: this .pkgdoc = pkgdoc;
057:
058: // by examining all classes in this package, find what packages
059: // use these classes - produce a map between using package and
060: // used classes.
061: ClassDoc[] content = pkgdoc.allClasses();
062: for (int i = 0; i < content.length; ++i) {
063: ClassDoc usedClass = content[i];
064: Set usingClasses = (Set) mapper.classToClass.get(usedClass
065: .qualifiedName());
066: if (usingClasses != null) {
067: for (Iterator it = usingClasses.iterator(); it
068: .hasNext();) {
069: ClassDoc usingClass = (ClassDoc) it.next();
070: PackageDoc usingPackage = usingClass
071: .containingPackage();
072: Set usedClasses = (Set) usingPackageToUsedClasses
073: .get(usingPackage.name());
074: if (usedClasses == null) {
075: usedClasses = new TreeSet();
076: usingPackageToUsedClasses.put(Util
077: .getPackageName(usingPackage),
078: usedClasses);
079: }
080: usedClasses.add(usedClass);
081: }
082: }
083: }
084: }
085:
086: /**
087: * Generate a class page.
088: *
089: * @param configuration the current configuration of the doclet.
090: * @param mapper the mapping of the class usage.
091: * @param pkgdoc the package doc being documented.
092: */
093: public static void generate(ConfigurationImpl configuration,
094: ClassUseMapper mapper, PackageDoc pkgdoc) {
095: PackageUseWriter pkgusegen;
096: String filename = "package-use.html";
097: try {
098: pkgusegen = new PackageUseWriter(configuration, mapper,
099: filename, pkgdoc);
100: pkgusegen.generatePackageUseFile();
101: pkgusegen.close();
102: } catch (IOException exc) {
103: configuration.standardmessage.error(
104: "doclet.exception_encountered", exc.toString(),
105: filename);
106: throw new DocletAbortException();
107: }
108: }
109:
110: /**
111: * Print the class use list.
112: */
113: protected void generatePackageUseFile() throws IOException {
114: printPackageUseHeader();
115:
116: if (usingPackageToUsedClasses.isEmpty()) {
117: printText("doclet.ClassUse_No.usage.of.0", pkgdoc.name());
118: p();
119: } else {
120: generatePackageUse();
121: }
122:
123: printPackageUseFooter();
124: }
125:
126: /**
127: * Print the class use list.
128: */
129: protected void generatePackageUse() throws IOException {
130: if (configuration.packages.length > 1) {
131: generatePackageList();
132: }
133: generateClassList();
134: }
135:
136: protected void generatePackageList() throws IOException {
137: tableIndexSummary();
138: tableHeaderStart("#CCCCFF");
139: printText("doclet.ClassUse_Packages.that.use.0",
140: getPackageLink(pkgdoc, Util.getPackageName(pkgdoc),
141: false));
142: tableHeaderEnd();
143: Iterator it = usingPackageToUsedClasses.keySet().iterator();
144: while (it.hasNext()) {
145: PackageDoc pkg = configuration.root
146: .packageNamed((String) it.next());
147: generatePackageUse(pkg);
148: }
149: tableEnd();
150: space();
151: p();
152: }
153:
154: protected void generateClassList() throws IOException {
155: Iterator itp = usingPackageToUsedClasses.keySet().iterator();
156: while (itp.hasNext()) {
157: String packageName = (String) itp.next();
158: PackageDoc usingPackage = configuration.root
159: .packageNamed(packageName);
160: if (usingPackage != null) {
161: anchor(usingPackage.name());
162: }
163: tableIndexSummary();
164: tableHeaderStart("#CCCCFF");
165: printText("doclet.ClassUse_Classes.in.0.used.by.1",
166: getPackageLink(pkgdoc, Util.getPackageName(pkgdoc),
167: false), getPackageLink(usingPackage, Util
168: .getPackageName(usingPackage), false));
169: tableHeaderEnd();
170: Iterator itc = ((Collection) usingPackageToUsedClasses
171: .get(packageName)).iterator();
172: while (itc.hasNext()) {
173: printClassRow((ClassDoc) itc.next(), packageName);
174: }
175: tableEnd();
176: space();
177: p();
178: }
179: }
180:
181: protected void printClassRow(ClassDoc usedClass, String packageName) {
182: String path = pathString(usedClass, "class-use/"
183: + usedClass.name() + ".html");
184:
185: trBgcolorStyle("white", "TableRowColor");
186: summaryRow(0);
187: bold();
188: printHyperLink(path, packageName, usedClass.name(), true);
189: boldEnd();
190: println();
191: br();
192: printNbsps();
193: printIndexComment(usedClass);
194: summaryRowEnd();
195: trEnd();
196: }
197:
198: /**
199: * Print the package use list.
200: */
201: protected void generatePackageUse(PackageDoc pkg)
202: throws IOException {
203: trBgcolorStyle("white", "TableRowColor");
204: summaryRow(0);
205: //Just want an anchor here.
206: printHyperLink("", pkg.name(), Util.getPackageName(pkg), true);
207: summaryRowEnd();
208: summaryRow(0);
209: if (pkg != null) {
210: printSummaryComment(pkg);
211: }
212: space();
213: summaryRowEnd();
214: trEnd();
215: }
216:
217: /**
218: * Print the header for the class use Listing.
219: */
220: protected void printPackageUseHeader() {
221: String packageLabel = configuration.getText("doclet.Package");
222: String name = pkgdoc.name();
223: printHtmlHeader(configuration.getText(
224: "doclet.Window_ClassUse_Header", packageLabel, name),
225: null, true);
226: printTop();
227: navLinks(true);
228: hr();
229: center();
230: h2();
231: boldText("doclet.ClassUse_Title", packageLabel, name);
232: h2End();
233: centerEnd();
234: }
235:
236: /**
237: * Print the footer for the class use Listing.
238: */
239: protected void printPackageUseFooter() {
240: hr();
241: navLinks(false);
242: printBottom();
243: printBodyHtmlEnd();
244: }
245:
246: /**
247: * Print this package link
248: */
249: protected void navLinkPackage() {
250: navCellStart();
251: printHyperLink("package-summary.html", "", configuration
252: .getText("doclet.Package"), true, "NavBarFont1");
253: navCellEnd();
254: }
255:
256: /**
257: * Print class use link
258: */
259: protected void navLinkClassUse() {
260: navCellRevStart();
261: fontStyle("NavBarFont1Rev");
262: boldText("doclet.navClassUse");
263: fontEnd();
264: navCellEnd();
265: }
266:
267: protected void navLinkTree() {
268: navCellStart();
269: printHyperLink("package-tree.html", "", configuration
270: .getText("doclet.Tree"), true, "NavBarFont1");
271: navCellEnd();
272: }
273:
274: }
|