001/*
002 * Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package org.jdrupes.mdoclet.internal.doclets.toolkit.builders;
027
028import static org.jdrupes.mdoclet.internal.doclets.toolkit.util.VisibleMemberTable.Kind.*;
029
030import java.util.*;
031
032import javax.lang.model.element.Element;
033import javax.lang.model.element.TypeElement;
034import javax.lang.model.element.VariableElement;
035
036import org.jdrupes.mdoclet.internal.doclets.toolkit.BaseOptions;
037import org.jdrupes.mdoclet.internal.doclets.toolkit.Content;
038import org.jdrupes.mdoclet.internal.doclets.toolkit.DocletException;
039import org.jdrupes.mdoclet.internal.doclets.toolkit.FieldWriter;
040
041/**
042 * Builds documentation for a field.
043 */
044public class FieldBuilder extends AbstractMemberBuilder {
045
046    /**
047     * The writer to output the field documentation.
048     */
049    private final FieldWriter writer;
050
051    /**
052     * The list of fields being documented.
053     */
054    private final List<? extends Element> fields;
055
056    /**
057     * The index of the current field that is being documented at this point
058     * in time.
059     */
060    private VariableElement currentElement;
061
062    /**
063     * Construct a new FieldBuilder.
064     *
065     * @param context  the build context.
066     * @param typeElement the class whose members are being documented.
067     * @param writer the doclet specific writer.
068     */
069    private FieldBuilder(Context context,
070            TypeElement typeElement,
071            FieldWriter writer) {
072        super(context, typeElement);
073        this.writer = Objects.requireNonNull(writer);
074        fields = getVisibleMembers(FIELDS);
075    }
076
077    /**
078     * Construct a new FieldBuilder.
079     *
080     * @param context  the build context.
081     * @param typeElement the class whose members are being documented.
082     * @param writer the doclet specific writer.
083     * @return the new FieldBuilder
084     */
085    public static FieldBuilder getInstance(Context context,
086            TypeElement typeElement,
087            FieldWriter writer) {
088        return new FieldBuilder(context, typeElement, writer);
089    }
090
091    /**
092     * Returns whether or not there are members to document.
093     *
094     * @return whether or not there are members to document
095     */
096    @Override
097    public boolean hasMembersToDocument() {
098        return !fields.isEmpty();
099    }
100
101    @Override
102    public void build(Content target) throws DocletException {
103        buildFieldDoc(target);
104    }
105
106    /**
107     * Build the field documentation.
108     *
109     * @param target the content to which the documentation will be added
110     * @throws DocletException if there is a problem while building the documentation
111     */
112    protected void buildFieldDoc(Content target) throws DocletException {
113        if (!fields.isEmpty()) {
114            Content fieldDetailsHeader = writer.getFieldDetailsHeader(target);
115            Content memberList = writer.getMemberList();
116
117            for (Element element : fields) {
118                currentElement = (VariableElement) element;
119                Content fieldContent
120                    = writer.getFieldHeaderContent(currentElement);
121
122                buildSignature(fieldContent);
123                buildDeprecationInfo(fieldContent);
124                buildPreviewInfo(fieldContent);
125                buildFieldComments(fieldContent);
126                buildTagInfo(fieldContent);
127
128                memberList.add(writer.getMemberListItem(fieldContent));
129            }
130            Content fieldDetails
131                = writer.getFieldDetails(fieldDetailsHeader, memberList);
132            target.add(fieldDetails);
133        }
134    }
135
136    /**
137     * Build the signature.
138     *
139     * @param fieldContent the content to which the documentation will be added
140     */
141    protected void buildSignature(Content fieldContent) {
142        fieldContent.add(writer.getSignature(currentElement));
143    }
144
145    /**
146     * Build the deprecation information.
147     *
148     * @param fieldContent the content to which the documentation will be added
149     */
150    protected void buildDeprecationInfo(Content fieldContent) {
151        writer.addDeprecated(currentElement, fieldContent);
152    }
153
154    /**
155     * Build the preview information.
156     *
157     * @param fieldContent the content to which the documentation will be added
158     */
159    protected void buildPreviewInfo(Content fieldContent) {
160        writer.addPreview(currentElement, fieldContent);
161    }
162
163    /**
164     * Build the comments for the field.  Do nothing if
165     * {@link BaseOptions#noComment()} is set to true.
166     *
167     * @param fieldContent the content to which the documentation will be added
168     */
169    protected void buildFieldComments(Content fieldContent) {
170        if (!options.noComment()) {
171            writer.addComments(currentElement, fieldContent);
172        }
173    }
174
175    /**
176     * Build the tag information.
177     *
178     * @param fieldContent the content to which the documentation will be added
179     */
180    protected void buildTagInfo(Content fieldContent) {
181        writer.addTags(currentElement, fieldContent);
182    }
183
184    /**
185     * Return the field writer for this builder.
186     *
187     * @return the field writer for this builder.
188     */
189    public FieldWriter getWriter() {
190        return writer;
191    }
192}