duke@1: /* ohair@798: * Copyright (c) 1998, 2010, 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.formats.html; duke@1: duke@1: import java.io.*; duke@1: import java.util.*; bpatel@766: import com.sun.javadoc.*; bpatel@766: import com.sun.tools.doclets.internal.toolkit.util.*; bpatel@766: import com.sun.tools.doclets.formats.html.markup.*; bpatel@766: import com.sun.tools.doclets.internal.toolkit.*; duke@1: duke@1: /** duke@1: * Generate package usage information. duke@1: * duke@1: * @author Robert G. Field bpatel@243: * @author Bhavesh Patel (Modified) duke@1: */ duke@1: public class PackageUseWriter extends SubWriterHolderWriter { duke@1: duke@1: final PackageDoc pkgdoc; jjg@74: final SortedMap> usingPackageToUsedClasses = new TreeMap>(); duke@1: duke@1: /** duke@1: * Constructor. duke@1: * duke@1: * @param filename the file to be generated. duke@1: * @throws IOException duke@1: * @throws DocletAbortException duke@1: */ duke@1: public PackageUseWriter(ConfigurationImpl configuration, duke@1: ClassUseMapper mapper, String filename, duke@1: PackageDoc pkgdoc) throws IOException { duke@1: super(configuration, DirectoryManager.getDirectoryPath(pkgdoc), duke@1: filename, duke@1: DirectoryManager.getRelativePath(pkgdoc.name())); duke@1: this.pkgdoc = pkgdoc; duke@1: duke@1: // by examining all classes in this package, find what packages duke@1: // use these classes - produce a map between using package and duke@1: // used classes. duke@1: ClassDoc[] content = pkgdoc.allClasses(); duke@1: for (int i = 0; i < content.length; ++i) { duke@1: ClassDoc usedClass = content[i]; jjg@74: Set usingClasses = mapper.classToClass.get(usedClass.qualifiedName()); duke@1: if (usingClasses != null) { mcimadamore@184: for (Iterator it = usingClasses.iterator(); it.hasNext(); ) { mcimadamore@184: ClassDoc usingClass = it.next(); duke@1: PackageDoc usingPackage = usingClass.containingPackage(); jjg@74: Set usedClasses = usingPackageToUsedClasses duke@1: .get(usingPackage.name()); duke@1: if (usedClasses == null) { jjg@74: usedClasses = new TreeSet(); duke@1: usingPackageToUsedClasses.put(Util.getPackageName(usingPackage), duke@1: usedClasses); duke@1: } duke@1: usedClasses.add(usedClass); duke@1: } duke@1: } duke@1: } duke@1: } duke@1: duke@1: /** duke@1: * Generate a class page. duke@1: * duke@1: * @param configuration the current configuration of the doclet. duke@1: * @param mapper the mapping of the class usage. duke@1: * @param pkgdoc the package doc being documented. duke@1: */ duke@1: public static void generate(ConfigurationImpl configuration, duke@1: ClassUseMapper mapper, PackageDoc pkgdoc) { duke@1: PackageUseWriter pkgusegen; duke@1: String filename = "package-use.html"; duke@1: try { duke@1: pkgusegen = new PackageUseWriter(configuration, duke@1: mapper, filename, pkgdoc); duke@1: pkgusegen.generatePackageUseFile(); duke@1: pkgusegen.close(); duke@1: } catch (IOException exc) { duke@1: configuration.standardmessage.error( duke@1: "doclet.exception_encountered", duke@1: exc.toString(), filename); duke@1: throw new DocletAbortException(); duke@1: } duke@1: } duke@1: duke@1: duke@1: /** bpatel@766: * Generate the package use list. duke@1: */ duke@1: protected void generatePackageUseFile() throws IOException { bpatel@766: Content body = getPackageUseHeader(); bpatel@766: HtmlTree div = new HtmlTree(HtmlTag.DIV); bpatel@766: div.addStyle(HtmlStyle.contentContainer); duke@1: if (usingPackageToUsedClasses.isEmpty()) { bpatel@766: div.addContent(getResource( bpatel@766: "doclet.ClassUse_No.usage.of.0", pkgdoc.name())); duke@1: } else { bpatel@766: addPackageUse(div); duke@1: } bpatel@766: body.addContent(div); bpatel@766: addNavLinks(false, body); bpatel@766: addBottom(body); bpatel@766: printHtmlDocument(null, true, body); duke@1: } duke@1: duke@1: /** bpatel@766: * Add the package use information. bpatel@766: * bpatel@766: * @param contentTree the content tree to which the package use information will be added duke@1: */ bpatel@766: protected void addPackageUse(Content contentTree) throws IOException { bpatel@766: HtmlTree ul = new HtmlTree(HtmlTag.UL); bpatel@766: ul.addStyle(HtmlStyle.blockList); duke@1: if (configuration.packages.length > 1) { bpatel@766: addPackageList(ul); duke@1: } bpatel@766: addClassList(ul); bpatel@766: contentTree.addContent(ul); duke@1: } duke@1: bpatel@766: /** bpatel@766: * Add the list of packages that use the given package. bpatel@766: * bpatel@766: * @param contentTree the content tree to which the package list will be added bpatel@766: */ bpatel@766: protected void addPackageList(Content contentTree) throws IOException { bpatel@766: Content table = HtmlTree.TABLE(0, 3, 0, useTableSummary, bpatel@766: getTableCaption(configuration().getText( bpatel@766: "doclet.ClassUse_Packages.that.use.0", bpatel@766: getPackageLinkString(pkgdoc, Util.getPackageName(pkgdoc), false)))); bpatel@766: table.addContent(getSummaryTableHeader(packageTableHeader, "col")); bpatel@766: Content tbody = new HtmlTree(HtmlTag.TBODY); mcimadamore@184: Iterator it = usingPackageToUsedClasses.keySet().iterator(); bpatel@766: for (int i = 0; it.hasNext(); i++) { mcimadamore@184: PackageDoc pkg = configuration.root.packageNamed(it.next()); bpatel@766: HtmlTree tr = new HtmlTree(HtmlTag.TR); bpatel@766: if (i % 2 == 0) { bpatel@766: tr.addStyle(HtmlStyle.altColor); bpatel@766: } else { bpatel@766: tr.addStyle(HtmlStyle.rowColor); bpatel@766: } bpatel@766: addPackageUse(pkg, tr); bpatel@766: tbody.addContent(tr); duke@1: } bpatel@766: table.addContent(tbody); bpatel@766: Content li = HtmlTree.LI(HtmlStyle.blockList, table); bpatel@766: contentTree.addContent(li); duke@1: } duke@1: bpatel@766: /** bpatel@766: * Add the list of classes that use the given package. bpatel@766: * bpatel@766: * @param contentTree the content tree to which the class list will be added bpatel@766: */ bpatel@766: protected void addClassList(Content contentTree) throws IOException { bpatel@243: String[] classTableHeader = new String[] { bpatel@243: configuration.getText("doclet.0_and_1", bpatel@243: configuration.getText("doclet.Class"), bpatel@243: configuration.getText("doclet.Description")) bpatel@243: }; mcimadamore@184: Iterator itp = usingPackageToUsedClasses.keySet().iterator(); duke@1: while (itp.hasNext()) { mcimadamore@184: String packageName = itp.next(); duke@1: PackageDoc usingPackage = configuration.root.packageNamed(packageName); bpatel@766: HtmlTree li = new HtmlTree(HtmlTag.LI); bpatel@766: li.addStyle(HtmlStyle.blockList); duke@1: if (usingPackage != null) { bpatel@766: li.addContent(getMarkerAnchor(usingPackage.name())); duke@1: } bpatel@766: String tableSummary = configuration.getText("doclet.Use_Table_Summary", bpatel@766: configuration.getText("doclet.classes")); bpatel@766: Content table = HtmlTree.TABLE(0, 3, 0, tableSummary, bpatel@766: getTableCaption(configuration().getText( bpatel@766: "doclet.ClassUse_Classes.in.0.used.by.1", bpatel@766: getPackageLinkString(pkgdoc, Util.getPackageName(pkgdoc), false), bpatel@766: getPackageLinkString(usingPackage,Util.getPackageName(usingPackage), false)))); bpatel@766: table.addContent(getSummaryTableHeader(classTableHeader, "col")); bpatel@766: Content tbody = new HtmlTree(HtmlTag.TBODY); mcimadamore@184: Iterator itc = mcimadamore@184: usingPackageToUsedClasses.get(packageName).iterator(); bpatel@766: for (int i = 0; itc.hasNext(); i++) { bpatel@766: HtmlTree tr = new HtmlTree(HtmlTag.TR); bpatel@766: if (i % 2 == 0) { bpatel@766: tr.addStyle(HtmlStyle.altColor); bpatel@766: } else { bpatel@766: tr.addStyle(HtmlStyle.rowColor); bpatel@766: } bpatel@766: addClassRow(itc.next(), packageName, tr); bpatel@766: tbody.addContent(tr); duke@1: } bpatel@766: table.addContent(tbody); bpatel@766: li.addContent(table); bpatel@766: contentTree.addContent(li); duke@1: } duke@1: } duke@1: bpatel@766: /** bpatel@766: * Add a row for the class that uses the given package. bpatel@766: * bpatel@766: * @param usedClass the class that uses the given package bpatel@766: * @param packageName the name of the package to which the class belongs bpatel@766: * @param contentTree the content tree to which the row will be added bpatel@766: */ bpatel@766: protected void addClassRow(ClassDoc usedClass, String packageName, bpatel@766: Content contentTree) { duke@1: String path = pathString(usedClass, bpatel@766: "class-use/" + usedClass.name() + ".html"); bpatel@766: Content td = HtmlTree.TD(HtmlStyle.colOne, bpatel@766: getHyperLink(path, packageName, new StringContent(usedClass.name()))); bpatel@766: addIndexComment(usedClass, td); bpatel@766: contentTree.addContent(td); duke@1: } duke@1: duke@1: /** bpatel@766: * Add the package use information. bpatel@766: * bpatel@766: * @param pkg the package that used the given package bpatel@766: * @param contentTree the content tree to which the information will be added duke@1: */ bpatel@766: protected void addPackageUse(PackageDoc pkg, Content contentTree) throws IOException { bpatel@766: Content tdFirst = HtmlTree.TD(HtmlStyle.colFirst, bpatel@766: getHyperLink("", pkg.name(), new StringContent(Util.getPackageName(pkg)))); bpatel@766: contentTree.addContent(tdFirst); bpatel@766: HtmlTree tdLast = new HtmlTree(HtmlTag.TD); bpatel@766: tdLast.addStyle(HtmlStyle.colLast); bpatel@766: if (pkg != null) bpatel@766: addSummaryComment(pkg, tdLast); bpatel@766: else bpatel@766: tdLast.addContent(getSpace()); bpatel@766: contentTree.addContent(tdLast); duke@1: } duke@1: duke@1: /** bpatel@766: * Get the header for the package use listing. bpatel@766: * bpatel@766: * @return a content tree representing the package use header duke@1: */ bpatel@766: protected Content getPackageUseHeader() { bpatel@766: String packageText = configuration.getText("doclet.Package"); duke@1: String name = pkgdoc.name(); bpatel@766: String title = configuration.getText("doclet.Window_ClassUse_Header", bpatel@766: packageText, name); bpatel@766: Content bodyTree = getBody(true, getWindowTitle(title)); bpatel@766: addTop(bodyTree); bpatel@766: addNavLinks(true, bodyTree); bpatel@766: Content headContent = getResource("doclet.ClassUse_Title", packageText, name); bpatel@766: Content heading = HtmlTree.HEADING(HtmlConstants.TITLE_HEADING, true, bpatel@766: HtmlStyle.title, headContent); bpatel@766: Content div = HtmlTree.DIV(HtmlStyle.header, heading); bpatel@766: bodyTree.addContent(div); bpatel@766: return bodyTree; duke@1: } duke@1: duke@1: /** bpatel@766: * Get this package link. bpatel@766: * bpatel@766: * @return a content tree for the package link duke@1: */ bpatel@766: protected Content getNavLinkPackage() { bpatel@766: Content linkContent = getHyperLink("package-summary.html", "", bpatel@766: packageLabel); bpatel@766: Content li = HtmlTree.LI(linkContent); bpatel@766: return li; duke@1: } duke@1: duke@1: /** bpatel@766: * Get the use link. bpatel@766: * bpatel@766: * @return a content tree for the use link duke@1: */ bpatel@766: protected Content getNavLinkClassUse() { bpatel@766: Content li = HtmlTree.LI(HtmlStyle.navBarCell1Rev, useLabel); bpatel@766: return li; duke@1: } duke@1: bpatel@766: /** bpatel@766: * Get the tree link. bpatel@766: * bpatel@766: * @return a content tree for the tree link bpatel@766: */ bpatel@766: protected Content getNavLinkTree() { bpatel@766: Content linkContent = getHyperLink("package-tree.html", "", bpatel@766: treeLabel); bpatel@766: Content li = HtmlTree.LI(linkContent); bpatel@766: return li; duke@1: } duke@1: }