001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 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 * <p>
030 * Checks that there are no tab characters ({@code '\t'}) in the source code.
031 * </p>
032 * <p>
033 * Rationale:
034 * </p>
035 * <ul>
036 * <li>
037 * Developers should not need to configure the tab width of their text editors in order
038 * to be able to read source code.
039 * </li>
040 * <li>
041 * From the Apache jakarta coding standards: In a distributed development environment,
042 * when the commit messages get sent to a mailing list, they are almost impossible to
043 * read if you use tabs.
044 * </li>
045 * </ul>
046 * <ul>
047 * <li>
048 * Property {@code eachLine} - Control whether to report on each line containing a tab,
049 * or just the first instance.
050 * Type is {@code boolean}.
051 * Default value is {@code false}.
052 * </li>
053 * <li>
054 * Property {@code fileExtensions} - Specify file type extension of files to process.
055 * Type is {@code java.lang.String[]}.
056 * Default value is {@code all files}.
057 * </li>
058 * </ul>
059 * <p>
060 * To configure the check to report on the first instance in each file:
061 * </p>
062 * <pre>
063 * &lt;module name=&quot;FileTabCharacter&quot;/&gt;
064 * </pre>
065 * <p>
066 * To configure the check to report on each line in each file:
067 * </p>
068 * <pre>
069 * &lt;module name=&quot;FileTabCharacter&quot;&gt;
070 *   &lt;property name=&quot;eachLine&quot; value=&quot;true&quot;/&gt;
071 * &lt;/module&gt;
072 * </pre>
073 * <p>
074 * Parent is {@code com.puppycrawl.tools.checkstyle.Checker}
075 * </p>
076 * <p>
077 * Violation Message Keys:
078 * </p>
079 * <ul>
080 * <li>
081 * {@code containsTab}
082 * </li>
083 * <li>
084 * {@code file.containsTab}
085 * </li>
086 * </ul>
087 *
088 * @since 5.0
089 */
090@StatelessCheck
091public class FileTabCharacterCheck extends AbstractFileSetCheck {
092
093    /**
094     * A key is pointing to the warning message text in "messages.properties"
095     * file.
096     */
097    public static final String MSG_CONTAINS_TAB = "containsTab";
098
099    /**
100     * A key is pointing to the warning message text in "messages.properties"
101     * file.
102     */
103    public static final String MSG_FILE_CONTAINS_TAB = "file.containsTab";
104
105    /** Control whether to report on each line containing a tab, or just the first instance. */
106    private boolean eachLine;
107
108    @Override
109    protected void processFiltered(File file, FileText fileText) {
110        int lineNum = 0;
111        for (int index = 0; index < fileText.size(); index++) {
112            final String line = fileText.get(index);
113            lineNum++;
114            final int tabPosition = line.indexOf('\t');
115            if (tabPosition != -1) {
116                if (eachLine) {
117                    log(lineNum, tabPosition, MSG_CONTAINS_TAB);
118                }
119                else {
120                    log(lineNum, tabPosition, MSG_FILE_CONTAINS_TAB);
121                    break;
122                }
123            }
124        }
125    }
126
127    /**
128     * Setter to control whether to report on each line containing a tab, or just the first
129     * instance.
130     *
131     * @param eachLine Whether report on each line containing a tab.
132     */
133    public void setEachLine(boolean eachLine) {
134        this.eachLine = eachLine;
135    }
136
137}