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 * 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" 050 * file. 051 */ 052 public static final String MSG_MISSING = "header.missing"; 053 054 /** 055 * A key is pointing to the warning message text in "messages.properties" 056 * file. 057 */ 058 public static final String MSG_MISMATCH = "header.mismatch"; 059 060 /** Specifies the line numbers to ignore when matching lines in a content of headerFile. */ 061 private BitSet ignoreLines = new BitSet(); 062 063 /** 064 * Returns true if lineNo is header lines or false. 065 * 066 * @param lineNo a line number 067 * @return if {@code lineNo} is one of the ignored header lines. 068 */ 069 private boolean isIgnoreLine(int lineNo) { 070 return ignoreLines.get(lineNo); 071 } 072 073 /** 074 * Checks if a code line matches the required header line. 075 * 076 * @param lineNumber the line number to check against the header 077 * @param line the line contents 078 * @return true if and only if the line matches the required header line 079 */ 080 private boolean isMatch(int lineNumber, String line) { 081 // skip lines we are meant to ignore 082 return isIgnoreLine(lineNumber + 1) 083 || getHeaderLines().get(lineNumber).equals(line); 084 } 085 086 /** 087 * Setter to specifies the line numbers 088 * to ignore when matching lines in a content of headerFile. 089 * 090 * @param lines line numbers to ignore in header. 091 * @since 3.2 092 */ 093 public void setIgnoreLines(int... lines) { 094 ignoreLines = TokenUtil.asBitSet(lines); 095 } 096 097 @Override 098 protected void processFiltered(File file, FileText fileText) { 099 if (getHeaderLines().size() > fileText.size()) { 100 log(1, MSG_MISSING); 101 } 102 else { 103 for (int i = 0; i < getHeaderLines().size(); i++) { 104 if (!isMatch(i, fileText.get(i))) { 105 log(i + 1, MSG_MISMATCH, getHeaderLines().get(i)); 106 break; 107 } 108 } 109 } 110 } 111 112 @Override 113 protected void postProcessHeaderLines() { 114 // no code 115 } 116 117}