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