001/*
002 * Copyright (c) 1998, 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.util;
027
028import java.io.*;
029
030import javax.lang.model.element.ModuleElement;
031import javax.lang.model.element.PackageElement;
032
033import org.jdrupes.mdoclet.internal.doclets.toolkit.BaseConfiguration;
034import org.jdrupes.mdoclet.internal.doclets.toolkit.BaseOptions;
035
036import jdk.javadoc.doclet.DocletEnvironment;
037
038/**
039 * Write out the element index.
040 */
041public class ElementListWriter {
042
043    private final BaseConfiguration configuration;
044    private final BaseOptions options;
045    private final Utils utils;
046    private final DocFile file;
047
048    /**
049     * Constructor.
050     *
051     * @param configuration the current configuration of the doclet.
052     */
053    public ElementListWriter(BaseConfiguration configuration) {
054        file = DocFile.createFileForOutput(configuration,
055            DocPaths.ELEMENT_LIST);
056        this.configuration = configuration;
057        this.options = configuration.getOptions();
058        this.utils = configuration.utils;
059    }
060
061    /**
062     * Generate the element index.
063     *
064     * @param configuration the current configuration of the doclet.
065     * @throws DocFileIOException if there is a problem writing the output
066     */
067    public static void generate(BaseConfiguration configuration)
068            throws DocFileIOException {
069        ElementListWriter elemgen = new ElementListWriter(configuration);
070        elemgen.generateElementListFile(configuration.docEnv);
071    }
072
073    protected void generateElementListFile(DocletEnvironment docEnv)
074            throws DocFileIOException {
075        try (Writer fileWriter = file.openWriter();
076                BufferedWriter out
077                    = (fileWriter instanceof BufferedWriter b) ? b
078                        : new BufferedWriter(fileWriter)) {
079            if (configuration.showModules) {
080                for (ModuleElement mdle : configuration.modulePackages
081                    .keySet()) {
082                    if (!(options.noDeprecated() && utils.isDeprecated(mdle))) {
083                        out.write(
084                            DocletConstants.MODULE_PREFIX + mdle.toString());
085                        out.newLine();
086                        for (PackageElement pkg : configuration.modulePackages
087                            .get(mdle)) {
088                            out.write(pkg.toString());
089                            out.newLine();
090                        }
091                    }
092                }
093            } else {
094                for (PackageElement pkg : configuration.packages) {
095                    // if the -nodeprecated option is set and the package is
096                    // marked as
097                    // deprecated, do not include it in the packages list.
098                    if (!(options.noDeprecated() && utils.isDeprecated(pkg))) {
099                        out.write(pkg.toString());
100                        out.newLine();
101                    }
102                }
103            }
104        } catch (IOException e) {
105            throw new DocFileIOException(file, DocFileIOException.Mode.WRITE,
106                e);
107        }
108    }
109}