View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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   * Property {@code ignoreLines} specifies the line numbers to ignore when matching
39   * lines in a header file. This property is very useful for supporting headers
40   * that contain copyright dates. For example, consider the following header:
41   * </p>
42   * <pre>
43   * line 1: ////////////////////////////////////////////////////////////////////
44   * line 2: // checkstyle:
45   * line 3: // Checks Java source code for adherence to a set of rules.
46   * line 4: // Copyright (C) 2002  Oliver Burn
47   * line 5: ////////////////////////////////////////////////////////////////////
48   * </pre>
49   *
50   * <p>
51   * Since the year information will change over time, you can tell Checkstyle
52   * to ignore line 4 by setting property {@code ignoreLines} to {@code 4}.
53   * </p>
54   *
55   * <p>
56   * In default configuration, if header is not specified, the default value
57   * of header is set to {@code null} and the check does not rise any violations.
58   * </p>
59   * <ul>
60   * <li>
61   * Property {@code charset} - Specify the character encoding to use when reading the headerFile.
62   * Type is {@code java.lang.String}.
63   * Default value is {@code the charset property of the parent
64   * <a href="https://checkstyle.org/config.html#Checker">Checker</a> module}.
65   * </li>
66   * <li>
67   * Property {@code fileExtensions} - Specify the file extensions of the files to process.
68   * Type is {@code java.lang.String[]}.
69   * Default value is {@code ""}.
70   * </li>
71   * <li>
72   * Property {@code header} - Specify the required header specified inline.
73   * Individual header lines must be separated by the string {@code "\n"}
74   * (even on platforms with a different line separator).
75   * Type is {@code java.lang.String}.
76   * Default value is {@code null}.
77   * </li>
78   * <li>
79   * Property {@code headerFile} - Specify the name of the file containing the required header.
80   * Type is {@code java.net.URI}.
81   * Default value is {@code null}.
82   * </li>
83   * <li>
84   * Property {@code ignoreLines} - Specify the line numbers to ignore.
85   * Type is {@code int[]}.
86   * Default value is {@code ""}.
87   * </li>
88   * </ul>
89   *
90   * <p>
91   * Parent is {@code com.puppycrawl.tools.checkstyle.Checker}
92   * </p>
93   *
94   * <p>
95   * Violation Message Keys:
96   * </p>
97   * <ul>
98   * <li>
99   * {@code header.mismatch}
100  * </li>
101  * <li>
102  * {@code header.missing}
103  * </li>
104  * </ul>
105  *
106  * @since 6.9
107  */
108 @StatelessCheck
109 public class HeaderCheck extends AbstractHeaderCheck {
110 
111     /**
112      * A key is pointing to the warning message text in "messages.properties"
113      * file.
114      */
115     public static final String MSG_MISSING = "header.missing";
116 
117     /**
118      * A key is pointing to the warning message text in "messages.properties"
119      * file.
120      */
121     public static final String MSG_MISMATCH = "header.mismatch";
122 
123     /** Specify the line numbers to ignore. */
124     private BitSet ignoreLines = new BitSet();
125 
126     /**
127      * Returns true if lineNo is header lines or false.
128      *
129      * @param lineNo a line number
130      * @return if {@code lineNo} is one of the ignored header lines.
131      */
132     private boolean isIgnoreLine(int lineNo) {
133         return ignoreLines.get(lineNo);
134     }
135 
136     /**
137      * Checks if a code line matches the required header line.
138      *
139      * @param lineNumber the line number to check against the header
140      * @param line the line contents
141      * @return true if and only if the line matches the required header line
142      */
143     private boolean isMatch(int lineNumber, String line) {
144         // skip lines we are meant to ignore
145         return isIgnoreLine(lineNumber + 1)
146             || getHeaderLines().get(lineNumber).equals(line);
147     }
148 
149     /**
150      * Setter to specify the line numbers to ignore.
151      *
152      * @param lines line numbers to ignore in header.
153      * @since 3.2
154      */
155     public void setIgnoreLines(int... lines) {
156         ignoreLines = TokenUtil.asBitSet(lines);
157     }
158 
159     @Override
160     protected void processFiltered(File file, FileText fileText) {
161         if (getHeaderLines().size() > fileText.size()) {
162             log(1, MSG_MISSING);
163         }
164         else {
165             for (int i = 0; i < getHeaderLines().size(); i++) {
166                 if (!isMatch(i, fileText.get(i))) {
167                     log(i + 1, MSG_MISMATCH, getHeaderLines().get(i));
168                     break;
169                 }
170             }
171         }
172     }
173 
174     @Override
175     protected void postProcessHeaderLines() {
176         // no code
177     }
178 
179 }