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