001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 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.Arrays; 024 025import com.puppycrawl.tools.checkstyle.StatelessCheck; 026import com.puppycrawl.tools.checkstyle.api.FileText; 027 028/** 029 * Checks the header of the source against a fixed header file. 030 * In default configuration,if header is not specified, 031 * the default value of header is set to null 032 * and the check does not rise any violations. 033 * 034 * @author Lars Kühne 035 */ 036@StatelessCheck 037public class HeaderCheck extends AbstractHeaderCheck { 038 039 /** 040 * A key is pointing to the warning message text in "messages.properties" 041 * file. 042 */ 043 public static final String MSG_MISSING = "header.missing"; 044 045 /** 046 * A key is pointing to the warning message text in "messages.properties" 047 * file. 048 */ 049 public static final String MSG_MISMATCH = "header.mismatch"; 050 051 /** Empty array to avoid instantiations. */ 052 private static final int[] EMPTY_INT_ARRAY = new int[0]; 053 054 /** The header lines to ignore in the check, sorted. */ 055 private int[] ignoreLines = EMPTY_INT_ARRAY; 056 057 /** 058 * Returns true if lineNo is header lines or false. 059 * @param lineNo a line number 060 * @return if {@code lineNo} is one of the ignored header lines. 061 */ 062 private boolean isIgnoreLine(int lineNo) { 063 return Arrays.binarySearch(ignoreLines, lineNo) >= 0; 064 } 065 066 /** 067 * Checks if a code line matches the required header line. 068 * @param lineNumber the line number to check against the header 069 * @param line the line contents 070 * @return true if and only if the line matches the required header line 071 */ 072 private boolean isMatch(int lineNumber, String line) { 073 // skip lines we are meant to ignore 074 return isIgnoreLine(lineNumber + 1) 075 || getHeaderLines().get(lineNumber).equals(line); 076 } 077 078 /** 079 * Set the lines numbers to ignore in the header check. 080 * @param list comma separated list of line numbers to ignore in header. 081 */ 082 public void setIgnoreLines(int... list) { 083 if (list.length == 0) { 084 ignoreLines = EMPTY_INT_ARRAY; 085 } 086 else { 087 ignoreLines = new int[list.length]; 088 System.arraycopy(list, 0, ignoreLines, 0, list.length); 089 Arrays.sort(ignoreLines); 090 } 091 } 092 093 @Override 094 protected void processFiltered(File file, FileText fileText) { 095 if (getHeaderLines().size() > fileText.size()) { 096 log(1, MSG_MISSING); 097 } 098 else { 099 for (int i = 0; i < getHeaderLines().size(); i++) { 100 if (!isMatch(i, fileText.get(i))) { 101 log(i + 1, MSG_MISMATCH, getHeaderLines().get(i)); 102 break; 103 } 104 } 105 } 106 } 107 108 @Override 109 protected void postProcessHeaderLines() { 110 // no code 111 } 112 113}