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