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