001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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 * Notes:
039 * In default configuration, if header is not specified, the default value
040 * of header is set to {@code null} and the check does not rise any violations.
041 * </p>
042 *
043 * @since 3.2
044 */
045@StatelessCheck
046public class HeaderCheck extends AbstractHeaderCheck {
047
048    /**
049     * A key is pointing to the warning message text in "messages.properties" file.
050     */
051    public static final String MSG_MISSING = "header.missing";
052
053    /**
054     * A key is pointing to the warning message text in "messages.properties" file.
055     */
056    public static final String MSG_MISMATCH = "header.mismatch";
057
058    /** Specifies the line numbers to ignore when matching lines in a content of headerFile. */
059    private BitSet ignoreLines = new BitSet();
060
061    /**
062     * Creates a new {@code HeaderCheck} instance.
063     */
064    public HeaderCheck() {
065        // no code by default
066    }
067
068    /**
069     * Returns true if lineNo is header lines or false.
070     *
071     * @param lineNo a line number
072     * @return if {@code lineNo} is one of the ignored header lines.
073     */
074    private boolean isIgnoreLine(int lineNo) {
075        return ignoreLines.get(lineNo);
076    }
077
078    /**
079     * Checks if a code line matches the required header line.
080     *
081     * @param lineNumber the line number to check against the header
082     * @param line the line contents
083     * @return true if and only if the line matches the required header line
084     */
085    private boolean isMatch(int lineNumber, String line) {
086        // skip lines we are meant to ignore
087        return isIgnoreLine(lineNumber + 1)
088            || getHeaderLines().get(lineNumber).equals(line);
089    }
090
091    /**
092     * Setter to specifies the line numbers
093     * to ignore when matching lines in a content of headerFile.
094     *
095     * @param lines line numbers to ignore in header.
096     * @since 3.2
097     */
098    public void setIgnoreLines(int... lines) {
099        ignoreLines = TokenUtil.asBitSet(lines);
100    }
101
102    @Override
103    protected void processFiltered(File file, FileText fileText) {
104        if (getHeaderLines().size() > fileText.size()) {
105            log(1, MSG_MISSING);
106        }
107        else {
108            for (int index = 0; index < getHeaderLines().size(); index++) {
109                if (!isMatch(index, fileText.get(index))) {
110                    log(index + 1, MSG_MISMATCH, getHeaderLines().get(index));
111                    break;
112                }
113            }
114        }
115    }
116
117    @Override
118    protected void postProcessHeaderLines() {
119        // no code
120    }
121
122}