1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 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 * Returns true if lineNo is header lines or false.
65 *
66 * @param lineNo a line number
67 * @return if {@code lineNo} is one of the ignored header lines.
68 */
69 private boolean isIgnoreLine(int lineNo) {
70 return ignoreLines.get(lineNo);
71 }
72
73 /**
74 * Checks if a code line matches the required header line.
75 *
76 * @param lineNumber the line number to check against the header
77 * @param line the line contents
78 * @return true if and only if the line matches the required header line
79 */
80 private boolean isMatch(int lineNumber, String line) {
81 // skip lines we are meant to ignore
82 return isIgnoreLine(lineNumber + 1)
83 || getHeaderLines().get(lineNumber).equals(line);
84 }
85
86 /**
87 * Setter to specifies the line numbers
88 * to ignore when matching lines in a content of headerFile.
89 *
90 * @param lines line numbers to ignore in header.
91 * @since 3.2
92 */
93 public void setIgnoreLines(int... lines) {
94 ignoreLines = TokenUtil.asBitSet(lines);
95 }
96
97 @Override
98 protected void processFiltered(File file, FileText fileText) {
99 if (getHeaderLines().size() > fileText.size()) {
100 log(1, MSG_MISSING);
101 }
102 else {
103 for (int i = 0; i < getHeaderLines().size(); i++) {
104 if (!isMatch(i, fileText.get(i))) {
105 log(i + 1, MSG_MISMATCH, getHeaderLines().get(i));
106 break;
107 }
108 }
109 }
110 }
111
112 @Override
113 protected void postProcessHeaderLines() {
114 // no code
115 }
116
117 }