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"
50       * file.
51       */
52      public static final String MSG_MISSING = "header.missing";
53  
54      /**
55       * A key is pointing to the warning message text in "messages.properties"
56       * file.
57       */
58      public static final String MSG_MISMATCH = "header.mismatch";
59  
60      /** Specifies the line numbers to ignore when matching lines in a content of headerFile. */
61      private BitSet ignoreLines = new BitSet();
62  
63      /**
64       * Creates a new {@code HeaderCheck} instance.
65       */
66      public HeaderCheck() {
67          // no code by default
68      }
69  
70      /**
71       * Returns true if lineNo is header lines or false.
72       *
73       * @param lineNo a line number
74       * @return if {@code lineNo} is one of the ignored header lines.
75       */
76      private boolean isIgnoreLine(int lineNo) {
77          return ignoreLines.get(lineNo);
78      }
79  
80      /**
81       * Checks if a code line matches the required header line.
82       *
83       * @param lineNumber the line number to check against the header
84       * @param line the line contents
85       * @return true if and only if the line matches the required header line
86       */
87      private boolean isMatch(int lineNumber, String line) {
88          // skip lines we are meant to ignore
89          return isIgnoreLine(lineNumber + 1)
90              || getHeaderLines().get(lineNumber).equals(line);
91      }
92  
93      /**
94       * Setter to specifies the line numbers
95       * to ignore when matching lines in a content of headerFile.
96       *
97       * @param lines line numbers to ignore in header.
98       * @since 3.2
99       */
100     public void setIgnoreLines(int... lines) {
101         ignoreLines = TokenUtil.asBitSet(lines);
102     }
103 
104     @Override
105     protected void processFiltered(File file, FileText fileText) {
106         if (getHeaderLines().size() > fileText.size()) {
107             log(1, MSG_MISSING);
108         }
109         else {
110             for (int i = 0; i < getHeaderLines().size(); i++) {
111                 if (!isMatch(i, fileText.get(i))) {
112                     log(i + 1, MSG_MISMATCH, getHeaderLines().get(i));
113                     break;
114                 }
115             }
116         }
117     }
118 
119     @Override
120     protected void postProcessHeaderLines() {
121         // no code
122     }
123 
124 }