| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| HeaderCheck |
|
| 2.75;2.75 |
| 1 | //////////////////////////////////////////////////////////////////////////////// | |
| 2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
| 3 | // Copyright (C) 2001-2014 Oliver Burn | |
| 4 | // | |
| 5 | // This library is free software; you can redistribute it and/or | |
| 6 | // modify it under the terms of the GNU Lesser General Public | |
| 7 | // License as published by the Free Software Foundation; either | |
| 8 | // version 2.1 of the License, or (at your option) any later version. | |
| 9 | // | |
| 10 | // This library is distributed in the hope that it will be useful, | |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 | // Lesser General Public License for more details. | |
| 14 | // | |
| 15 | // You should have received a copy of the GNU Lesser General Public | |
| 16 | // License along with this library; if not, write to the Free Software | |
| 17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 18 | //////////////////////////////////////////////////////////////////////////////// | |
| 19 | package com.puppycrawl.tools.checkstyle.checks.header; | |
| 20 | ||
| 21 | import java.io.File; | |
| 22 | import java.util.Arrays; | |
| 23 | import java.util.List; | |
| 24 | ||
| 25 | /** | |
| 26 | * Checks the header of the source against a fixed header file. | |
| 27 | * | |
| 28 | * @author Lars Kühne | |
| 29 | */ | |
| 30 | 5 | public class HeaderCheck extends AbstractHeaderCheck |
| 31 | { | |
| 32 | /** empty array to avoid instantiations. */ | |
| 33 | 1 | private static final int[] EMPTY_INT_ARRAY = new int[0]; |
| 34 | ||
| 35 | /** the header lines to ignore in the check, sorted. */ | |
| 36 | 5 | private int[] mIgnoreLines = EMPTY_INT_ARRAY; |
| 37 | ||
| 38 | /** | |
| 39 | * @param aLineNo a line number | |
| 40 | * @return if <code>aLineNo</code> is one of the ignored header lines. | |
| 41 | */ | |
| 42 | private boolean isIgnoreLine(int aLineNo) | |
| 43 | { | |
| 44 | 0 | return (Arrays.binarySearch(mIgnoreLines, aLineNo) >= 0); |
| 45 | } | |
| 46 | ||
| 47 | /** | |
| 48 | * Checks if a code line matches the required header line. | |
| 49 | * @param aLineNumber the line number to check against the header | |
| 50 | * @param aLine the line contents | |
| 51 | * @return true if and only if the line matches the required header line | |
| 52 | */ | |
| 53 | protected boolean isMatch(int aLineNumber, String aLine) | |
| 54 | { | |
| 55 | // skip lines we are meant to ignore | |
| 56 | 0 | return isIgnoreLine(aLineNumber + 1) |
| 57 | || getHeaderLines().get(aLineNumber).equals(aLine); | |
| 58 | } | |
| 59 | ||
| 60 | /** | |
| 61 | * Set the lines numbers to ignore in the header check. | |
| 62 | * @param aList comma separated list of line numbers to ignore in header. | |
| 63 | */ | |
| 64 | public void setIgnoreLines(int[] aList) | |
| 65 | { | |
| 66 | 1 | if ((aList == null) || (aList.length == 0)) { |
| 67 | 1 | mIgnoreLines = EMPTY_INT_ARRAY; |
| 68 | 1 | return; |
| 69 | } | |
| 70 | ||
| 71 | 0 | mIgnoreLines = new int[aList.length]; |
| 72 | 0 | System.arraycopy(aList, 0, mIgnoreLines, 0, aList.length); |
| 73 | 0 | Arrays.sort(mIgnoreLines); |
| 74 | 0 | } |
| 75 | ||
| 76 | @Override | |
| 77 | protected void processFiltered(File aFile, List<String> aLines) | |
| 78 | { | |
| 79 | 1 | if (getHeaderLines().size() > aLines.size()) { |
| 80 | 1 | log(1, "header.missing"); |
| 81 | } | |
| 82 | else { | |
| 83 | 0 | for (int i = 0; i < getHeaderLines().size(); i++) { |
| 84 | 0 | if (!isMatch(i, aLines.get(i))) { |
| 85 | 0 | log(i + 1, "header.mismatch", getHeaderLines().get(i)); |
| 86 | 0 | break; // stop checking |
| 87 | } | |
| 88 | } | |
| 89 | } | |
| 90 | 1 | } |
| 91 | } |