Wed, 31 Oct 2012 13:48:15 -0700
8001664: refactor javadoc to use abstraction to handle files
Reviewed-by: darcy
1 /*
2 * Copyright (c) 2003, 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.internal.toolkit.builders;
28 import java.io.*;
29 import java.util.*;
31 import com.sun.javadoc.*;
32 import com.sun.tools.doclets.internal.toolkit.*;
33 import com.sun.tools.doclets.internal.toolkit.util.*;
35 /**
36 * Builds the Constants Summary Page.
37 *
38 * <p><b>This is NOT part of any supported API.
39 * If you write code that depends on this, you do so at your own risk.
40 * This code and its internal interfaces are subject to change or
41 * deletion without notice.</b>
42 *
43 * @author Jamie Ho
44 * @author Bhavesh Patel (Modified)
45 * @since 1.5
46 */
47 public class ConstantsSummaryBuilder extends AbstractBuilder {
49 /**
50 * The root element of the constant summary XML is {@value}.
51 */
52 public static final String ROOT = "ConstantSummary";
54 /**
55 * The maximum number of package directories shown in the constant
56 * value index.
57 */
58 public static final int MAX_CONSTANT_VALUE_INDEX_LENGTH = 2;
60 /**
61 * The writer used to write the results.
62 */
63 protected ConstantsSummaryWriter writer;
65 /**
66 * The set of ClassDocs that have constant fields.
67 */
68 protected Set<ClassDoc> classDocsWithConstFields;
70 /**
71 * The set of printed package headers.
72 */
73 protected Set<String> printedPackageHeaders;
75 /**
76 * The current package being documented.
77 */
78 private PackageDoc currentPackage;
80 /**
81 * The current class being documented.
82 */
83 private ClassDoc currentClass;
85 /**
86 * The content tree for the constant summary documentation.
87 */
88 private Content contentTree;
90 /**
91 * Construct a new ConstantsSummaryBuilder.
92 *
93 * @param configuration the current configuration of the
94 * doclet.
95 */
96 private ConstantsSummaryBuilder(Configuration configuration) {
97 super(configuration);
98 }
100 /**
101 * Construct a ConstantsSummaryBuilder.
102 *
103 * @param configuration the configuration used in this run
104 * of the doclet.
105 * @param writer the writer for the summary.
106 */
107 public static ConstantsSummaryBuilder getInstance(
108 Configuration configuration, ConstantsSummaryWriter writer) {
109 ConstantsSummaryBuilder builder = new ConstantsSummaryBuilder(
110 configuration);
111 builder.writer = writer;
112 builder.classDocsWithConstFields = new HashSet<ClassDoc>();
113 return builder;
114 }
116 /**
117 * {@inheritDoc}
118 */
119 public void build() throws IOException {
120 if (writer == null) {
121 //Doclet does not support this output.
122 return;
123 }
124 build(LayoutParser.getInstance(configuration).parseXML(ROOT), contentTree);
125 }
127 /**
128 * {@inheritDoc}
129 */
130 public String getName() {
131 return ROOT;
132 }
134 /**
135 * Build the constant summary.
136 *
137 * @param node the XML element that specifies which components to document
138 * @param contentTree the content tree to which the documentation will be added
139 */
140 public void buildConstantSummary(XMLNode node, Content contentTree) throws Exception {
141 contentTree = writer.getHeader();
142 buildChildren(node, contentTree);
143 writer.addFooter(contentTree);
144 writer.printDocument(contentTree);
145 writer.close();
146 }
148 /**
149 * Build the list of packages.
150 *
151 * @param node the XML element that specifies which components to document
152 * @param contentTree the content tree to which the content list will be added
153 */
154 public void buildContents(XMLNode node, Content contentTree) {
155 Content contentListTree = writer.getContentsHeader();
156 PackageDoc[] packages = configuration.packages;
157 printedPackageHeaders = new HashSet<String>();
158 for (int i = 0; i < packages.length; i++) {
159 if (hasConstantField(packages[i]) && ! hasPrintedPackageIndex(packages[i].name())) {
160 writer.addLinkToPackageContent(packages[i],
161 parsePackageName(packages[i].name()),
162 printedPackageHeaders, contentListTree);
163 }
164 }
165 contentTree.addContent(writer.getContentsList(contentListTree));
166 }
168 /**
169 * Build the summary for each documented package.
170 *
171 * @param node the XML element that specifies which components to document
172 * @param contentTree the tree to which the summaries will be added
173 */
174 public void buildConstantSummaries(XMLNode node, Content contentTree) {
175 PackageDoc[] packages = configuration.packages;
176 printedPackageHeaders = new HashSet<String>();
177 Content summariesTree = writer.getConstantSummaries();
178 for (int i = 0; i < packages.length; i++) {
179 if (hasConstantField(packages[i])) {
180 currentPackage = packages[i];
181 //Build the documentation for the current package.
182 buildChildren(node, summariesTree);
183 }
184 }
185 contentTree.addContent(summariesTree);
186 }
188 /**
189 * Build the header for the given package.
190 *
191 * @param node the XML element that specifies which components to document
192 * @param summariesTree the tree to which the package header will be added
193 */
194 public void buildPackageHeader(XMLNode node, Content summariesTree) {
195 String parsedPackageName = parsePackageName(currentPackage.name());
196 if (! printedPackageHeaders.contains(parsedPackageName)) {
197 writer.addPackageName(currentPackage,
198 parsePackageName(currentPackage.name()), summariesTree);
199 printedPackageHeaders.add(parsedPackageName);
200 }
201 }
203 /**
204 * Build the summary for the current class.
205 *
206 * @param node the XML element that specifies which components to document
207 * @param summariesTree the tree to which the class constant summary will be added
208 */
209 public void buildClassConstantSummary(XMLNode node, Content summariesTree) {
210 ClassDoc[] classes = currentPackage.name().length() > 0 ?
211 currentPackage.allClasses() :
212 configuration.classDocCatalog.allClasses(
213 DocletConstants.DEFAULT_PACKAGE_NAME);
214 Arrays.sort(classes);
215 Content classConstantTree = writer.getClassConstantHeader();
216 for (int i = 0; i < classes.length; i++) {
217 if (! classDocsWithConstFields.contains(classes[i]) ||
218 ! classes[i].isIncluded()) {
219 continue;
220 }
221 currentClass = classes[i];
222 //Build the documentation for the current class.
223 buildChildren(node, classConstantTree);
224 }
225 summariesTree.addContent(classConstantTree);
226 }
228 /**
229 * Build the summary of constant members in the class.
230 *
231 * @param node the XML element that specifies which components to document
232 * @param classConstantTree the tree to which the constant members table
233 * will be added
234 */
235 public void buildConstantMembers(XMLNode node, Content classConstantTree) {
236 new ConstantFieldBuilder(currentClass).buildMembersSummary(node, classConstantTree);
237 }
239 /**
240 * Return true if the given package has constant fields to document.
241 *
242 * @param pkg the package being checked.
243 * @return true if the given package has constant fields to document.
244 */
245 private boolean hasConstantField(PackageDoc pkg) {
246 ClassDoc[] classes;
247 if (pkg.name().length() > 0) {
248 classes = pkg.allClasses();
249 } else {
250 classes = configuration.classDocCatalog.allClasses(
251 DocletConstants.DEFAULT_PACKAGE_NAME);
252 }
253 boolean found = false;
254 for (int j = 0; j < classes.length; j++){
255 if (classes[j].isIncluded() && hasConstantField(classes[j])) {
256 found = true;
257 }
258 }
259 return found;
260 }
262 /**
263 * Return true if the given class has constant fields to document.
264 *
265 * @param classDoc the class being checked.
266 * @return true if the given package has constant fields to document.
267 */
268 private boolean hasConstantField (ClassDoc classDoc) {
269 VisibleMemberMap visibleMemberMapFields = new VisibleMemberMap(classDoc,
270 VisibleMemberMap.FIELDS, configuration.nodeprecated);
271 List<?> fields = visibleMemberMapFields.getLeafClassMembers(configuration);
272 for (Iterator<?> iter = fields.iterator(); iter.hasNext(); ) {
273 FieldDoc field = (FieldDoc) iter.next();
274 if (field.constantValueExpression() != null) {
275 classDocsWithConstFields.add(classDoc);
276 return true;
277 }
278 }
279 return false;
280 }
282 /**
283 * Return true if the given package name has been printed. Also
284 * return true if the root of this package has been printed.
285 *
286 * @param pkgname the name of the package to check.
287 */
288 private boolean hasPrintedPackageIndex(String pkgname) {
289 String[] list = printedPackageHeaders.toArray(new String[] {});
290 for (int i = 0; i < list.length; i++) {
291 if (pkgname.startsWith(list[i])) {
292 return true;
293 }
294 }
295 return false;
296 }
298 /**
299 * Print the table of constants.
300 *
301 * @author Jamie Ho
302 * @since 1.4
303 */
304 private class ConstantFieldBuilder {
306 /**
307 * The map used to get the visible variables.
308 */
309 protected VisibleMemberMap visibleMemberMapFields = null;
311 /**
312 * The map used to get the visible variables.
313 */
314 protected VisibleMemberMap visibleMemberMapEnumConst = null;
316 /**
317 * The classdoc that we are examining constants for.
318 */
319 protected ClassDoc classdoc;
321 /**
322 * Construct a ConstantFieldSubWriter.
323 * @param classdoc the classdoc that we are examining constants for.
324 */
325 public ConstantFieldBuilder(ClassDoc classdoc) {
326 this.classdoc = classdoc;
327 visibleMemberMapFields = new VisibleMemberMap(classdoc,
328 VisibleMemberMap.FIELDS, configuration.nodeprecated);
329 visibleMemberMapEnumConst = new VisibleMemberMap(classdoc,
330 VisibleMemberMap.ENUM_CONSTANTS, configuration.nodeprecated);
331 }
333 /**
334 * Builds the table of constants for a given class.
335 *
336 * @param node the XML element that specifies which components to document
337 * @param classConstantTree the tree to which the class constants table
338 * will be added
339 */
340 protected void buildMembersSummary(XMLNode node, Content classConstantTree) {
341 List<FieldDoc> members = new ArrayList<FieldDoc>(members());
342 if (members.size() > 0) {
343 Collections.sort(members);
344 writer.addConstantMembers(classdoc, members, classConstantTree);
345 }
346 }
348 /**
349 * Return the list of visible constant fields for the given classdoc.
350 * @return the list of visible constant fields for the given classdoc.
351 */
352 protected List<FieldDoc> members() {
353 List<ProgramElementDoc> l = visibleMemberMapFields.getLeafClassMembers(configuration);
354 l.addAll(visibleMemberMapEnumConst.getLeafClassMembers(configuration));
355 Iterator<ProgramElementDoc> iter;
357 if(l != null){
358 iter = l.iterator();
359 } else {
360 return null;
361 }
362 List<FieldDoc> inclList = new LinkedList<FieldDoc>();
363 FieldDoc member;
364 while(iter.hasNext()){
365 member = (FieldDoc)iter.next();
366 if(member.constantValue() != null){
367 inclList.add(member);
368 }
369 }
370 return inclList;
371 }
372 }
374 /**
375 * Parse the package name. We only want to display package name up to
376 * 2 levels.
377 */
378 private String parsePackageName(String pkgname) {
379 int index = -1;
380 for (int j = 0; j < MAX_CONSTANT_VALUE_INDEX_LENGTH; j++) {
381 index = pkgname.indexOf(".", index + 1);
382 }
383 if (index != -1) {
384 pkgname = pkgname.substring(0, index);
385 }
386 return pkgname;
387 }
388 }