001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.javadoc;
021
022import java.io.File;
023import java.util.HashSet;
024import java.util.List;
025import java.util.Set;
026
027import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
028
029/**
030 * Checks that all packages have a package documentation. See the documentation
031 * for more information.
032 * @author Oliver Burn
033 */
034public class JavadocPackageCheck extends AbstractFileSetCheck {
035
036    /**
037     * A key is pointing to the warning message text in "messages.properties"
038     * file.
039     */
040    public static final String MSG_LEGACY_PACKAGE_HTML = "javadoc.legacyPackageHtml";
041
042    /**
043     * A key is pointing to the warning message text in "messages.properties"
044     * file.
045     */
046    public static final String MSG_PACKAGE_INFO = "javadoc.packageInfo";
047
048    /** The directories checked. */
049    private final Set<File> directoriesChecked = new HashSet<>();
050
051    /** Indicates if allow legacy "package.html" file to be used. */
052    private boolean allowLegacy;
053
054    /**
055     * Creates a new instance.
056     */
057    public JavadocPackageCheck() {
058        // java, not html!
059        // The rule is: Every JAVA file should have a package.html sibling
060        setFileExtensions("java");
061    }
062
063    @Override
064    public void beginProcessing(String charset) {
065        super.beginProcessing(charset);
066        directoriesChecked.clear();
067    }
068
069    @Override
070    protected void processFiltered(File file, List<String> lines) {
071        // Check if already processed directory
072        final File dir = file.getParentFile();
073        if (!directoriesChecked.contains(dir)) {
074            directoriesChecked.add(dir);
075
076            // Check for the preferred file.
077            final File packageInfo = new File(dir, "package-info.java");
078            final File packageHtml = new File(dir, "package.html");
079
080            if (packageInfo.exists()) {
081                if (packageHtml.exists()) {
082                    log(0, MSG_LEGACY_PACKAGE_HTML);
083                }
084            }
085            else if (!allowLegacy || !packageHtml.exists()) {
086                log(0, MSG_PACKAGE_INFO);
087            }
088        }
089    }
090
091    /**
092     * Indicates whether to allow support for the legacy <i>package.html</i>
093     * file.
094     * @param allowLegacy whether to allow support.
095     */
096    public void setAllowLegacy(boolean allowLegacy) {
097        this.allowLegacy = allowLegacy;
098    }
099}