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