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