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.whitespace;
021
022import java.io.File;
023import java.util.List;
024
025import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
026
027/**
028 * Checks to see if a file contains a tab character.
029 * @author oliverb
030 */
031public class FileTabCharacterCheck extends AbstractFileSetCheck {
032
033    /**
034     * A key is pointing to the warning message text in "messages.properties"
035     * file.
036     */
037    public static final String MSG_CONTAINS_TAB = "containsTab";
038
039    /**
040     * A key is pointing to the warning message text in "messages.properties"
041     * file.
042     */
043    public static final String MSG_FILE_CONTAINS_TAB = "file.containsTab";
044
045    /** Indicates whether to report once per file, or for each line. */
046    private boolean eachLine;
047
048    @Override
049    protected void processFiltered(File file, List<String> lines) {
050        int lineNum = 0;
051        for (final String line : lines) {
052            lineNum++;
053            final int tabPosition = line.indexOf('\t');
054            if (tabPosition != -1) {
055                if (eachLine) {
056                    log(lineNum, tabPosition + 1, MSG_CONTAINS_TAB);
057                }
058                else {
059                    log(lineNum, tabPosition + 1, MSG_FILE_CONTAINS_TAB);
060                    break;
061                }
062            }
063        }
064    }
065
066    /**
067     * Whether report on each line containing a tab.
068     * @param eachLine Whether report on each line containing a tab.
069     */
070    public void setEachLine(boolean eachLine) {
071        this.eachLine = eachLine;
072    }
073}