1 /*
   2  * Copyright (c) 2003, 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.internal.toolkit.builders;
  27 
  28 import java.util.*;
  29 
  30 import com.sun.javadoc.*;
  31 import com.sun.tools.doclets.internal.toolkit.*;
  32 import com.sun.tools.doclets.internal.toolkit.util.*;
  33 
  34 /**
  35  * Builds documentation for a enum constants.
  36  *
  37  *  <p><b>This is NOT part of any supported API.
  38  *  If you write code that depends on this, you do so at your own risk.
  39  *  This code and its internal interfaces are subject to change or
  40  *  deletion without notice.</b>
  41  *
  42  * @author Jamie Ho
  43  * @author Bhavesh Patel (Modified)
  44  * @since 1.5
  45  */
  46 public class EnumConstantBuilder extends AbstractMemberBuilder {
  47 
  48     /**
  49      * The class whose enum constants are being documented.
  50      */
  51     private final ClassDoc classDoc;
  52 
  53     /**
  54      * The visible enum constantss for the given class.
  55      */
  56     private final VisibleMemberMap visibleMemberMap;
  57 
  58     /**
  59      * The writer to output the enum constants documentation.
  60      */
  61     private final EnumConstantWriter writer;
  62 
  63     /**
  64      * The list of enum constants being documented.
  65      */
  66     private final List<ProgramElementDoc> enumConstants;
  67 
  68     /**
  69      * The index of the current enum constant that is being documented at this point
  70      * in time.
  71      */
  72     private int currentEnumConstantsIndex;
  73 
  74     /**
  75      * Construct a new EnumConstantsBuilder.
  76      *
  77      * @param context  the build context.
  78      * @param classDoc the class whoses members are being documented.
  79      * @param writer the doclet specific writer.
  80      */
  81     private EnumConstantBuilder(Context context,
  82             ClassDoc classDoc, EnumConstantWriter writer) {
  83         super(context);
  84         this.classDoc = classDoc;
  85         this.writer = writer;
  86         visibleMemberMap =
  87                 new VisibleMemberMap(
  88                 classDoc,
  89                 VisibleMemberMap.ENUM_CONSTANTS,
  90                 configuration);
  91         enumConstants = new ArrayList<>(visibleMemberMap.getMembersFor(classDoc));
  92         if (configuration.getMemberComparator() != null) {
  93             Collections.sort(enumConstants, configuration.getMemberComparator());
  94         }
  95     }
  96 
  97     /**
  98      * Construct a new EnumConstantsBuilder.
  99      *
 100      * @param context  the build context.
 101      * @param classDoc the class whoses members are being documented.
 102      * @param writer the doclet specific writer.
 103      */
 104     public static EnumConstantBuilder getInstance(Context context,
 105             ClassDoc classDoc, EnumConstantWriter writer) {
 106         return new EnumConstantBuilder(context, classDoc, writer);
 107     }
 108 
 109     /**
 110      * {@inheritDoc}
 111      */
 112     public String getName() {
 113         return "EnumConstantDetails";
 114     }
 115 
 116     /**
 117      * Returns a list of enum constants that will be documented for the given class.
 118      * This information can be used for doclet specific documentation
 119      * generation.
 120      *
 121      * @param classDoc the {@link ClassDoc} we want to check.
 122      * @return a list of enum constants that will be documented.
 123      */
 124     public List<ProgramElementDoc> members(ClassDoc classDoc) {
 125         return visibleMemberMap.getMembersFor(classDoc);
 126     }
 127 
 128     /**
 129      * Returns the visible member map for the enum constants of this class.
 130      *
 131      * @return the visible member map for the enum constants of this class.
 132      */
 133     public VisibleMemberMap getVisibleMemberMap() {
 134         return visibleMemberMap;
 135     }
 136 
 137     /**
 138      * summaryOrder.size()
 139      */
 140     public boolean hasMembersToDocument() {
 141         return enumConstants.size() > 0;
 142     }
 143 
 144     /**
 145      * Build the enum constant documentation.
 146      *
 147      * @param node the XML element that specifies which components to document
 148      * @param memberDetailsTree the content tree to which the documentation will be added
 149      */
 150     public void buildEnumConstant(XMLNode node, Content memberDetailsTree) {
 151         if (writer == null) {
 152             return;
 153         }
 154         int size = enumConstants.size();
 155         if (size > 0) {
 156             Content enumConstantsDetailsTree = writer.getEnumConstantsDetailsTreeHeader(
 157                     classDoc, memberDetailsTree);
 158             for (currentEnumConstantsIndex = 0; currentEnumConstantsIndex < size;
 159                     currentEnumConstantsIndex++) {
 160                 Content enumConstantsTree = writer.getEnumConstantsTreeHeader(
 161                         (FieldDoc) enumConstants.get(currentEnumConstantsIndex),
 162                         enumConstantsDetailsTree);
 163                 buildChildren(node, enumConstantsTree);
 164                 enumConstantsDetailsTree.addContent(writer.getEnumConstants(
 165                         enumConstantsTree, (currentEnumConstantsIndex == size - 1)));
 166             }
 167             memberDetailsTree.addContent(
 168                     writer.getEnumConstantsDetails(enumConstantsDetailsTree));
 169         }
 170     }
 171 
 172     /**
 173      * Build the signature.
 174      *
 175      * @param node the XML element that specifies which components to document
 176      * @param enumConstantsTree the content tree to which the documentation will be added
 177      */
 178     public void buildSignature(XMLNode node, Content enumConstantsTree) {
 179         enumConstantsTree.addContent(writer.getSignature(
 180                 (FieldDoc) enumConstants.get(currentEnumConstantsIndex)));
 181     }
 182 
 183     /**
 184      * Build the deprecation information.
 185      *
 186      * @param node the XML element that specifies which components to document
 187      * @param enumConstantsTree the content tree to which the documentation will be added
 188      */
 189     public void buildDeprecationInfo(XMLNode node, Content enumConstantsTree) {
 190         writer.addDeprecated(
 191                 (FieldDoc) enumConstants.get(currentEnumConstantsIndex),
 192                 enumConstantsTree);
 193     }
 194 
 195     /**
 196      * Build the comments for the enum constant.  Do nothing if
 197      * {@link Configuration#nocomment} is set to true.
 198      *
 199      * @param node the XML element that specifies which components to document
 200      * @param enumConstantsTree the content tree to which the documentation will be added
 201      */
 202     public void buildEnumConstantComments(XMLNode node, Content enumConstantsTree) {
 203         if (!configuration.nocomment) {
 204             writer.addComments(
 205                     (FieldDoc) enumConstants.get(currentEnumConstantsIndex),
 206                     enumConstantsTree);
 207         }
 208     }
 209 
 210     /**
 211      * Build the tag information.
 212      *
 213      * @param node the XML element that specifies which components to document
 214      * @param enumConstantsTree the content tree to which the documentation will be added
 215      */
 216     public void buildTagInfo(XMLNode node, Content enumConstantsTree) {
 217         writer.addTags(
 218                 (FieldDoc) enumConstants.get(currentEnumConstantsIndex),
 219                 enumConstantsTree);
 220     }
 221 
 222     /**
 223      * Return the enum constant writer for this builder.
 224      *
 225      * @return the enum constant writer for this builder.
 226      */
 227     public EnumConstantWriter getWriter() {
 228         return writer;
 229     }
 230 }