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 javax.lang.model.element.PackageElement;
029
030import org.jdrupes.mdoclet.internal.doclets.toolkit.Content;
031import org.jdrupes.mdoclet.internal.doclets.toolkit.DocFilesHandler;
032import org.jdrupes.mdoclet.internal.doclets.toolkit.DocletException;
033import org.jdrupes.mdoclet.internal.doclets.toolkit.PackageSummaryWriter;
034
035/**
036 * Builds the summary for a given package.
037 */
038public class PackageSummaryBuilder extends AbstractBuilder {
039
040    /**
041     * The package being documented.
042     */
043    private final PackageElement packageElement;
044
045    /**
046     * The doclet specific writer that will output the result.
047     */
048    private final PackageSummaryWriter packageWriter;
049
050    /**
051     * Construct a new PackageSummaryBuilder.
052     *
053     * @param context  the build context.
054     * @param pkg the package being documented.
055     * @param packageWriter the doclet specific writer that will output the
056     *        result.
057     */
058    private PackageSummaryBuilder(Context context,
059            PackageElement pkg,
060            PackageSummaryWriter packageWriter) {
061        super(context);
062        this.packageElement = pkg;
063        this.packageWriter = packageWriter;
064    }
065
066    /**
067     * Construct a new PackageSummaryBuilder.
068     *
069     * @param context  the build context.
070     * @param pkg the package being documented.
071     * @param packageWriter the doclet specific writer that will output the
072     *        result.
073     *
074     * @return an instance of a PackageSummaryBuilder.
075     */
076    public static PackageSummaryBuilder getInstance(Context context,
077            PackageElement pkg, PackageSummaryWriter packageWriter) {
078        return new PackageSummaryBuilder(context, pkg, packageWriter);
079    }
080
081    /**
082     * Build the package summary.
083     *
084     * @throws DocletException if there is a problem while building the documentation
085     */
086    @Override
087    public void build() throws DocletException {
088        if (packageWriter == null) {
089            // Doclet does not support this output.
090            return;
091        }
092        buildPackageDoc();
093    }
094
095    /**
096     * Build the package documentation.
097     *
098     * @throws DocletException if there is a problem while building the documentation
099     */
100    protected void buildPackageDoc() throws DocletException {
101        Content content = packageWriter.getPackageHeader();
102
103        buildContent();
104
105        packageWriter.addPackageFooter();
106        packageWriter.printDocument(content);
107        DocFilesHandler docFilesHandler = configuration
108            .getWriterFactory()
109            .getDocFilesHandler(packageElement);
110        docFilesHandler.copyDocFiles();
111    }
112
113    /**
114     * Build the content for the package.
115     *
116     * @throws DocletException if there is a problem while building the documentation
117     */
118    protected void buildContent() throws DocletException {
119        Content packageContent = packageWriter.getContentHeader();
120
121        packageWriter.addPackageSignature(packageContent);
122        buildPackageDescription(packageContent);
123        buildPackageTags(packageContent);
124        buildSummary(packageContent);
125
126        packageWriter.addPackageContent(packageContent);
127    }
128
129    /**
130     * Builds the list of summaries for the different kinds of types in this package.
131     *
132     * @param packageContent the package content to which the summaries will
133     *                       be added
134     * @throws DocletException if there is a problem while building the documentation
135     */
136    protected void buildSummary(Content packageContent) throws DocletException {
137        Content summariesList = packageWriter.getSummariesList();
138
139        buildRelatedPackagesSummary(summariesList);
140        buildAllClassesAndInterfacesSummary(summariesList);
141
142        packageContent.add(packageWriter.getPackageSummary(summariesList));
143    }
144
145    /**
146     * Builds a list of "nearby" packages (subpackages, superpackages, and sibling packages).
147     *
148     * @param summariesList the list of summaries to which the summary will be added
149     */
150    protected void buildRelatedPackagesSummary(Content summariesList) {
151        packageWriter.addRelatedPackagesSummary(summariesList);
152    }
153
154    /**
155     * Builds the summary for all classes and interfaces in this package.
156     *
157     * @param summariesList the list of summaries to which the summary will be added
158     */
159    protected void buildAllClassesAndInterfacesSummary(Content summariesList) {
160        packageWriter.addAllClassesAndInterfacesSummary(summariesList);
161    }
162
163    /**
164     * Build the description of the summary.
165     *
166     * @param packageContent the content to which the package description will
167     *                       be added
168     */
169    protected void buildPackageDescription(Content packageContent) {
170        if (options.noComment()) {
171            return;
172        }
173        packageWriter.addPackageDescription(packageContent);
174    }
175
176    /**
177     * Build the tags of the summary.
178     *
179     * @param packageContent the content to which the package tags will be added
180     */
181    protected void buildPackageTags(Content packageContent) {
182        if (options.noComment()) {
183            return;
184        }
185        packageWriter.addPackageTags(packageContent);
186    }
187}