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.header;
21  
22  import java.io.File;
23  import java.util.Arrays;
24  
25  import com.puppycrawl.tools.checkstyle.StatelessCheck;
26  import com.puppycrawl.tools.checkstyle.api.FileText;
27  
28  /**
29   * Checks the header of the source against a fixed header file.
30   * In default configuration,if header is not specified,
31   * the default value of header is set to null
32   * and the check does not rise any violations.
33   *
34   * @author Lars K├╝hne
35   */
36  @StatelessCheck
37  public class HeaderCheck extends AbstractHeaderCheck {
38  
39      /**
40       * A key is pointing to the warning message text in "messages.properties"
41       * file.
42       */
43      public static final String MSG_MISSING = "header.missing";
44  
45      /**
46       * A key is pointing to the warning message text in "messages.properties"
47       * file.
48       */
49      public static final String MSG_MISMATCH = "header.mismatch";
50  
51      /** Empty array to avoid instantiations. */
52      private static final int[] EMPTY_INT_ARRAY = new int[0];
53  
54      /** The header lines to ignore in the check, sorted. */
55      private int[] ignoreLines = EMPTY_INT_ARRAY;
56  
57      /**
58       * Returns true if lineNo is header lines or false.
59       * @param lineNo a line number
60       * @return if {@code lineNo} is one of the ignored header lines.
61       */
62      private boolean isIgnoreLine(int lineNo) {
63          return Arrays.binarySearch(ignoreLines, lineNo) >= 0;
64      }
65  
66      /**
67       * Checks if a code line matches the required header line.
68       * @param lineNumber the line number to check against the header
69       * @param line the line contents
70       * @return true if and only if the line matches the required header line
71       */
72      private boolean isMatch(int lineNumber, String line) {
73          // skip lines we are meant to ignore
74          return isIgnoreLine(lineNumber + 1)
75              || getHeaderLines().get(lineNumber).equals(line);
76      }
77  
78      /**
79       * Set the lines numbers to ignore in the header check.
80       * @param list comma separated list of line numbers to ignore in header.
81       */
82      public void setIgnoreLines(int... list) {
83          if (list.length == 0) {
84              ignoreLines = EMPTY_INT_ARRAY;
85          }
86          else {
87              ignoreLines = new int[list.length];
88              System.arraycopy(list, 0, ignoreLines, 0, list.length);
89              Arrays.sort(ignoreLines);
90          }
91      }
92  
93      @Override
94      protected void processFiltered(File file, FileText fileText) {
95          if (getHeaderLines().size() > fileText.size()) {
96              log(1, MSG_MISSING);
97          }
98          else {
99              for (int i = 0; i < getHeaderLines().size(); i++) {
100                 if (!isMatch(i, fileText.get(i))) {
101                     log(i + 1, MSG_MISMATCH, getHeaderLines().get(i));
102                     break;
103                 }
104             }
105         }
106     }
107 
108     @Override
109     protected void postProcessHeaderLines() {
110         // no code
111     }
112 }