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