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[] mIgnoreLines = EMPTY_INT_ARRAY;
037
038    /**
039     * @param aLineNo a line number
040     * @return if <code>aLineNo</code> is one of the ignored header lines.
041     */
042    private boolean isIgnoreLine(int aLineNo)
043    {
044        return (Arrays.binarySearch(mIgnoreLines, aLineNo) >= 0);
045    }
046
047    /**
048     * Checks if a code line matches the required header line.
049     * @param aLineNumber the line number to check against the header
050     * @param aLine the line contents
051     * @return true if and only if the line matches the required header line
052     */
053    protected boolean isMatch(int aLineNumber, String aLine)
054    {
055        // skip lines we are meant to ignore
056        return isIgnoreLine(aLineNumber + 1)
057            || getHeaderLines().get(aLineNumber).equals(aLine);
058    }
059
060    /**
061     * Set the lines numbers to ignore in the header check.
062     * @param aList comma separated list of line numbers to ignore in header.
063     */
064    public void setIgnoreLines(int[] aList)
065    {
066        if ((aList == null) || (aList.length == 0)) {
067            mIgnoreLines = EMPTY_INT_ARRAY;
068            return;
069        }
070
071        mIgnoreLines = new int[aList.length];
072        System.arraycopy(aList, 0, mIgnoreLines, 0, aList.length);
073        Arrays.sort(mIgnoreLines);
074    }
075
076    @Override
077    protected void processFiltered(File aFile, List<String> aLines)
078    {
079        if (getHeaderLines().size() > aLines.size()) {
080            log(1, "header.missing");
081        }
082        else {
083            for (int i = 0; i < getHeaderLines().size(); i++) {
084                if (!isMatch(i, aLines.get(i))) {
085                    log(i + 1, "header.mismatch", getHeaderLines().get(i));
086                    break; // stop checking
087                }
088            }
089        }
090    }
091}