001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2014 Oliver Burn 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//////////////////////////////////////////////////////////////////////////////// 019package com.puppycrawl.tools.checkstyle.checks.header; 020 021import java.io.File; 022import java.util.Arrays; 023import java.util.List; 024 025/** 026 * Checks the header of the source against a fixed header file. 027 * 028 * @author Lars Kühne 029 */ 030public class HeaderCheck extends AbstractHeaderCheck 031{ 032 /** empty array to avoid instantiations. */ 033 private static final int[] EMPTY_INT_ARRAY = new int[0]; 034 035 /** the header lines to ignore in the check, sorted. */ 036 private int[] ignoreLines = EMPTY_INT_ARRAY; 037 038 /** 039 * @param lineNo a line number 040 * @return if <code>lineNo</code> is one of the ignored header lines. 041 */ 042 private boolean isIgnoreLine(int lineNo) 043 { 044 return (Arrays.binarySearch(ignoreLines, lineNo) >= 0); 045 } 046 047 /** 048 * Checks if a code line matches the required header line. 049 * @param lineNumber the line number to check against the header 050 * @param line the line contents 051 * @return true if and only if the line matches the required header line 052 */ 053 protected boolean isMatch(int lineNumber, String line) 054 { 055 // skip lines we are meant to ignore 056 return isIgnoreLine(lineNumber + 1) 057 || getHeaderLines().get(lineNumber).equals(line); 058 } 059 060 /** 061 * Set the lines numbers to ignore in the header check. 062 * @param list comma separated list of line numbers to ignore in header. 063 */ 064 public void setIgnoreLines(int[] list) 065 { 066 if ((list == null) || (list.length == 0)) { 067 ignoreLines = EMPTY_INT_ARRAY; 068 return; 069 } 070 071 ignoreLines = new int[list.length]; 072 System.arraycopy(list, 0, ignoreLines, 0, list.length); 073 Arrays.sort(ignoreLines); 074 } 075 076 @Override 077 protected void processFiltered(File file, List<String> lines) 078 { 079 if (getHeaderLines().size() > lines.size()) { 080 log(1, "header.missing"); 081 } 082 else { 083 for (int i = 0; i < getHeaderLines().size(); i++) { 084 if (!isMatch(i, lines.get(i))) { 085 log(i + 1, "header.mismatch", getHeaderLines().get(i)); 086 break; // stop checking 087 } 088 } 089 } 090 } 091}