View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.BitSet;
24  
25  import com.puppycrawl.tools.checkstyle.StatelessCheck;
26  import com.puppycrawl.tools.checkstyle.api.FileText;
27  import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
28  
29  /**
30   * <div>
31   * Checks that a source file begins with a specified header.
32   * Property {@code headerFile} specifies a file that contains the required header.
33   * Alternatively, the header specification can be set directly in the
34   * {@code header} property without the need for an external file.
35   * </div>
36   *
37   * <p>
38   * Notes:
39   * In default configuration, if header is not specified, the default value
40   * of header is set to {@code null} and the check does not rise any violations.
41   * </p>
42   *
43   * @since 3.2
44   */
45  @StatelessCheck
46  public class HeaderCheck extends AbstractHeaderCheck {
47  
48      /**
49       * A key is pointing to the warning message text in "messages.properties" file.
50       */
51      public static final String MSG_MISSING = "header.missing";
52  
53      /**
54       * A key is pointing to the warning message text in "messages.properties" file.
55       */
56      public static final String MSG_MISMATCH = "header.mismatch";
57  
58      /** Specifies the line numbers to ignore when matching lines in a content of headerFile. */
59      private BitSet ignoreLines = new BitSet();
60  
61      /**
62       * Creates a new {@code HeaderCheck} instance.
63       */
64      public HeaderCheck() {
65          // no code by default
66      }
67  
68      /**
69       * Returns true if lineNo is header lines or false.
70       *
71       * @param lineNo a line number
72       * @return if {@code lineNo} is one of the ignored header lines.
73       */
74      private boolean isIgnoreLine(int lineNo) {
75          return ignoreLines.get(lineNo);
76      }
77  
78      /**
79       * Checks if a code line matches the required header line.
80       *
81       * @param lineNumber the line number to check against the header
82       * @param line the line contents
83       * @return true if and only if the line matches the required header line
84       */
85      private boolean isMatch(int lineNumber, String line) {
86          // skip lines we are meant to ignore
87          return isIgnoreLine(lineNumber + 1)
88              || getHeaderLines().get(lineNumber).equals(line);
89      }
90  
91      /**
92       * Setter to specifies the line numbers
93       * to ignore when matching lines in a content of headerFile.
94       *
95       * @param lines line numbers to ignore in header.
96       * @since 3.2
97       */
98      public void setIgnoreLines(int... lines) {
99          ignoreLines = TokenUtil.asBitSet(lines);
100     }
101 
102     @Override
103     protected void processFiltered(File file, FileText fileText) {
104         if (getHeaderLines().size() > fileText.size()) {
105             log(1, MSG_MISSING);
106         }
107         else {
108             for (int index = 0; index < getHeaderLines().size(); index++) {
109                 if (!isMatch(index, fileText.get(index))) {
110                     log(index + 1, MSG_MISMATCH, getHeaderLines().get(index));
111                     break;
112                 }
113             }
114         }
115     }
116 
117     @Override
118     protected void postProcessHeaderLines() {
119         // no code
120     }
121 
122 }