src/share/classes/com/sun/tools/doclets/formats/html/PackageUseWriter.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.doclets.formats.html;
aoqi@0 27
aoqi@0 28 import java.io.*;
aoqi@0 29 import java.util.*;
aoqi@0 30
aoqi@0 31 import com.sun.javadoc.*;
aoqi@0 32 import com.sun.tools.doclets.formats.html.markup.*;
aoqi@0 33 import com.sun.tools.doclets.internal.toolkit.*;
aoqi@0 34 import com.sun.tools.doclets.internal.toolkit.util.*;
aoqi@0 35
aoqi@0 36 /**
aoqi@0 37 * Generate package usage information.
aoqi@0 38 *
aoqi@0 39 * <p><b>This is NOT part of any supported API.
aoqi@0 40 * If you write code that depends on this, you do so at your own risk.
aoqi@0 41 * This code and its internal interfaces are subject to change or
aoqi@0 42 * deletion without notice.</b>
aoqi@0 43 *
aoqi@0 44 * @author Robert G. Field
aoqi@0 45 * @author Bhavesh Patel (Modified)
aoqi@0 46 */
aoqi@0 47 public class PackageUseWriter extends SubWriterHolderWriter {
aoqi@0 48
aoqi@0 49 final PackageDoc pkgdoc;
aoqi@0 50 final SortedMap<String,Set<ClassDoc>> usingPackageToUsedClasses = new TreeMap<String,Set<ClassDoc>>();
aoqi@0 51
aoqi@0 52 /**
aoqi@0 53 * Constructor.
aoqi@0 54 *
aoqi@0 55 * @param filename the file to be generated.
aoqi@0 56 * @throws IOException
aoqi@0 57 * @throws DocletAbortException
aoqi@0 58 */
aoqi@0 59 public PackageUseWriter(ConfigurationImpl configuration,
aoqi@0 60 ClassUseMapper mapper, DocPath filename,
aoqi@0 61 PackageDoc pkgdoc) throws IOException {
aoqi@0 62 super(configuration, DocPath.forPackage(pkgdoc).resolve(filename));
aoqi@0 63 this.pkgdoc = pkgdoc;
aoqi@0 64
aoqi@0 65 // by examining all classes in this package, find what packages
aoqi@0 66 // use these classes - produce a map between using package and
aoqi@0 67 // used classes.
aoqi@0 68 ClassDoc[] content = pkgdoc.allClasses();
aoqi@0 69 for (int i = 0; i < content.length; ++i) {
aoqi@0 70 ClassDoc usedClass = content[i];
aoqi@0 71 Set<ClassDoc> usingClasses = mapper.classToClass.get(usedClass.qualifiedName());
aoqi@0 72 if (usingClasses != null) {
aoqi@0 73 for (Iterator<ClassDoc> it = usingClasses.iterator(); it.hasNext(); ) {
aoqi@0 74 ClassDoc usingClass = it.next();
aoqi@0 75 PackageDoc usingPackage = usingClass.containingPackage();
aoqi@0 76 Set<ClassDoc> usedClasses = usingPackageToUsedClasses
aoqi@0 77 .get(usingPackage.name());
aoqi@0 78 if (usedClasses == null) {
aoqi@0 79 usedClasses = new TreeSet<ClassDoc>();
aoqi@0 80 usingPackageToUsedClasses.put(Util.getPackageName(usingPackage),
aoqi@0 81 usedClasses);
aoqi@0 82 }
aoqi@0 83 usedClasses.add(usedClass);
aoqi@0 84 }
aoqi@0 85 }
aoqi@0 86 }
aoqi@0 87 }
aoqi@0 88
aoqi@0 89 /**
aoqi@0 90 * Generate a class page.
aoqi@0 91 *
aoqi@0 92 * @param configuration the current configuration of the doclet.
aoqi@0 93 * @param mapper the mapping of the class usage.
aoqi@0 94 * @param pkgdoc the package doc being documented.
aoqi@0 95 */
aoqi@0 96 public static void generate(ConfigurationImpl configuration,
aoqi@0 97 ClassUseMapper mapper, PackageDoc pkgdoc) {
aoqi@0 98 PackageUseWriter pkgusegen;
aoqi@0 99 DocPath filename = DocPaths.PACKAGE_USE;
aoqi@0 100 try {
aoqi@0 101 pkgusegen = new PackageUseWriter(configuration,
aoqi@0 102 mapper, filename, pkgdoc);
aoqi@0 103 pkgusegen.generatePackageUseFile();
aoqi@0 104 pkgusegen.close();
aoqi@0 105 } catch (IOException exc) {
aoqi@0 106 configuration.standardmessage.error(
aoqi@0 107 "doclet.exception_encountered",
aoqi@0 108 exc.toString(), filename);
aoqi@0 109 throw new DocletAbortException(exc);
aoqi@0 110 }
aoqi@0 111 }
aoqi@0 112
aoqi@0 113
aoqi@0 114 /**
aoqi@0 115 * Generate the package use list.
aoqi@0 116 */
aoqi@0 117 protected void generatePackageUseFile() throws IOException {
aoqi@0 118 Content body = getPackageUseHeader();
aoqi@0 119 HtmlTree div = new HtmlTree(HtmlTag.DIV);
aoqi@0 120 div.addStyle(HtmlStyle.contentContainer);
aoqi@0 121 if (usingPackageToUsedClasses.isEmpty()) {
aoqi@0 122 div.addContent(getResource(
aoqi@0 123 "doclet.ClassUse_No.usage.of.0", pkgdoc.name()));
aoqi@0 124 } else {
aoqi@0 125 addPackageUse(div);
aoqi@0 126 }
aoqi@0 127 body.addContent(div);
aoqi@0 128 addNavLinks(false, body);
aoqi@0 129 addBottom(body);
aoqi@0 130 printHtmlDocument(null, true, body);
aoqi@0 131 }
aoqi@0 132
aoqi@0 133 /**
aoqi@0 134 * Add the package use information.
aoqi@0 135 *
aoqi@0 136 * @param contentTree the content tree to which the package use information will be added
aoqi@0 137 */
aoqi@0 138 protected void addPackageUse(Content contentTree) throws IOException {
aoqi@0 139 HtmlTree ul = new HtmlTree(HtmlTag.UL);
aoqi@0 140 ul.addStyle(HtmlStyle.blockList);
aoqi@0 141 if (configuration.packages.length > 1) {
aoqi@0 142 addPackageList(ul);
aoqi@0 143 }
aoqi@0 144 addClassList(ul);
aoqi@0 145 contentTree.addContent(ul);
aoqi@0 146 }
aoqi@0 147
aoqi@0 148 /**
aoqi@0 149 * Add the list of packages that use the given package.
aoqi@0 150 *
aoqi@0 151 * @param contentTree the content tree to which the package list will be added
aoqi@0 152 */
aoqi@0 153 protected void addPackageList(Content contentTree) throws IOException {
aoqi@0 154 Content table = HtmlTree.TABLE(HtmlStyle.useSummary, 0, 3, 0, useTableSummary,
aoqi@0 155 getTableCaption(configuration.getResource(
aoqi@0 156 "doclet.ClassUse_Packages.that.use.0",
aoqi@0 157 getPackageLink(pkgdoc, Util.getPackageName(pkgdoc)))));
aoqi@0 158 table.addContent(getSummaryTableHeader(packageTableHeader, "col"));
aoqi@0 159 Content tbody = new HtmlTree(HtmlTag.TBODY);
aoqi@0 160 Iterator<String> it = usingPackageToUsedClasses.keySet().iterator();
aoqi@0 161 for (int i = 0; it.hasNext(); i++) {
aoqi@0 162 PackageDoc pkg = configuration.root.packageNamed(it.next());
aoqi@0 163 HtmlTree tr = new HtmlTree(HtmlTag.TR);
aoqi@0 164 if (i % 2 == 0) {
aoqi@0 165 tr.addStyle(HtmlStyle.altColor);
aoqi@0 166 } else {
aoqi@0 167 tr.addStyle(HtmlStyle.rowColor);
aoqi@0 168 }
aoqi@0 169 addPackageUse(pkg, tr);
aoqi@0 170 tbody.addContent(tr);
aoqi@0 171 }
aoqi@0 172 table.addContent(tbody);
aoqi@0 173 Content li = HtmlTree.LI(HtmlStyle.blockList, table);
aoqi@0 174 contentTree.addContent(li);
aoqi@0 175 }
aoqi@0 176
aoqi@0 177 /**
aoqi@0 178 * Add the list of classes that use the given package.
aoqi@0 179 *
aoqi@0 180 * @param contentTree the content tree to which the class list will be added
aoqi@0 181 */
aoqi@0 182 protected void addClassList(Content contentTree) throws IOException {
aoqi@0 183 String[] classTableHeader = new String[] {
aoqi@0 184 configuration.getText("doclet.0_and_1",
aoqi@0 185 configuration.getText("doclet.Class"),
aoqi@0 186 configuration.getText("doclet.Description"))
aoqi@0 187 };
aoqi@0 188 Iterator<String> itp = usingPackageToUsedClasses.keySet().iterator();
aoqi@0 189 while (itp.hasNext()) {
aoqi@0 190 String packageName = itp.next();
aoqi@0 191 PackageDoc usingPackage = configuration.root.packageNamed(packageName);
aoqi@0 192 HtmlTree li = new HtmlTree(HtmlTag.LI);
aoqi@0 193 li.addStyle(HtmlStyle.blockList);
aoqi@0 194 if (usingPackage != null) {
aoqi@0 195 li.addContent(getMarkerAnchor(usingPackage.name()));
aoqi@0 196 }
aoqi@0 197 String tableSummary = configuration.getText("doclet.Use_Table_Summary",
aoqi@0 198 configuration.getText("doclet.classes"));
aoqi@0 199 Content table = HtmlTree.TABLE(HtmlStyle.useSummary, 0, 3, 0, tableSummary,
aoqi@0 200 getTableCaption(configuration.getResource(
aoqi@0 201 "doclet.ClassUse_Classes.in.0.used.by.1",
aoqi@0 202 getPackageLink(pkgdoc, Util.getPackageName(pkgdoc)),
aoqi@0 203 getPackageLink(usingPackage, Util.getPackageName(usingPackage)))));
aoqi@0 204 table.addContent(getSummaryTableHeader(classTableHeader, "col"));
aoqi@0 205 Content tbody = new HtmlTree(HtmlTag.TBODY);
aoqi@0 206 Iterator<ClassDoc> itc =
aoqi@0 207 usingPackageToUsedClasses.get(packageName).iterator();
aoqi@0 208 for (int i = 0; itc.hasNext(); i++) {
aoqi@0 209 HtmlTree tr = new HtmlTree(HtmlTag.TR);
aoqi@0 210 if (i % 2 == 0) {
aoqi@0 211 tr.addStyle(HtmlStyle.altColor);
aoqi@0 212 } else {
aoqi@0 213 tr.addStyle(HtmlStyle.rowColor);
aoqi@0 214 }
aoqi@0 215 addClassRow(itc.next(), packageName, tr);
aoqi@0 216 tbody.addContent(tr);
aoqi@0 217 }
aoqi@0 218 table.addContent(tbody);
aoqi@0 219 li.addContent(table);
aoqi@0 220 contentTree.addContent(li);
aoqi@0 221 }
aoqi@0 222 }
aoqi@0 223
aoqi@0 224 /**
aoqi@0 225 * Add a row for the class that uses the given package.
aoqi@0 226 *
aoqi@0 227 * @param usedClass the class that uses the given package
aoqi@0 228 * @param packageName the name of the package to which the class belongs
aoqi@0 229 * @param contentTree the content tree to which the row will be added
aoqi@0 230 */
aoqi@0 231 protected void addClassRow(ClassDoc usedClass, String packageName,
aoqi@0 232 Content contentTree) {
aoqi@0 233 DocPath dp = pathString(usedClass,
aoqi@0 234 DocPaths.CLASS_USE.resolve(DocPath.forName(usedClass)));
aoqi@0 235 Content td = HtmlTree.TD(HtmlStyle.colOne,
aoqi@0 236 getHyperLink(dp.fragment(packageName), new StringContent(usedClass.name())));
aoqi@0 237 addIndexComment(usedClass, td);
aoqi@0 238 contentTree.addContent(td);
aoqi@0 239 }
aoqi@0 240
aoqi@0 241 /**
aoqi@0 242 * Add the package use information.
aoqi@0 243 *
aoqi@0 244 * @param pkg the package that used the given package
aoqi@0 245 * @param contentTree the content tree to which the information will be added
aoqi@0 246 */
aoqi@0 247 protected void addPackageUse(PackageDoc pkg, Content contentTree) throws IOException {
aoqi@0 248 Content tdFirst = HtmlTree.TD(HtmlStyle.colFirst,
aoqi@0 249 getHyperLink(Util.getPackageName(pkg),
aoqi@0 250 new StringContent(Util.getPackageName(pkg))));
aoqi@0 251 contentTree.addContent(tdFirst);
aoqi@0 252 HtmlTree tdLast = new HtmlTree(HtmlTag.TD);
aoqi@0 253 tdLast.addStyle(HtmlStyle.colLast);
aoqi@0 254 if (pkg != null && pkg.name().length() != 0) {
aoqi@0 255 addSummaryComment(pkg, tdLast);
aoqi@0 256 } else {
aoqi@0 257 tdLast.addContent(getSpace());
aoqi@0 258 }
aoqi@0 259 contentTree.addContent(tdLast);
aoqi@0 260 }
aoqi@0 261
aoqi@0 262 /**
aoqi@0 263 * Get the header for the package use listing.
aoqi@0 264 *
aoqi@0 265 * @return a content tree representing the package use header
aoqi@0 266 */
aoqi@0 267 protected Content getPackageUseHeader() {
aoqi@0 268 String packageText = configuration.getText("doclet.Package");
aoqi@0 269 String name = pkgdoc.name();
aoqi@0 270 String title = configuration.getText("doclet.Window_ClassUse_Header",
aoqi@0 271 packageText, name);
aoqi@0 272 Content bodyTree = getBody(true, getWindowTitle(title));
aoqi@0 273 addTop(bodyTree);
aoqi@0 274 addNavLinks(true, bodyTree);
aoqi@0 275 ContentBuilder headContent = new ContentBuilder();
aoqi@0 276 headContent.addContent(getResource("doclet.ClassUse_Title", packageText));
aoqi@0 277 headContent.addContent(new HtmlTree(HtmlTag.BR));
aoqi@0 278 headContent.addContent(name);
aoqi@0 279 Content heading = HtmlTree.HEADING(HtmlConstants.TITLE_HEADING, true,
aoqi@0 280 HtmlStyle.title, headContent);
aoqi@0 281 Content div = HtmlTree.DIV(HtmlStyle.header, heading);
aoqi@0 282 bodyTree.addContent(div);
aoqi@0 283 return bodyTree;
aoqi@0 284 }
aoqi@0 285
aoqi@0 286 /**
aoqi@0 287 * Get this package link.
aoqi@0 288 *
aoqi@0 289 * @return a content tree for the package link
aoqi@0 290 */
aoqi@0 291 protected Content getNavLinkPackage() {
aoqi@0 292 Content linkContent = getHyperLink(DocPaths.PACKAGE_SUMMARY,
aoqi@0 293 packageLabel);
aoqi@0 294 Content li = HtmlTree.LI(linkContent);
aoqi@0 295 return li;
aoqi@0 296 }
aoqi@0 297
aoqi@0 298 /**
aoqi@0 299 * Get the use link.
aoqi@0 300 *
aoqi@0 301 * @return a content tree for the use link
aoqi@0 302 */
aoqi@0 303 protected Content getNavLinkClassUse() {
aoqi@0 304 Content li = HtmlTree.LI(HtmlStyle.navBarCell1Rev, useLabel);
aoqi@0 305 return li;
aoqi@0 306 }
aoqi@0 307
aoqi@0 308 /**
aoqi@0 309 * Get the tree link.
aoqi@0 310 *
aoqi@0 311 * @return a content tree for the tree link
aoqi@0 312 */
aoqi@0 313 protected Content getNavLinkTree() {
aoqi@0 314 Content linkContent = getHyperLink(DocPaths.PACKAGE_TREE,
aoqi@0 315 treeLabel);
aoqi@0 316 Content li = HtmlTree.LI(linkContent);
aoqi@0 317 return li;
aoqi@0 318 }
aoqi@0 319 }

mercurial