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