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