001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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 * 048 * <p> 049 * Notes: 050 * When the {@code FileTabCharacter} check is used with the default configuration, 051 * only the first instance of a tab character is reported. 052 * </p> 053 * 054 * @since 5.0 055 */ 056@StatelessCheck 057public class FileTabCharacterCheck extends AbstractFileSetCheck { 058 059 /** 060 * A key is pointing to the warning message text in "messages.properties" file. 061 */ 062 public static final String MSG_CONTAINS_TAB = "containsTab"; 063 064 /** 065 * A key is pointing to the warning message text in "messages.properties" file. 066 */ 067 public static final String MSG_FILE_CONTAINS_TAB = "file.containsTab"; 068 069 /** Control whether to report on each line containing a tab, or just the first instance. */ 070 private boolean eachLine; 071 072 /** 073 * Creates a new {@code FileTabCharacterCheck} instance. 074 */ 075 public FileTabCharacterCheck() { 076 // no code by default 077 } 078 079 @Override 080 protected void processFiltered(File file, FileText fileText) { 081 int lineNum = 0; 082 for (int index = 0; index < fileText.size(); index++) { 083 final String line = fileText.get(index); 084 lineNum++; 085 final int tabPosition = line.indexOf('\t'); 086 if (tabPosition != -1) { 087 if (eachLine) { 088 log(lineNum, tabPosition, MSG_CONTAINS_TAB); 089 } 090 else { 091 log(lineNum, tabPosition, MSG_FILE_CONTAINS_TAB); 092 break; 093 } 094 } 095 } 096 } 097 098 /** 099 * Setter to control whether to report on each line containing a tab, or just the first 100 * instance. 101 * 102 * @param eachLine Whether report on each line containing a tab. 103 * @since 5.0 104 */ 105 public void setEachLine(boolean eachLine) { 106 this.eachLine = eachLine; 107 } 108 109}