Wed, 01 Dec 2010 11:02:38 -0800
6851834: Javadoc doclet needs a structured approach to generate the output HTML.
Reviewed-by: jjg
duke@1 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.doclets.internal.toolkit.builders; |
duke@1 | 27 | |
bpatel@766 | 28 | import java.io.*; |
bpatel@766 | 29 | import java.util.*; |
bpatel@766 | 30 | import com.sun.javadoc.*; |
duke@1 | 31 | import com.sun.tools.doclets.internal.toolkit.util.*; |
duke@1 | 32 | import com.sun.tools.doclets.internal.toolkit.*; |
duke@1 | 33 | |
duke@1 | 34 | /** |
duke@1 | 35 | * Builds the Constants Summary Page. |
duke@1 | 36 | * |
duke@1 | 37 | * This code is not part of an API. |
duke@1 | 38 | * It is implementation that is subject to change. |
duke@1 | 39 | * Do not use it as an API |
duke@1 | 40 | * |
duke@1 | 41 | * @author Jamie Ho |
bpatel@766 | 42 | * @author Bhavesh Patel (Modified) |
duke@1 | 43 | * @since 1.5 |
duke@1 | 44 | */ |
duke@1 | 45 | public class ConstantsSummaryBuilder extends AbstractBuilder { |
duke@1 | 46 | |
duke@1 | 47 | /** |
duke@1 | 48 | * The root element of the constant summary XML is {@value}. |
duke@1 | 49 | */ |
duke@1 | 50 | public static final String ROOT = "ConstantSummary"; |
duke@1 | 51 | |
duke@1 | 52 | /** |
duke@1 | 53 | * The maximum number of package directories shown in the constant |
duke@1 | 54 | * value index. |
duke@1 | 55 | */ |
duke@1 | 56 | public static final int MAX_CONSTANT_VALUE_INDEX_LENGTH = 2; |
duke@1 | 57 | |
duke@1 | 58 | /** |
duke@1 | 59 | * The writer used to write the results. |
duke@1 | 60 | */ |
duke@1 | 61 | protected ConstantsSummaryWriter writer; |
duke@1 | 62 | |
duke@1 | 63 | /** |
duke@1 | 64 | * The set of ClassDocs that have constant fields. |
duke@1 | 65 | */ |
jjg@74 | 66 | protected Set<ClassDoc> classDocsWithConstFields; |
duke@1 | 67 | |
duke@1 | 68 | /** |
duke@1 | 69 | * The set of printed package headers. |
duke@1 | 70 | */ |
jjg@74 | 71 | protected Set<String> printedPackageHeaders; |
duke@1 | 72 | |
duke@1 | 73 | /** |
duke@1 | 74 | * The current package being documented. |
duke@1 | 75 | */ |
duke@1 | 76 | private PackageDoc currentPackage; |
duke@1 | 77 | |
duke@1 | 78 | /** |
duke@1 | 79 | * The current class being documented. |
duke@1 | 80 | */ |
duke@1 | 81 | private ClassDoc currentClass; |
duke@1 | 82 | |
duke@1 | 83 | /** |
bpatel@766 | 84 | * The content tree for the constant summary documentation. |
bpatel@766 | 85 | */ |
bpatel@766 | 86 | private Content contentTree; |
bpatel@766 | 87 | |
bpatel@766 | 88 | /** |
duke@1 | 89 | * Construct a new ConstantsSummaryBuilder. |
duke@1 | 90 | * |
duke@1 | 91 | * @param configuration the current configuration of the |
duke@1 | 92 | * doclet. |
duke@1 | 93 | */ |
duke@1 | 94 | private ConstantsSummaryBuilder(Configuration configuration) { |
duke@1 | 95 | super(configuration); |
duke@1 | 96 | } |
duke@1 | 97 | |
duke@1 | 98 | /** |
duke@1 | 99 | * Construct a ConstantsSummaryBuilder. |
duke@1 | 100 | * |
duke@1 | 101 | * @param configuration the configuration used in this run |
duke@1 | 102 | * of the doclet. |
duke@1 | 103 | * @param writer the writer for the summary. |
duke@1 | 104 | */ |
duke@1 | 105 | public static ConstantsSummaryBuilder getInstance( |
duke@1 | 106 | Configuration configuration, ConstantsSummaryWriter writer) { |
duke@1 | 107 | ConstantsSummaryBuilder builder = new ConstantsSummaryBuilder( |
duke@1 | 108 | configuration); |
duke@1 | 109 | builder.writer = writer; |
jjg@74 | 110 | builder.classDocsWithConstFields = new HashSet<ClassDoc>(); |
duke@1 | 111 | return builder; |
duke@1 | 112 | } |
duke@1 | 113 | |
duke@1 | 114 | /** |
duke@1 | 115 | * {@inheritDoc} |
duke@1 | 116 | */ |
duke@1 | 117 | public void build() throws IOException { |
duke@1 | 118 | if (writer == null) { |
duke@1 | 119 | //Doclet does not support this output. |
duke@1 | 120 | return; |
duke@1 | 121 | } |
bpatel@766 | 122 | build(LayoutParser.getInstance(configuration).parseXML(ROOT), contentTree); |
duke@1 | 123 | } |
duke@1 | 124 | |
duke@1 | 125 | /** |
duke@1 | 126 | * {@inheritDoc} |
duke@1 | 127 | */ |
duke@1 | 128 | public String getName() { |
duke@1 | 129 | return ROOT; |
duke@1 | 130 | } |
duke@1 | 131 | |
duke@1 | 132 | /** |
duke@1 | 133 | * Build the constant summary. |
duke@1 | 134 | * |
bpatel@766 | 135 | * @param node the XML element that specifies which components to document |
bpatel@766 | 136 | * @param contentTree the content tree to which the documentation will be added |
duke@1 | 137 | */ |
bpatel@766 | 138 | public void buildConstantSummary(XMLNode node, Content contentTree) throws Exception { |
bpatel@766 | 139 | contentTree = writer.getHeader(); |
bpatel@766 | 140 | buildChildren(node, contentTree); |
bpatel@766 | 141 | writer.addFooter(contentTree); |
bpatel@766 | 142 | writer.printDocument(contentTree); |
duke@1 | 143 | writer.close(); |
duke@1 | 144 | } |
duke@1 | 145 | |
duke@1 | 146 | /** |
bpatel@766 | 147 | * Build the list of packages. |
bpatel@766 | 148 | * |
bpatel@766 | 149 | * @param node the XML element that specifies which components to document |
bpatel@766 | 150 | * @param contentTree the content tree to which the content list will be added |
duke@1 | 151 | */ |
bpatel@766 | 152 | public void buildContents(XMLNode node, Content contentTree) { |
bpatel@766 | 153 | Content contentListTree = writer.getContentsHeader(); |
duke@1 | 154 | PackageDoc[] packages = configuration.packages; |
jjg@74 | 155 | printedPackageHeaders = new HashSet<String>(); |
duke@1 | 156 | for (int i = 0; i < packages.length; i++) { |
duke@1 | 157 | if (hasConstantField(packages[i]) && ! hasPrintedPackageIndex(packages[i].name())) { |
bpatel@766 | 158 | writer.addLinkToPackageContent(packages[i], |
duke@1 | 159 | parsePackageName(packages[i].name()), |
bpatel@766 | 160 | printedPackageHeaders, contentListTree); |
duke@1 | 161 | } |
duke@1 | 162 | } |
bpatel@766 | 163 | contentTree.addContent(writer.getContentsList(contentListTree)); |
duke@1 | 164 | } |
duke@1 | 165 | |
duke@1 | 166 | /** |
duke@1 | 167 | * Build the summary for each documented package. |
duke@1 | 168 | * |
bpatel@766 | 169 | * @param node the XML element that specifies which components to document |
bpatel@766 | 170 | * @param contentTree the tree to which the summaries will be added |
duke@1 | 171 | */ |
bpatel@766 | 172 | public void buildConstantSummaries(XMLNode node, Content contentTree) { |
duke@1 | 173 | PackageDoc[] packages = configuration.packages; |
jjg@74 | 174 | printedPackageHeaders = new HashSet<String>(); |
bpatel@766 | 175 | Content summariesTree = writer.getConstantSummaries(); |
duke@1 | 176 | for (int i = 0; i < packages.length; i++) { |
duke@1 | 177 | if (hasConstantField(packages[i])) { |
duke@1 | 178 | currentPackage = packages[i]; |
duke@1 | 179 | //Build the documentation for the current package. |
bpatel@766 | 180 | buildChildren(node, summariesTree); |
duke@1 | 181 | } |
duke@1 | 182 | } |
bpatel@766 | 183 | contentTree.addContent(summariesTree); |
duke@1 | 184 | } |
duke@1 | 185 | |
duke@1 | 186 | /** |
bpatel@766 | 187 | * Build the header for the given package. |
duke@1 | 188 | * |
bpatel@766 | 189 | * @param node the XML element that specifies which components to document |
bpatel@766 | 190 | * @param summariesTree the tree to which the package header will be added |
duke@1 | 191 | */ |
bpatel@766 | 192 | public void buildPackageHeader(XMLNode node, Content summariesTree) { |
bpatel@766 | 193 | String parsedPackageName = parsePackageName(currentPackage.name()); |
bpatel@766 | 194 | if (! printedPackageHeaders.contains(parsedPackageName)) { |
bpatel@766 | 195 | writer.addPackageName(currentPackage, |
bpatel@766 | 196 | parsePackageName(currentPackage.name()), summariesTree); |
bpatel@766 | 197 | printedPackageHeaders.add(parsedPackageName); |
bpatel@766 | 198 | } |
duke@1 | 199 | } |
duke@1 | 200 | |
duke@1 | 201 | /** |
duke@1 | 202 | * Build the summary for the current class. |
duke@1 | 203 | * |
bpatel@766 | 204 | * @param node the XML element that specifies which components to document |
bpatel@766 | 205 | * @param summariesTree the tree to which the class constant summary will be added |
duke@1 | 206 | */ |
bpatel@766 | 207 | public void buildClassConstantSummary(XMLNode node, Content summariesTree) { |
duke@1 | 208 | ClassDoc[] classes = currentPackage.name().length() > 0 ? |
duke@1 | 209 | currentPackage.allClasses() : |
duke@1 | 210 | configuration.classDocCatalog.allClasses( |
duke@1 | 211 | DocletConstants.DEFAULT_PACKAGE_NAME); |
duke@1 | 212 | Arrays.sort(classes); |
bpatel@766 | 213 | Content classConstantTree = writer.getClassConstantHeader(); |
duke@1 | 214 | for (int i = 0; i < classes.length; i++) { |
duke@1 | 215 | if (! classDocsWithConstFields.contains(classes[i]) || |
duke@1 | 216 | ! classes[i].isIncluded()) { |
duke@1 | 217 | continue; |
duke@1 | 218 | } |
duke@1 | 219 | currentClass = classes[i]; |
duke@1 | 220 | //Build the documentation for the current class. |
bpatel@766 | 221 | buildChildren(node, classConstantTree); |
duke@1 | 222 | } |
bpatel@766 | 223 | summariesTree.addContent(classConstantTree); |
duke@1 | 224 | } |
duke@1 | 225 | |
duke@1 | 226 | /** |
bpatel@766 | 227 | * Build the summary of constant members in the class. |
bpatel@766 | 228 | * |
bpatel@766 | 229 | * @param node the XML element that specifies which components to document |
bpatel@766 | 230 | * @param classConstantTree the tree to which the constant members table |
bpatel@766 | 231 | * will be added |
duke@1 | 232 | */ |
bpatel@766 | 233 | public void buildConstantMembers(XMLNode node, Content classConstantTree) { |
bpatel@766 | 234 | new ConstantFieldBuilder(currentClass).buildMembersSummary(node, classConstantTree); |
duke@1 | 235 | } |
duke@1 | 236 | |
duke@1 | 237 | /** |
duke@1 | 238 | * Return true if the given package has constant fields to document. |
duke@1 | 239 | * |
duke@1 | 240 | * @param pkg the package being checked. |
duke@1 | 241 | * @return true if the given package has constant fields to document. |
duke@1 | 242 | */ |
duke@1 | 243 | private boolean hasConstantField(PackageDoc pkg) { |
duke@1 | 244 | ClassDoc[] classes; |
duke@1 | 245 | if (pkg.name().length() > 0) { |
duke@1 | 246 | classes = pkg.allClasses(); |
duke@1 | 247 | } else { |
duke@1 | 248 | classes = configuration.classDocCatalog.allClasses( |
duke@1 | 249 | DocletConstants.DEFAULT_PACKAGE_NAME); |
duke@1 | 250 | } |
duke@1 | 251 | boolean found = false; |
duke@1 | 252 | for (int j = 0; j < classes.length; j++){ |
duke@1 | 253 | if (classes[j].isIncluded() && hasConstantField(classes[j])) { |
duke@1 | 254 | found = true; |
duke@1 | 255 | } |
duke@1 | 256 | } |
duke@1 | 257 | return found; |
duke@1 | 258 | } |
duke@1 | 259 | |
duke@1 | 260 | /** |
duke@1 | 261 | * Return true if the given class has constant fields to document. |
duke@1 | 262 | * |
duke@1 | 263 | * @param classDoc the class being checked. |
duke@1 | 264 | * @return true if the given package has constant fields to document. |
duke@1 | 265 | */ |
duke@1 | 266 | private boolean hasConstantField (ClassDoc classDoc) { |
duke@1 | 267 | VisibleMemberMap visibleMemberMapFields = new VisibleMemberMap(classDoc, |
duke@1 | 268 | VisibleMemberMap.FIELDS, configuration.nodeprecated); |
mcimadamore@184 | 269 | List<?> fields = visibleMemberMapFields.getLeafClassMembers(configuration); |
mcimadamore@184 | 270 | for (Iterator<?> iter = fields.iterator(); iter.hasNext(); ) { |
duke@1 | 271 | FieldDoc field = (FieldDoc) iter.next(); |
duke@1 | 272 | if (field.constantValueExpression() != null) { |
duke@1 | 273 | classDocsWithConstFields.add(classDoc); |
duke@1 | 274 | return true; |
duke@1 | 275 | } |
duke@1 | 276 | } |
duke@1 | 277 | return false; |
duke@1 | 278 | } |
duke@1 | 279 | |
duke@1 | 280 | /** |
duke@1 | 281 | * Return true if the given package name has been printed. Also |
duke@1 | 282 | * return true if the root of this package has been printed. |
duke@1 | 283 | * |
duke@1 | 284 | * @param pkgname the name of the package to check. |
duke@1 | 285 | */ |
duke@1 | 286 | private boolean hasPrintedPackageIndex(String pkgname) { |
jjg@74 | 287 | String[] list = printedPackageHeaders.toArray(new String[] {}); |
duke@1 | 288 | for (int i = 0; i < list.length; i++) { |
duke@1 | 289 | if (pkgname.startsWith(list[i])) { |
duke@1 | 290 | return true; |
duke@1 | 291 | } |
duke@1 | 292 | } |
duke@1 | 293 | return false; |
duke@1 | 294 | } |
duke@1 | 295 | |
duke@1 | 296 | /** |
duke@1 | 297 | * Print the table of constants. |
duke@1 | 298 | * |
duke@1 | 299 | * @author Jamie Ho |
duke@1 | 300 | * @since 1.4 |
duke@1 | 301 | */ |
duke@1 | 302 | private class ConstantFieldBuilder { |
duke@1 | 303 | |
duke@1 | 304 | /** |
duke@1 | 305 | * The map used to get the visible variables. |
duke@1 | 306 | */ |
duke@1 | 307 | protected VisibleMemberMap visibleMemberMapFields = null; |
duke@1 | 308 | |
duke@1 | 309 | /** |
duke@1 | 310 | * The map used to get the visible variables. |
duke@1 | 311 | */ |
duke@1 | 312 | protected VisibleMemberMap visibleMemberMapEnumConst = null; |
duke@1 | 313 | |
duke@1 | 314 | /** |
duke@1 | 315 | * The classdoc that we are examining constants for. |
duke@1 | 316 | */ |
duke@1 | 317 | protected ClassDoc classdoc; |
duke@1 | 318 | |
duke@1 | 319 | /** |
duke@1 | 320 | * Construct a ConstantFieldSubWriter. |
duke@1 | 321 | * @param classdoc the classdoc that we are examining constants for. |
duke@1 | 322 | */ |
duke@1 | 323 | public ConstantFieldBuilder(ClassDoc classdoc) { |
duke@1 | 324 | this.classdoc = classdoc; |
duke@1 | 325 | visibleMemberMapFields = new VisibleMemberMap(classdoc, |
duke@1 | 326 | VisibleMemberMap.FIELDS, configuration.nodeprecated); |
duke@1 | 327 | visibleMemberMapEnumConst = new VisibleMemberMap(classdoc, |
duke@1 | 328 | VisibleMemberMap.ENUM_CONSTANTS, configuration.nodeprecated); |
duke@1 | 329 | } |
duke@1 | 330 | |
duke@1 | 331 | /** |
duke@1 | 332 | * Builds the table of constants for a given class. |
bpatel@766 | 333 | * |
bpatel@766 | 334 | * @param node the XML element that specifies which components to document |
bpatel@766 | 335 | * @param classConstantTree the tree to which the class constants table |
bpatel@766 | 336 | * will be added |
duke@1 | 337 | */ |
bpatel@766 | 338 | protected void buildMembersSummary(XMLNode node, Content classConstantTree) { |
jjg@74 | 339 | List<FieldDoc> members = new ArrayList<FieldDoc>(members()); |
duke@1 | 340 | if (members.size() > 0) { |
duke@1 | 341 | Collections.sort(members); |
bpatel@766 | 342 | writer.addConstantMembers(classdoc, members, classConstantTree); |
duke@1 | 343 | } |
duke@1 | 344 | } |
duke@1 | 345 | |
duke@1 | 346 | /** |
duke@1 | 347 | * Return the list of visible constant fields for the given classdoc. |
duke@1 | 348 | * @param cd the classdoc to examine. |
duke@1 | 349 | * @return the list of visible constant fields for the given classdoc. |
duke@1 | 350 | */ |
jjg@74 | 351 | protected List<FieldDoc> members() { |
jjg@74 | 352 | List<ProgramElementDoc> l = visibleMemberMapFields.getLeafClassMembers(configuration); |
duke@1 | 353 | l.addAll(visibleMemberMapEnumConst.getLeafClassMembers(configuration)); |
jjg@74 | 354 | Iterator<ProgramElementDoc> iter; |
duke@1 | 355 | |
duke@1 | 356 | if(l != null){ |
duke@1 | 357 | iter = l.iterator(); |
duke@1 | 358 | } else { |
duke@1 | 359 | return null; |
duke@1 | 360 | } |
jjg@74 | 361 | List<FieldDoc> inclList = new LinkedList<FieldDoc>(); |
duke@1 | 362 | FieldDoc member; |
duke@1 | 363 | while(iter.hasNext()){ |
duke@1 | 364 | member = (FieldDoc)iter.next(); |
duke@1 | 365 | if(member.constantValue() != null){ |
duke@1 | 366 | inclList.add(member); |
duke@1 | 367 | } |
duke@1 | 368 | } |
duke@1 | 369 | return inclList; |
duke@1 | 370 | } |
duke@1 | 371 | } |
duke@1 | 372 | |
duke@1 | 373 | /** |
duke@1 | 374 | * Parse the package name. We only want to display package name up to |
duke@1 | 375 | * 2 levels. |
duke@1 | 376 | */ |
duke@1 | 377 | private String parsePackageName(String pkgname) { |
duke@1 | 378 | int index = -1; |
duke@1 | 379 | for (int j = 0; j < MAX_CONSTANT_VALUE_INDEX_LENGTH; j++) { |
duke@1 | 380 | index = pkgname.indexOf(".", index + 1); |
duke@1 | 381 | } |
duke@1 | 382 | if (index != -1) { |
duke@1 | 383 | pkgname = pkgname.substring(0, index); |
duke@1 | 384 | } |
duke@1 | 385 | return pkgname; |
duke@1 | 386 | } |
duke@1 | 387 | } |