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