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.ExecutableElement;
034import javax.lang.model.element.TypeElement;
035
036import org.jdrupes.mdoclet.internal.doclets.toolkit.BaseOptions;
037import org.jdrupes.mdoclet.internal.doclets.toolkit.ConstructorWriter;
038import org.jdrupes.mdoclet.internal.doclets.toolkit.Content;
039import org.jdrupes.mdoclet.internal.doclets.toolkit.DocletException;
040
041/**
042 * Builds documentation for a constructor.
043 */
044public class ConstructorBuilder extends AbstractMemberBuilder {
045
046    /**
047     * The current constructor that is being documented at this point in time.
048     */
049    private ExecutableElement currentConstructor;
050
051    /**
052     * The writer to output the constructor documentation.
053     */
054    private final ConstructorWriter writer;
055
056    /**
057     * The constructors being documented.
058     */
059    private final List<? extends Element> constructors;
060
061    /**
062     * Construct a new ConstructorBuilder.
063     *
064     * @param context  the build context.
065     * @param typeElement the class whose members are being documented.
066     * @param writer the doclet specific writer.
067     */
068    private ConstructorBuilder(Context context,
069            TypeElement typeElement,
070            ConstructorWriter writer) {
071        super(context, typeElement);
072        this.writer = Objects.requireNonNull(writer);
073        constructors = getVisibleMembers(CONSTRUCTORS);
074        for (Element ctor : constructors) {
075            if (utils.isProtected(ctor) || utils.isPrivate(ctor)) {
076                writer.setFoundNonPubConstructor(true);
077            }
078        }
079    }
080
081    /**
082     * Construct a new ConstructorBuilder.
083     *
084     * @param context  the build context.
085     * @param typeElement the class whose members are being documented.
086     * @param writer the doclet specific writer.
087     * @return the new ConstructorBuilder
088     */
089    public static ConstructorBuilder getInstance(Context context,
090            TypeElement typeElement, ConstructorWriter writer) {
091        return new ConstructorBuilder(context, typeElement, writer);
092    }
093
094    @Override
095    public boolean hasMembersToDocument() {
096        return !constructors.isEmpty();
097    }
098
099    /**
100     * Return the constructor writer for this builder.
101     *
102     * @return the constructor writer for this builder.
103     */
104    public ConstructorWriter getWriter() {
105        return writer;
106    }
107
108    @Override
109    public void build(Content target) throws DocletException {
110        buildConstructorDoc(target);
111    }
112
113    /**
114     * Build the constructor documentation.
115     *
116     * @param target the content to which the documentation will be added
117     * @throws DocletException if there is a problem while building the documentation
118     */
119    protected void buildConstructorDoc(Content target) throws DocletException {
120        if (hasMembersToDocument()) {
121            Content constructorDetailsHeader
122                = writer.getConstructorDetailsHeader(target);
123            Content memberList = writer.getMemberList();
124
125            for (Element constructor : constructors) {
126                currentConstructor = (ExecutableElement) constructor;
127                Content constructorContent
128                    = writer.getConstructorHeaderContent(currentConstructor);
129
130                buildSignature(constructorContent);
131                buildDeprecationInfo(constructorContent);
132                buildPreviewInfo(constructorContent);
133                buildConstructorComments(constructorContent);
134                buildTagInfo(constructorContent);
135
136                memberList.add(writer.getMemberListItem(constructorContent));
137            }
138            Content constructorDetails = writer
139                .getConstructorDetails(constructorDetailsHeader, memberList);
140            target.add(constructorDetails);
141        }
142    }
143
144    /**
145     * Build the signature.
146     *
147     * @param constructorContent the content to which the documentation will be added
148     */
149    protected void buildSignature(Content constructorContent) {
150        constructorContent.add(writer.getSignature(currentConstructor));
151    }
152
153    /**
154     * Build the deprecation information.
155     *
156     * @param constructorContent the content to which the documentation will be added
157     */
158    protected void buildDeprecationInfo(Content constructorContent) {
159        writer.addDeprecated(currentConstructor, constructorContent);
160    }
161
162    /**
163     * Build the preview information.
164     *
165     * @param constructorContent the content to which the documentation will be added
166     */
167    protected void buildPreviewInfo(Content constructorContent) {
168        writer.addPreview(currentConstructor, constructorContent);
169    }
170
171    /**
172     * Build the comments for the constructor.  Do nothing if
173     * {@link BaseOptions#noComment()} is set to true.
174     *
175     * @param constructorContent the content to which the documentation will be added
176     */
177    protected void buildConstructorComments(Content constructorContent) {
178        if (!options.noComment()) {
179            writer.addComments(currentConstructor, constructorContent);
180        }
181    }
182
183    /**
184     * Build the tag information.
185     *
186     * @param constructorContent the content to which the documentation will be added
187     */
188    protected void buildTagInfo(Content constructorContent) {
189        writer.addTags(currentConstructor, constructorContent);
190    }
191}