Tue, 14 May 2013 10:14:54 -0700
8012175: Convert TagletOutputImpl to use ContentBuilder instead of StringBuilder
Reviewed-by: darcy
1 /*
2 * Copyright (c) 2001, 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 */
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 * Write the Constants Summary Page in HTML format.
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 Jamie Ho
45 * @author Bhavesh Patel (Modified)
46 * @since 1.4
47 */
48 public class ConstantsSummaryWriterImpl extends HtmlDocletWriter
49 implements ConstantsSummaryWriter {
51 /**
52 * The configuration used in this run of the standard doclet.
53 */
54 ConfigurationImpl configuration;
56 /**
57 * The current class being documented.
58 */
59 private ClassDoc currentClassDoc;
61 private final String constantsTableSummary;
63 private final String[] constantsTableHeader;
65 /**
66 * Construct a ConstantsSummaryWriter.
67 * @param configuration the configuration used in this run
68 * of the standard doclet.
69 */
70 public ConstantsSummaryWriterImpl(ConfigurationImpl configuration)
71 throws IOException {
72 super(configuration, DocPaths.CONSTANT_VALUES);
73 this.configuration = configuration;
74 constantsTableSummary = configuration.getText("doclet.Constants_Table_Summary",
75 configuration.getText("doclet.Constants_Summary"));
76 constantsTableHeader = new String[] {
77 getModifierTypeHeader(),
78 configuration.getText("doclet.ConstantField"),
79 configuration.getText("doclet.Value")
80 };
81 }
83 /**
84 * {@inheritDoc}
85 */
86 public Content getHeader() {
87 String label = configuration.getText("doclet.Constants_Summary");
88 Content bodyTree = getBody(true, getWindowTitle(label));
89 addTop(bodyTree);
90 addNavLinks(true, bodyTree);
91 return bodyTree;
92 }
94 /**
95 * {@inheritDoc}
96 */
97 public Content getContentsHeader() {
98 return new HtmlTree(HtmlTag.UL);
99 }
101 /**
102 * {@inheritDoc}
103 */
104 public void addLinkToPackageContent(PackageDoc pkg, String parsedPackageName,
105 Set<String> printedPackageHeaders, Content contentListTree) {
106 String packageName = pkg.name();
107 //add link to summary
108 Content link;
109 if (packageName.length() == 0) {
110 link = getHyperLink(DocLink.fragment(DocletConstants.UNNAMED_PACKAGE_ANCHOR),
111 defaultPackageLabel, "", "");
112 } else {
113 Content packageNameContent = getPackageLabel(parsedPackageName);
114 packageNameContent.addContent(".*");
115 link = getHyperLink(DocLink.fragment(parsedPackageName),
116 packageNameContent, "", "");
117 printedPackageHeaders.add(parsedPackageName);
118 }
119 contentListTree.addContent(HtmlTree.LI(link));
120 }
122 /**
123 * {@inheritDoc}
124 */
125 public Content getContentsList(Content contentListTree) {
126 Content titleContent = getResource(
127 "doclet.Constants_Summary");
128 Content pHeading = HtmlTree.HEADING(HtmlConstants.TITLE_HEADING, true,
129 HtmlStyle.title, titleContent);
130 Content div = HtmlTree.DIV(HtmlStyle.header, pHeading);
131 Content headingContent = getResource(
132 "doclet.Contents");
133 div.addContent(HtmlTree.HEADING(HtmlConstants.CONTENT_HEADING, true,
134 headingContent));
135 div.addContent(contentListTree);
136 return div;
137 }
139 /**
140 * {@inheritDoc}
141 */
142 public Content getConstantSummaries() {
143 HtmlTree summariesDiv = new HtmlTree(HtmlTag.DIV);
144 summariesDiv.addStyle(HtmlStyle.constantValuesContainer);
145 return summariesDiv;
146 }
148 /**
149 * {@inheritDoc}
150 */
151 public void addPackageName(PackageDoc pkg, String parsedPackageName,
152 Content summariesTree) {
153 Content pkgNameContent;
154 if (parsedPackageName.length() == 0) {
155 summariesTree.addContent(getMarkerAnchor(
156 DocletConstants.UNNAMED_PACKAGE_ANCHOR));
157 pkgNameContent = defaultPackageLabel;
158 } else {
159 summariesTree.addContent(getMarkerAnchor(
160 parsedPackageName));
161 pkgNameContent = getPackageLabel(parsedPackageName);
162 }
163 Content headingContent = new StringContent(".*");
164 Content heading = HtmlTree.HEADING(HtmlConstants.PACKAGE_HEADING, true,
165 pkgNameContent);
166 heading.addContent(headingContent);
167 summariesTree.addContent(heading);
168 }
170 /**
171 * {@inheritDoc}
172 */
173 public Content getClassConstantHeader() {
174 HtmlTree ul = new HtmlTree(HtmlTag.UL);
175 ul.addStyle(HtmlStyle.blockList);
176 return ul;
177 }
179 /**
180 * Get the table caption and header for the constant summary table
181 *
182 * @param cd classdoc to be documented
183 * @return constant members header content
184 */
185 public Content getConstantMembersHeader(ClassDoc cd) {
186 //generate links backward only to public classes.
187 String classlink = (cd.isPublic() || cd.isProtected()) ?
188 getLink(new LinkInfoImpl(configuration,
189 LinkInfoImpl.Kind.CONSTANT_SUMMARY, cd)).toString() :
190 cd.qualifiedName();
191 String name = cd.containingPackage().name();
192 if (name.length() > 0) {
193 return getClassName(name + "." + classlink);
194 } else {
195 return getClassName(classlink);
196 }
197 }
199 /**
200 * Get the class name in the table caption and the table header.
201 *
202 * @param classStr the class name to print.
203 * @return the table caption and header
204 */
205 protected Content getClassName(String classStr) {
206 Content table = HtmlTree.TABLE(0, 3, 0, constantsTableSummary,
207 getTableCaption(classStr));
208 table.addContent(getSummaryTableHeader(constantsTableHeader, "col"));
209 return table;
210 }
212 /**
213 * {@inheritDoc}
214 */
215 public void addConstantMembers(ClassDoc cd, List<FieldDoc> fields,
216 Content classConstantTree) {
217 currentClassDoc = cd;
218 Content tbody = new HtmlTree(HtmlTag.TBODY);
219 for (int i = 0; i < fields.size(); ++i) {
220 HtmlTree tr = new HtmlTree(HtmlTag.TR);
221 if (i%2 == 0)
222 tr.addStyle(HtmlStyle.altColor);
223 else
224 tr.addStyle(HtmlStyle.rowColor);
225 addConstantMember(fields.get(i), tr);
226 tbody.addContent(tr);
227 }
228 Content table = getConstantMembersHeader(cd);
229 table.addContent(tbody);
230 Content li = HtmlTree.LI(HtmlStyle.blockList, table);
231 classConstantTree.addContent(li);
232 }
234 /**
235 * Add the row for the constant summary table.
236 *
237 * @param member the field to be documented.
238 * @param trTree an htmltree object for the table row
239 */
240 private void addConstantMember(FieldDoc member, HtmlTree trTree) {
241 trTree.addContent(getTypeColumn(member));
242 trTree.addContent(getNameColumn(member));
243 trTree.addContent(getValue(member));
244 }
246 /**
247 * Get the type column for the constant summary table row.
248 *
249 * @param member the field to be documented.
250 * @return the type column of the constant table row
251 */
252 private Content getTypeColumn(FieldDoc member) {
253 Content anchor = getMarkerAnchor(currentClassDoc.qualifiedName() +
254 "." + member.name());
255 Content tdType = HtmlTree.TD(HtmlStyle.colFirst, anchor);
256 Content code = new HtmlTree(HtmlTag.CODE);
257 StringTokenizer mods = new StringTokenizer(member.modifiers());
258 while(mods.hasMoreTokens()) {
259 Content modifier = new StringContent(mods.nextToken());
260 code.addContent(modifier);
261 code.addContent(getSpace());
262 }
263 Content type = getLink(new LinkInfoImpl(configuration,
264 LinkInfoImpl.Kind.CONSTANT_SUMMARY, member.type()));
265 code.addContent(type);
266 tdType.addContent(code);
267 return tdType;
268 }
270 /**
271 * Get the name column for the constant summary table row.
272 *
273 * @param member the field to be documented.
274 * @return the name column of the constant table row
275 */
276 private Content getNameColumn(FieldDoc member) {
277 Content nameContent = getDocLink(
278 LinkInfoImpl.Kind.CONSTANT_SUMMARY, member, member.name(), false);
279 Content code = HtmlTree.CODE(nameContent);
280 return HtmlTree.TD(code);
281 }
283 /**
284 * Get the value column for the constant summary table row.
285 *
286 * @param member the field to be documented.
287 * @return the value column of the constant table row
288 */
289 private Content getValue(FieldDoc member) {
290 Content valueContent = new StringContent(member.constantValueExpression());
291 Content code = HtmlTree.CODE(valueContent);
292 return HtmlTree.TD(HtmlStyle.colLast, code);
293 }
295 /**
296 * {@inheritDoc}
297 */
298 public void addFooter(Content contentTree) {
299 addNavLinks(false, contentTree);
300 addBottom(contentTree);
301 }
303 /**
304 * {@inheritDoc}
305 */
306 public void printDocument(Content contentTree) throws IOException {
307 printHtmlDocument(null, true, contentTree);
308 }
309 }