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