View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.whitespace;
21  
22  import java.io.File;
23  
24  import com.puppycrawl.tools.checkstyle.StatelessCheck;
25  import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
26  import com.puppycrawl.tools.checkstyle.api.FileText;
27  
28  /**
29   * Checks to see if a file contains a tab character.
30   * @author oliverb
31   */
32  @StatelessCheck
33  public class FileTabCharacterCheck extends AbstractFileSetCheck {
34  
35      /**
36       * A key is pointing to the warning message text in "messages.properties"
37       * file.
38       */
39      public static final String MSG_CONTAINS_TAB = "containsTab";
40  
41      /**
42       * A key is pointing to the warning message text in "messages.properties"
43       * file.
44       */
45      public static final String MSG_FILE_CONTAINS_TAB = "file.containsTab";
46  
47      /** Indicates whether to report once per file, or for each line. */
48      private boolean eachLine;
49  
50      @Override
51      protected void processFiltered(File file, FileText fileText) {
52          int lineNum = 0;
53          for (int index = 0; index < fileText.size(); index++) {
54              final String line = fileText.get(index);
55              lineNum++;
56              final int tabPosition = line.indexOf('\t');
57              if (tabPosition != -1) {
58                  if (eachLine) {
59                      log(lineNum, tabPosition + 1, MSG_CONTAINS_TAB);
60                  }
61                  else {
62                      log(lineNum, tabPosition + 1, MSG_FILE_CONTAINS_TAB);
63                      break;
64                  }
65              }
66          }
67      }
68  
69      /**
70       * Whether report on each line containing a tab.
71       * @param eachLine Whether report on each line containing a tab.
72       */
73      public void setEachLine(boolean eachLine) {
74          this.eachLine = eachLine;
75      }
76  }