001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2025 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.header;
021
022import java.io.File;
023import java.util.BitSet;
024
025import com.puppycrawl.tools.checkstyle.StatelessCheck;
026import com.puppycrawl.tools.checkstyle.api.FileText;
027import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
028
029/**
030 * <div>
031 * Checks that a source file begins with a specified header.
032 * Property {@code headerFile} specifies a file that contains the required header.
033 * Alternatively, the header specification can be set directly in the
034 * {@code header} property without the need for an external file.
035 * </div>
036 *
037 * <p>
038 * In default configuration, if header is not specified, the default value
039 * of header is set to {@code null} and the check does not rise any violations.
040 * </p>
041 * <ul>
042 * <li>
043 * Property {@code charset} - Specify the character encoding to use when reading the headerFile.
044 * Type is {@code java.lang.String}.
045 * Default value is {@code the charset property of the parent
046 * <a href="https://checkstyle.org/config.html#Checker">Checker</a> module}.
047 * </li>
048 * <li>
049 * Property {@code fileExtensions} - Specify the file extensions of the files to process.
050 * Type is {@code java.lang.String[]}.
051 * Default value is {@code ""}.
052 * </li>
053 * <li>
054 * Property {@code header} - Specify the required header specified inline.
055 * Individual header lines must be separated by the string {@code "\n"}
056 * (even on platforms with a different line separator).
057 * Type is {@code java.lang.String}.
058 * Default value is {@code null}.
059 * </li>
060 * <li>
061 * Property {@code headerFile} - Specify the name of the file containing the required header.
062 * Type is {@code java.net.URI}.
063 * Default value is {@code null}.
064 * </li>
065 * <li>
066 * Property {@code ignoreLines} - Specifies the line
067 *           numbers to ignore when matching lines in a content of headerFile.
068 * Type is {@code int[]}.
069 * Default value is {@code ""}.
070 * </li>
071 * </ul>
072 *
073 * <p>
074 * Parent is {@code com.puppycrawl.tools.checkstyle.Checker}
075 * </p>
076 *
077 * <p>
078 * Violation Message Keys:
079 * </p>
080 * <ul>
081 * <li>
082 * {@code header.mismatch}
083 * </li>
084 * <li>
085 * {@code header.missing}
086 * </li>
087 * </ul>
088 *
089 * @since 6.9
090 */
091@StatelessCheck
092public class HeaderCheck extends AbstractHeaderCheck {
093
094    /**
095     * A key is pointing to the warning message text in "messages.properties"
096     * file.
097     */
098    public static final String MSG_MISSING = "header.missing";
099
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}