1 /*
   2  * Copyright (c) 2003, 2019, 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 jdk.javadoc.internal.doclets.toolkit.builders;
  27 
  28 import java.util.*;
  29 
  30 import javax.lang.model.element.Element;
  31 import javax.lang.model.element.TypeElement;
  32 import javax.lang.model.element.VariableElement;
  33 
  34 import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration;
  35 import jdk.javadoc.internal.doclets.toolkit.Content;
  36 import jdk.javadoc.internal.doclets.toolkit.DocletException;
  37 import jdk.javadoc.internal.doclets.toolkit.EnumConstantWriter;
  38 
  39 import static jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberTable.Kind.*;
  40 
  41 /**
  42  * Builds documentation for a enum constants.
  43  *
  44  *  <p><b>This is NOT part of any supported API.
  45  *  If you write code that depends on this, you do so at your own risk.
  46  *  This code and its internal interfaces are subject to change or
  47  *  deletion without notice.</b>
  48  *
  49  * @author Jamie Ho
  50  * @author Bhavesh Patel (Modified)
  51  */
  52 public class EnumConstantBuilder extends AbstractMemberBuilder {
  53 
  54     /**
  55      * The writer to output the enum constants documentation.
  56      */
  57     private final EnumConstantWriter writer;
  58 
  59     /**
  60      * The set of enum constants being documented.
  61      */
  62     private final List<? extends Element> enumConstants;
  63 
  64     /**
  65      * The current enum constant that is being documented at this point
  66      * in time.
  67      */
  68     private VariableElement currentElement;
  69 
  70     /**
  71      * Construct a new EnumConstantsBuilder.
  72      *
  73      * @param context  the build context.
  74      * @param typeElement the class whose members are being documented.
  75      * @param writer the doclet specific writer.
  76      */
  77     private EnumConstantBuilder(Context context,
  78             TypeElement typeElement, EnumConstantWriter writer) {
  79         super(context, typeElement);
  80         this.writer = writer;
  81         enumConstants = getVisibleMembers(ENUM_CONSTANTS);
  82     }
  83 
  84     /**
  85      * Construct a new EnumConstantsBuilder.
  86      *
  87      * @param context  the build context.
  88      * @param typeElement the class whoses members are being documented.
  89      * @param writer the doclet specific writer.
  90      * @return the new EnumConstantsBuilder
  91      */
  92     public static EnumConstantBuilder getInstance(Context context,
  93             TypeElement typeElement, EnumConstantWriter writer) {
  94         return new EnumConstantBuilder(context, typeElement, writer);
  95     }
  96 
  97     /**
  98      * Returns whether or not there are members to document.
  99      *
 100      * @return whether or not there are members to document
 101      */
 102     @Override
 103     public boolean hasMembersToDocument() {
 104         return !enumConstants.isEmpty();
 105     }
 106 
 107     /**
 108      * {@inheritDoc}
 109      */
 110     @Override
 111     public void build(Content contentTree) throws DocletException {
 112         buildEnumConstant(contentTree);
 113     }
 114 
 115     /**
 116      * Build the enum constant documentation.
 117      *
 118      * @param memberDetailsTree the content tree to which the documentation will be added
 119      * @throws DocletException is there is a problem while building the documentation
 120      */
 121     protected void buildEnumConstant(Content memberDetailsTree) throws DocletException {
 122         if (writer == null) {
 123             return;
 124         }
 125         if (hasMembersToDocument()) {
 126             Content enumConstantsDetailsTreeHeader = writer.getEnumConstantsDetailsTreeHeader(typeElement,
 127                     memberDetailsTree);
 128             Content enumConstantsDetailsTree = writer.getMemberTreeHeader();
 129 
 130             for (Element enumConstant : enumConstants) {
 131                 currentElement = (VariableElement)enumConstant;
 132                 Content enumConstantsTree = writer.getEnumConstantsTreeHeader(currentElement,
 133                         enumConstantsDetailsTree);
 134 
 135                 buildSignature(enumConstantsTree);
 136                 buildDeprecationInfo(enumConstantsTree);
 137                 buildEnumConstantComments(enumConstantsTree);
 138                 buildTagInfo(enumConstantsTree);
 139 
 140                 enumConstantsDetailsTree.add(writer.getEnumConstants(enumConstantsTree));
 141             }
 142             memberDetailsTree.add(
 143                     writer.getEnumConstantsDetails(enumConstantsDetailsTreeHeader, enumConstantsDetailsTree));
 144         }
 145     }
 146 
 147     /**
 148      * Build the signature.
 149      *
 150      * @param enumConstantsTree the content tree to which the documentation will be added
 151      */
 152     protected void buildSignature(Content enumConstantsTree) {
 153         enumConstantsTree.add(writer.getSignature(currentElement));
 154     }
 155 
 156     /**
 157      * Build the deprecation information.
 158      *
 159      * @param enumConstantsTree the content tree to which the documentation will be added
 160      */
 161     protected void buildDeprecationInfo(Content enumConstantsTree) {
 162         writer.addDeprecated(currentElement, enumConstantsTree);
 163     }
 164 
 165     /**
 166      * Build the comments for the enum constant.  Do nothing if
 167      * {@link BaseConfiguration#nocomment} is set to true.
 168      *
 169      * @param enumConstantsTree the content tree to which the documentation will be added
 170      */
 171     protected void buildEnumConstantComments(Content enumConstantsTree) {
 172         if (!configuration.nocomment) {
 173             writer.addComments(currentElement, enumConstantsTree);
 174         }
 175     }
 176 
 177     /**
 178      * Build the tag information.
 179      *
 180      * @param enumConstantsTree the content tree to which the documentation will be added
 181      */
 182     protected void buildTagInfo(Content enumConstantsTree) {
 183         writer.addTags(currentElement, enumConstantsTree);
 184     }
 185 
 186     /**
 187      * Return the enum constant writer for this builder.
 188      *
 189      * @return the enum constant writer for this builder.
 190      */
 191     public EnumConstantWriter getWriter() {
 192         return writer;
 193     }
 194 }