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" 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 * Creates a new {@code HeaderCheck} instance. 065 */ 066 public HeaderCheck() { 067 // no code by default 068 } 069 070 /** 071 * Returns true if lineNo is header lines or false. 072 * 073 * @param lineNo a line number 074 * @return if {@code lineNo} is one of the ignored header lines. 075 */ 076 private boolean isIgnoreLine(int lineNo) { 077 return ignoreLines.get(lineNo); 078 } 079 080 /** 081 * Checks if a code line matches the required header line. 082 * 083 * @param lineNumber the line number to check against the header 084 * @param line the line contents 085 * @return true if and only if the line matches the required header line 086 */ 087 private boolean isMatch(int lineNumber, String line) { 088 // skip lines we are meant to ignore 089 return isIgnoreLine(lineNumber + 1) 090 || getHeaderLines().get(lineNumber).equals(line); 091 } 092 093 /** 094 * Setter to specifies the line numbers 095 * to ignore when matching lines in a content of headerFile. 096 * 097 * @param lines line numbers to ignore in header. 098 * @since 3.2 099 */ 100 public void setIgnoreLines(int... lines) { 101 ignoreLines = TokenUtil.asBitSet(lines); 102 } 103 104 @Override 105 protected void processFiltered(File file, FileText fileText) { 106 if (getHeaderLines().size() > fileText.size()) { 107 log(1, MSG_MISSING); 108 } 109 else { 110 for (int i = 0; i < getHeaderLines().size(); i++) { 111 if (!isMatch(i, fileText.get(i))) { 112 log(i + 1, MSG_MISMATCH, getHeaderLines().get(i)); 113 break; 114 } 115 } 116 } 117 } 118 119 @Override 120 protected void postProcessHeaderLines() { 121 // no code 122 } 123 124}