src/share/classes/com/sun/tools/doclets/internal/toolkit/builders/ConstantsSummaryBuilder.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1606
ccbe7ffdd867
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial