001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2019 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 */ 031@StatelessCheck 032public class FileTabCharacterCheck extends AbstractFileSetCheck { 033 034 /** 035 * A key is pointing to the warning message text in "messages.properties" 036 * file. 037 */ 038 public static final String MSG_CONTAINS_TAB = "containsTab"; 039 040 /** 041 * A key is pointing to the warning message text in "messages.properties" 042 * file. 043 */ 044 public static final String MSG_FILE_CONTAINS_TAB = "file.containsTab"; 045 046 /** Indicates whether to report once per file, or for each line. */ 047 private boolean eachLine; 048 049 @Override 050 protected void processFiltered(File file, FileText fileText) { 051 int lineNum = 0; 052 for (int index = 0; index < fileText.size(); index++) { 053 final String line = fileText.get(index); 054 lineNum++; 055 final int tabPosition = line.indexOf('\t'); 056 if (tabPosition != -1) { 057 if (eachLine) { 058 log(lineNum, tabPosition + 1, MSG_CONTAINS_TAB); 059 } 060 else { 061 log(lineNum, tabPosition + 1, MSG_FILE_CONTAINS_TAB); 062 break; 063 } 064 } 065 } 066 } 067 068 /** 069 * Whether report on each line containing a tab. 070 * @param eachLine Whether report on each line containing a tab. 071 */ 072 public void setEachLine(boolean eachLine) { 073 this.eachLine = eachLine; 074 } 075 076}