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