001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2019 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.ArrayList; 024import java.util.Arrays; 025import java.util.List; 026import java.util.regex.Pattern; 027import java.util.regex.PatternSyntaxException; 028 029import com.puppycrawl.tools.checkstyle.StatelessCheck; 030import com.puppycrawl.tools.checkstyle.api.FileText; 031import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 032 033/** 034 * <p> 035 * Checks the header of a source file against a header that contains a 036 * <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/regex/Pattern.html"> 037 * regular expression</a> for each line of the source header. 038 * </p> 039 * <p> 040 * Rationale: In some projects <a href="https://checkstyle.org/config_header.html#Header"> 041 * checking against a fixed header</a> is not sufficient, e.g. the header might 042 * require a copyright line where the year information is not static. 043 * </p> 044 * <p> 045 * For example, consider the following header: 046 * </p> 047 * <pre> 048 * line 1: ^/{71}$ 049 * line 2: ^// checkstyle:$ 050 * line 3: ^// Checks Java source code for adherence to a set of rules\.$ 051 * line 4: ^// Copyright \(C\) \d\d\d\d Oliver Burn$ 052 * line 5: ^// Last modification by \$Author.*\$$ 053 * line 6: ^/{71}$ 054 * line 7: 055 * line 8: ^package 056 * line 9: 057 * line 10: ^import 058 * line 11: 059 * line 12: ^/\*\* 060 * line 13: ^ \*([^/]|$) 061 * line 14: ^ \*/ 062 * </pre> 063 * <p> 064 * Lines 1 and 6 demonstrate a more compact notation for 71 '/' characters. 065 * Line 4 enforces that the copyright notice includes a four digit year. 066 * Line 5 is an example how to enforce revision control keywords in a file header. 067 * Lines 12-14 is a template for javadoc (line 13 is so complicated to remove 068 * conflict with and of javadoc comment). Lines 7, 9 and 11 will be treated 069 * as '^$' and will forcefully expect the line to be empty. 070 * </p> 071 * <p> 072 * Different programming languages have different comment syntax rules, 073 * but all of them start a comment with a non-word character. 074 * Hence you can often use the non-word character class to abstract away 075 * the concrete comment syntax and allow checking the header for different 076 * languages with a single header definition. For example, consider the following 077 * header specification (note that this is not the full Apache license header): 078 * </p> 079 * <pre> 080 * line 1: ^#! 081 * line 2: ^<\?xml.*>$ 082 * line 3: ^\W*$ 083 * line 4: ^\W*Copyright 2006 The Apache Software Foundation or its licensors, as applicable\.$ 084 * line 5: ^\W*Licensed under the Apache License, Version 2\.0 \(the "License"\);$ 085 * line 6: ^\W*$ 086 * </pre> 087 * <p> 088 * Lines 1 and 2 leave room for technical header lines, e.g. the "#!/bin/sh" 089 * line in Unix shell scripts, or the XML file header of XML files. 090 * Set the multiline property to "1, 2" so these lines can be ignored for 091 * file types where they do no apply. Lines 3 through 6 define the actual header content. 092 * Note how lines 2, 4 and 5 use escapes for characters that have special regexp semantics. 093 * </p> 094 * <p> 095 * In default configuration, if header is not specified, the default value 096 * of header is set to null and the check does not rise any violations. 097 * </p> 098 * <ul> 099 * <li> 100 * Property {@code headerFile} - Specify the name of the file containing the required header. 101 * Default value is {@code null}. 102 * </li> 103 * <li> 104 * Property {@code charset} - Specify the character encoding to use when reading the headerFile. 105 * Default value is the charset property of the parent 106 * <a href="https://checkstyle.org/config.html#Checker">Checker</a> module. 107 * </li> 108 * <li> 109 * Property {@code header} - Define the required header specified inline. 110 * Individual header lines must be separated by the string {@code "\n"} 111 * (even on platforms with a different line separator). 112 * For header lines containing {@code "\n\n"} checkstyle will 113 * forcefully expect an empty line to exist. See examples below. 114 * Regular expressions must not span multiple lines. 115 * Default value is {@code null}. 116 * </li> 117 * <li> 118 * Property {@code multiLines} - Specify the line numbers to repeat (zero or more times). 119 * Default value is {@code {}}. 120 * </li> 121 * <li> 122 * Property {@code fileExtensions} - Specify the file type extension of files to process. 123 * Default value is {@code all files}. 124 * </li> 125 * </ul> 126 * <p> 127 * In default configuration the check does not rise any violations. 128 * Default values of properties are used. 129 * </p> 130 * <pre> 131 * <module name="RegexpHeader"/> 132 * </pre> 133 * <p> 134 * To configure the check to use header file {@code "config/java.header"} and 135 * {@code 10} and {@code 13} multi-lines: 136 * </p> 137 * <pre> 138 * <module name="RegexpHeader"> 139 * <property name="headerFile" value="config/java.header"/> 140 * <property name="multiLines" value="10, 13"/> 141 * </module> 142 * </pre> 143 * <p> 144 * To configure the check to verify that each file starts with the header 145 * </p> 146 * <pre> 147 * ^// Copyright \(C\) (\d\d\d\d -)? 2004 MyCompany$ 148 * ^// All rights reserved$ 149 * </pre> 150 * <p> 151 * without the need for an external header file: 152 * </p> 153 * <pre> 154 * <module name="RegexpHeader"> 155 * <property 156 * name="header" 157 * value="^// Copyright \(C\) (\d\d\d\d -)? 2004 MyCompany$ 158 * \n^// All rights reserved$"/> 159 * </module> 160 * </pre> 161 * <p> 162 * For regex containing {@code "\n\n"} 163 * </p> 164 * <pre> 165 * <module name="RegexpHeader"> 166 * <property 167 * name="header" 168 * value="^package .*\n\n.*"/> 169 * </module> 170 * </pre> 171 * <p> 172 * {@code "\n\n"} will be treated as '^$' and will forcefully expect the line 173 * to be empty. For example - 174 * </p> 175 * <pre> 176 * package com.some.package; 177 * public class ThisWillFail { } 178 * </pre> 179 * <p> 180 * would fail for the regex above. Expected - 181 * </p> 182 * <pre> 183 * package com.some.package; 184 * 185 * public class ThisWillPass { } 186 * </pre> 187 * <p> 188 * <u>Note</u>: {@code ignoreLines} property has been removed from this check to simplify it. 189 * To make some line optional use "^.*$" regexp for this line. 190 * </p> 191 * 192 * @since 6.9 193 */ 194@StatelessCheck 195public class RegexpHeaderCheck extends AbstractHeaderCheck { 196 197 /** 198 * A key is pointing to the warning message text in "messages.properties" 199 * file. 200 */ 201 public static final String MSG_HEADER_MISSING = "header.missing"; 202 203 /** 204 * A key is pointing to the warning message text in "messages.properties" 205 * file. 206 */ 207 public static final String MSG_HEADER_MISMATCH = "header.mismatch"; 208 209 /** Empty array to avoid instantiations. */ 210 private static final int[] EMPTY_INT_ARRAY = new int[0]; 211 212 /** Regex pattern for a blank line. **/ 213 private static final String EMPTY_LINE_PATTERN = "^$"; 214 215 /** Compiled regex pattern for a blank line. **/ 216 private static final Pattern BLANK_LINE = Pattern.compile(EMPTY_LINE_PATTERN); 217 218 /** The compiled regular expressions. */ 219 private final List<Pattern> headerRegexps = new ArrayList<>(); 220 221 /** Specify the line numbers to repeat (zero or more times). */ 222 private int[] multiLines = EMPTY_INT_ARRAY; 223 224 /** 225 * Setter to specify the line numbers to repeat (zero or more times). 226 * 227 * @param list comma separated list of line numbers to repeat in header. 228 */ 229 public void setMultiLines(int... list) { 230 if (list.length == 0) { 231 multiLines = EMPTY_INT_ARRAY; 232 } 233 else { 234 multiLines = new int[list.length]; 235 System.arraycopy(list, 0, multiLines, 0, list.length); 236 Arrays.sort(multiLines); 237 } 238 } 239 240 @Override 241 protected void processFiltered(File file, FileText fileText) { 242 final int headerSize = getHeaderLines().size(); 243 final int fileSize = fileText.size(); 244 245 if (headerSize - multiLines.length > fileSize) { 246 log(1, MSG_HEADER_MISSING); 247 } 248 else { 249 int headerLineNo = 0; 250 int index; 251 for (index = 0; headerLineNo < headerSize && index < fileSize; index++) { 252 final String line = fileText.get(index); 253 boolean isMatch = isMatch(line, headerLineNo); 254 while (!isMatch && isMultiLine(headerLineNo)) { 255 headerLineNo++; 256 isMatch = headerLineNo == headerSize 257 || isMatch(line, headerLineNo); 258 } 259 if (!isMatch) { 260 log(index + 1, MSG_HEADER_MISMATCH, getHeaderLine(headerLineNo)); 261 break; 262 } 263 if (!isMultiLine(headerLineNo)) { 264 headerLineNo++; 265 } 266 } 267 if (index == fileSize) { 268 // if file finished, but we have at least one non-multi-line 269 // header isn't completed 270 logFirstSinglelineLine(headerLineNo, headerSize); 271 } 272 } 273 } 274 275 /** 276 * Returns the line from the header. Where the line is blank return the regexp pattern 277 * for a blank line. 278 * @param headerLineNo header line number to return 279 * @return the line from the header 280 */ 281 private String getHeaderLine(int headerLineNo) { 282 String line = getHeaderLines().get(headerLineNo); 283 if (line.isEmpty()) { 284 line = EMPTY_LINE_PATTERN; 285 } 286 return line; 287 } 288 289 /** 290 * Logs warning if any non-multiline lines left in header regexp. 291 * @param startHeaderLine header line number to start from 292 * @param headerSize whole header size 293 */ 294 private void logFirstSinglelineLine(int startHeaderLine, int headerSize) { 295 for (int lineNum = startHeaderLine; lineNum < headerSize; lineNum++) { 296 if (!isMultiLine(lineNum)) { 297 log(1, MSG_HEADER_MISSING); 298 break; 299 } 300 } 301 } 302 303 /** 304 * Checks if a code line matches the required header line. 305 * @param line the code line 306 * @param headerLineNo the header line number. 307 * @return true if and only if the line matches the required header line. 308 */ 309 private boolean isMatch(String line, int headerLineNo) { 310 return headerRegexps.get(headerLineNo).matcher(line).find(); 311 } 312 313 /** 314 * Returns true if line is multiline header lines or false. 315 * @param lineNo a line number 316 * @return if {@code lineNo} is one of the repeat header lines. 317 */ 318 private boolean isMultiLine(int lineNo) { 319 return Arrays.binarySearch(multiLines, lineNo + 1) >= 0; 320 } 321 322 @Override 323 protected void postProcessHeaderLines() { 324 final List<String> headerLines = getHeaderLines(); 325 for (String line : headerLines) { 326 try { 327 if (line.isEmpty()) { 328 headerRegexps.add(BLANK_LINE); 329 } 330 else { 331 headerRegexps.add(Pattern.compile(line)); 332 } 333 } 334 catch (final PatternSyntaxException ex) { 335 throw new IllegalArgumentException("line " 336 + (headerRegexps.size() + 1) 337 + " in header specification" 338 + " is not a regular expression", ex); 339 } 340 } 341 } 342 343 /** 344 * Setter to define the required header specified inline. 345 * Individual header lines must be separated by the string {@code "\n"} 346 * (even on platforms with a different line separator). 347 * For header lines containing {@code "\n\n"} checkstyle will forcefully 348 * expect an empty line to exist. See examples below. 349 * Regular expressions must not span multiple lines. 350 * 351 * @param header the header value to validate and set (in that order) 352 */ 353 @Override 354 public void setHeader(String header) { 355 if (!CommonUtil.isBlank(header)) { 356 if (!CommonUtil.isPatternValid(header)) { 357 throw new IllegalArgumentException("Unable to parse format: " + header); 358 } 359 super.setHeader(header); 360 } 361 } 362 363}