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 * <ul> 043 * <li> 044 * Property {@code charset} - Specify the character encoding to use when reading the headerFile. 045 * Type is {@code java.lang.String}. 046 * Default value is {@code the charset property of the parent 047 * <a href="https://checkstyle.org/config.html#Checker">Checker</a> module}. 048 * </li> 049 * <li> 050 * Property {@code fileExtensions} - Specify the file extensions of the files to process. 051 * Type is {@code java.lang.String[]}. 052 * Default value is {@code ""}. 053 * </li> 054 * <li> 055 * Property {@code header} - Specify the required header specified inline. 056 * Individual header lines must be separated by the string {@code "\n"} 057 * (even on platforms with a different line separator). 058 * Type is {@code java.lang.String}. 059 * Default value is {@code null}. 060 * </li> 061 * <li> 062 * Property {@code headerFile} - Specify the name of the file containing the required header. 063 * Type is {@code java.net.URI}. 064 * Default value is {@code null}. 065 * </li> 066 * <li> 067 * Property {@code ignoreLines} - Specifies the line 068 * numbers to ignore when matching lines in a content of headerFile. 069 * Type is {@code int[]}. 070 * Default value is {@code ""}. 071 * </li> 072 * </ul> 073 * 074 * <p> 075 * Parent is {@code com.puppycrawl.tools.checkstyle.Checker} 076 * </p> 077 * 078 * <p> 079 * Violation Message Keys: 080 * </p> 081 * <ul> 082 * <li> 083 * {@code header.mismatch} 084 * </li> 085 * <li> 086 * {@code header.missing} 087 * </li> 088 * </ul> 089 * 090 * @since 3.2 091 */ 092@StatelessCheck 093public class HeaderCheck extends AbstractHeaderCheck { 094 095 /** 096 * A key is pointing to the warning message text in "messages.properties" 097 * file. 098 */ 099 public static final String MSG_MISSING = "header.missing"; 100 101 /** 102 * A key is pointing to the warning message text in "messages.properties" 103 * file. 104 */ 105 public static final String MSG_MISMATCH = "header.mismatch"; 106 107 /** Specifies the line numbers to ignore when matching lines in a content of headerFile. */ 108 private BitSet ignoreLines = new BitSet(); 109 110 /** 111 * Returns true if lineNo is header lines or false. 112 * 113 * @param lineNo a line number 114 * @return if {@code lineNo} is one of the ignored header lines. 115 */ 116 private boolean isIgnoreLine(int lineNo) { 117 return ignoreLines.get(lineNo); 118 } 119 120 /** 121 * Checks if a code line matches the required header line. 122 * 123 * @param lineNumber the line number to check against the header 124 * @param line the line contents 125 * @return true if and only if the line matches the required header line 126 */ 127 private boolean isMatch(int lineNumber, String line) { 128 // skip lines we are meant to ignore 129 return isIgnoreLine(lineNumber + 1) 130 || getHeaderLines().get(lineNumber).equals(line); 131 } 132 133 /** 134 * Setter to specifies the line numbers 135 * to ignore when matching lines in a content of headerFile. 136 * 137 * @param lines line numbers to ignore in header. 138 * @since 3.2 139 */ 140 public void setIgnoreLines(int... lines) { 141 ignoreLines = TokenUtil.asBitSet(lines); 142 } 143 144 @Override 145 protected void processFiltered(File file, FileText fileText) { 146 if (getHeaderLines().size() > fileText.size()) { 147 log(1, MSG_MISSING); 148 } 149 else { 150 for (int i = 0; i < getHeaderLines().size(); i++) { 151 if (!isMatch(i, fileText.get(i))) { 152 log(i + 1, MSG_MISMATCH, getHeaderLines().get(i)); 153 break; 154 } 155 } 156 } 157 } 158 159 @Override 160 protected void postProcessHeaderLines() { 161 // no code 162 } 163 164}