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