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  */
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  * 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 {
50 
51     /**
52      * The configuration used in this run of the standard doclet.
53      */
54     ConfigurationImpl configuration;
55 
56     /**
57      * The current class being documented.
58      */
59     private ClassDoc currentClassDoc;
60 
61     private final String constantsTableSummary;
62 
63     private final String[] constantsTableHeader;
64 
65     /**
66      * Construct a ConstantsSummaryWriter.
67      * @param configuration the configuration used in this run
68      *        of the standard doclet.
69      */
ConstantsSummaryWriterImpl(ConfigurationImpl configuration)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     }
82 
83     /**
84      * {@inheritDoc}
85      */
getHeader()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     }
93 
94     /**
95      * {@inheritDoc}
96      */
getContentsHeader()97     public Content getContentsHeader() {
98         return new HtmlTree(HtmlTag.UL);
99     }
100 
101     /**
102      * {@inheritDoc}
103      */
addLinkToPackageContent(PackageDoc pkg, String parsedPackageName, Set<String> printedPackageHeaders, Content contentListTree)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(getDocLink(
111                     SectionName.UNNAMED_PACKAGE_ANCHOR),
112                     defaultPackageLabel, "", "");
113         } else {
114             Content packageNameContent = getPackageLabel(parsedPackageName);
115             packageNameContent.addContent(".*");
116             link = getHyperLink(DocLink.fragment(parsedPackageName),
117                     packageNameContent, "", "");
118             printedPackageHeaders.add(parsedPackageName);
119         }
120         contentListTree.addContent(HtmlTree.LI(link));
121     }
122 
123     /**
124      * {@inheritDoc}
125      */
getContentsList(Content contentListTree)126     public Content getContentsList(Content contentListTree) {
127         Content titleContent = getResource(
128                 "doclet.Constants_Summary");
129         Content pHeading = HtmlTree.HEADING(HtmlConstants.TITLE_HEADING, true,
130                 HtmlStyle.title, titleContent);
131         Content div = HtmlTree.DIV(HtmlStyle.header, pHeading);
132         Content headingContent = getResource(
133                 "doclet.Contents");
134         div.addContent(HtmlTree.HEADING(HtmlConstants.CONTENT_HEADING, true,
135                 headingContent));
136         div.addContent(contentListTree);
137         return div;
138     }
139 
140     /**
141      * {@inheritDoc}
142      */
getConstantSummaries()143     public Content getConstantSummaries() {
144         HtmlTree summariesDiv = new HtmlTree(HtmlTag.DIV);
145         summariesDiv.addStyle(HtmlStyle.constantValuesContainer);
146         return summariesDiv;
147     }
148 
149     /**
150      * {@inheritDoc}
151      */
addPackageName(PackageDoc pkg, String parsedPackageName, Content summariesTree)152     public void addPackageName(PackageDoc pkg, String parsedPackageName,
153             Content summariesTree) {
154         Content pkgNameContent;
155         if (parsedPackageName.length() == 0) {
156             summariesTree.addContent(getMarkerAnchor(
157                     SectionName.UNNAMED_PACKAGE_ANCHOR));
158             pkgNameContent = defaultPackageLabel;
159         } else {
160             summariesTree.addContent(getMarkerAnchor(
161                     parsedPackageName));
162             pkgNameContent = getPackageLabel(parsedPackageName);
163         }
164         Content headingContent = new StringContent(".*");
165         Content heading = HtmlTree.HEADING(HtmlConstants.PACKAGE_HEADING, true,
166                 pkgNameContent);
167         heading.addContent(headingContent);
168         summariesTree.addContent(heading);
169     }
170 
171     /**
172      * {@inheritDoc}
173      */
getClassConstantHeader()174     public Content getClassConstantHeader() {
175         HtmlTree ul = new HtmlTree(HtmlTag.UL);
176         ul.addStyle(HtmlStyle.blockList);
177         return ul;
178     }
179 
180     /**
181      * Get the table caption and header for the constant summary table
182      *
183      * @param cd classdoc to be documented
184      * @return constant members header content
185      */
getConstantMembersHeader(ClassDoc cd)186     public Content getConstantMembersHeader(ClassDoc cd) {
187         //generate links backward only to public classes.
188         Content classlink = (cd.isPublic() || cd.isProtected()) ?
189             getLink(new LinkInfoImpl(configuration,
190                     LinkInfoImpl.Kind.CONSTANT_SUMMARY, cd)) :
191             new StringContent(cd.qualifiedName());
192         String name = cd.containingPackage().name();
193         if (name.length() > 0) {
194             Content cb = new ContentBuilder();
195             cb.addContent(name);
196             cb.addContent(".");
197             cb.addContent(classlink);
198             return getClassName(cb);
199         } else {
200             return getClassName(classlink);
201         }
202     }
203 
204     /**
205      * Get the class name in the table caption and the table header.
206      *
207      * @param classStr the class name to print.
208      * @return the table caption and header
209      */
getClassName(Content classStr)210     protected Content getClassName(Content classStr) {
211         Content table = HtmlTree.TABLE(HtmlStyle.constantsSummary, 0, 3, 0, constantsTableSummary,
212                 getTableCaption(classStr));
213         table.addContent(getSummaryTableHeader(constantsTableHeader, "col"));
214         return table;
215     }
216 
217     /**
218      * {@inheritDoc}
219      */
addConstantMembers(ClassDoc cd, List<FieldDoc> fields, Content classConstantTree)220     public void addConstantMembers(ClassDoc cd, List<FieldDoc> fields,
221             Content classConstantTree) {
222         currentClassDoc = cd;
223         Content tbody = new HtmlTree(HtmlTag.TBODY);
224         for (int i = 0; i < fields.size(); ++i) {
225             HtmlTree tr = new HtmlTree(HtmlTag.TR);
226             if (i%2 == 0)
227                 tr.addStyle(HtmlStyle.altColor);
228             else
229                 tr.addStyle(HtmlStyle.rowColor);
230             addConstantMember(fields.get(i), tr);
231             tbody.addContent(tr);
232         }
233         Content table = getConstantMembersHeader(cd);
234         table.addContent(tbody);
235         Content li = HtmlTree.LI(HtmlStyle.blockList, table);
236         classConstantTree.addContent(li);
237     }
238 
239     /**
240      * Add the row for the constant summary table.
241      *
242      * @param member the field to be documented.
243      * @param trTree an htmltree object for the table row
244      */
addConstantMember(FieldDoc member, HtmlTree trTree)245     private void addConstantMember(FieldDoc member, HtmlTree trTree) {
246         trTree.addContent(getTypeColumn(member));
247         trTree.addContent(getNameColumn(member));
248         trTree.addContent(getValue(member));
249     }
250 
251     /**
252      * Get the type column for the constant summary table row.
253      *
254      * @param member the field to be documented.
255      * @return the type column of the constant table row
256      */
getTypeColumn(FieldDoc member)257     private Content getTypeColumn(FieldDoc member) {
258         Content anchor = getMarkerAnchor(currentClassDoc.qualifiedName() +
259                 "." + member.name());
260         Content tdType = HtmlTree.TD(HtmlStyle.colFirst, anchor);
261         Content code = new HtmlTree(HtmlTag.CODE);
262         StringTokenizer mods = new StringTokenizer(member.modifiers());
263         while(mods.hasMoreTokens()) {
264             Content modifier = new StringContent(mods.nextToken());
265             code.addContent(modifier);
266             code.addContent(getSpace());
267         }
268         Content type = getLink(new LinkInfoImpl(configuration,
269                 LinkInfoImpl.Kind.CONSTANT_SUMMARY, member.type()));
270         code.addContent(type);
271         tdType.addContent(code);
272         return tdType;
273     }
274 
275     /**
276      * Get the name column for the constant summary table row.
277      *
278      * @param member the field to be documented.
279      * @return the name column of the constant table row
280      */
getNameColumn(FieldDoc member)281     private Content getNameColumn(FieldDoc member) {
282         Content nameContent = getDocLink(
283                 LinkInfoImpl.Kind.CONSTANT_SUMMARY, member, member.name(), false);
284         Content code = HtmlTree.CODE(nameContent);
285         return HtmlTree.TD(code);
286     }
287 
288     /**
289      * Get the value column for the constant summary table row.
290      *
291      * @param member the field to be documented.
292      * @return the value column of the constant table row
293      */
getValue(FieldDoc member)294     private Content getValue(FieldDoc member) {
295         Content valueContent = new StringContent(member.constantValueExpression());
296         Content code = HtmlTree.CODE(valueContent);
297         return HtmlTree.TD(HtmlStyle.colLast, code);
298     }
299 
300     /**
301      * {@inheritDoc}
302      */
addFooter(Content contentTree)303     public void addFooter(Content contentTree) {
304         addNavLinks(false, contentTree);
305         addBottom(contentTree);
306     }
307 
308     /**
309      * {@inheritDoc}
310      */
printDocument(Content contentTree)311     public void printDocument(Content contentTree) throws IOException {
312         printHtmlDocument(null, true, contentTree);
313     }
314 }
315