001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 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.io.IOException;
024import java.util.Set;
025import java.util.concurrent.ConcurrentHashMap;
026
027import com.puppycrawl.tools.checkstyle.GlobalStatefulCheck;
028import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
029import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
030import com.puppycrawl.tools.checkstyle.api.FileText;
031
032/**
033 * Checks that all packages have a package documentation. See the documentation
034 * for more information.
035 */
036@GlobalStatefulCheck
037public class JavadocPackageCheck extends AbstractFileSetCheck {
038
039    /**
040     * A key is pointing to the warning message text in "messages.properties"
041     * file.
042     */
043    public static final String MSG_LEGACY_PACKAGE_HTML = "javadoc.legacyPackageHtml";
044
045    /**
046     * A key is pointing to the warning message text in "messages.properties"
047     * file.
048     */
049    public static final String MSG_PACKAGE_INFO = "javadoc.packageInfo";
050
051    /** The directories checked. */
052    private final Set<File> directoriesChecked = ConcurrentHashMap.newKeySet();
053
054    /** Indicates if allow legacy "package.html" file to be used. */
055    private boolean allowLegacy;
056
057    /**
058     * Creates a new instance.
059     */
060    public JavadocPackageCheck() {
061        // java, not html!
062        // The rule is: Every JAVA file should have a package.html sibling
063        setFileExtensions("java");
064    }
065
066    @Override
067    public void beginProcessing(String charset) {
068        super.beginProcessing(charset);
069        directoriesChecked.clear();
070    }
071
072    @Override
073    protected void processFiltered(File file, FileText fileText) throws CheckstyleException {
074        // Check if already processed directory
075        final File dir;
076        try {
077            dir = file.getCanonicalFile().getParentFile();
078        }
079        catch (IOException ex) {
080            throw new CheckstyleException(
081                    "Exception while getting canonical path to file " + file.getPath(), ex);
082        }
083        final boolean isDirChecked = !directoriesChecked.add(dir);
084        if (!isDirChecked) {
085            // Check for the preferred file.
086            final File packageInfo = new File(dir, "package-info.java");
087            final File packageHtml = new File(dir, "package.html");
088
089            if (packageInfo.exists()) {
090                if (packageHtml.exists()) {
091                    log(0, MSG_LEGACY_PACKAGE_HTML);
092                }
093            }
094            else if (!allowLegacy || !packageHtml.exists()) {
095                log(0, MSG_PACKAGE_INFO);
096            }
097        }
098    }
099
100    /**
101     * Indicates whether to allow support for the legacy <i>package.html</i>
102     * file.
103     * @param allowLegacy whether to allow support.
104     */
105    public void setAllowLegacy(boolean allowLegacy) {
106        this.allowLegacy = allowLegacy;
107    }
108
109}