001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
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////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.checks.javadoc;
020
021import com.google.common.collect.Sets;
022import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
023import java.io.File;
024import java.util.List;
025import java.util.Set;
026
027/**
028 * Checks that all packages have a package documentation. See the documentation
029 * for more information.
030 * @author Oliver Burn
031 */
032public class JavadocPackageCheck extends AbstractFileSetCheck
033{
034    /** Indicates if allow legacy "package.html" file to be used. */
035    private boolean allowLegacy;
036    /** The directories checked. */
037    private final Set<File> directoriesChecked = Sets.newHashSet();
038
039    /**
040     * Creates a new instance.
041     */
042    public JavadocPackageCheck()
043    {
044        // java, not html!
045        // The rule is: Every JAVA file should have a package.html sibling
046        setFileExtensions(new String[]{"java"});
047    }
048
049    @Override
050    public void beginProcessing(String charset)
051    {
052        super.beginProcessing(charset);
053        directoriesChecked.clear();
054    }
055
056    @Override
057    protected void processFiltered(File file, List<String> lines)
058    {
059        // Check if already processed directory
060        final File dir = file.getParentFile();
061        if (directoriesChecked.contains(dir)) {
062            return;
063        }
064        directoriesChecked.add(dir);
065
066        // Check for the preferred file.
067        final File packageInfo = new File(dir, "package-info.java");
068        final File packageHtml = new File(dir, "package.html");
069
070        if (packageInfo.exists()) {
071            if (packageHtml.exists()) {
072                log(0, "javadoc.legacyPackageHtml");
073            }
074        }
075        else if (!allowLegacy || !packageHtml.exists()) {
076            log(0, "javadoc.packageInfo");
077        }
078    }
079
080    /**
081     * Indicates whether to allow support for the legacy <i>package.html</i>
082     * file.
083     * @param allowLegacy whether to allow support.
084     */
085    public void setAllowLegacy(boolean allowLegacy)
086    {
087        this.allowLegacy = allowLegacy;
088    }
089}