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////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.header;
021
022import java.util.Arrays;
023
024import java.io.File;
025import java.util.List;
026import java.util.regex.Pattern;
027import java.util.regex.PatternSyntaxException;
028
029import org.apache.commons.beanutils.ConversionException;
030
031import com.google.common.collect.Lists;
032import com.puppycrawl.tools.checkstyle.api.Utils;
033
034/**
035 * Checks the header of the source against a header file that contains a
036 * {@link java.util.regex.Pattern regular expression}
037 * for each line of the source header.
038 *
039 * @author Lars Kühne
040 * @author o_sukhodolsky
041 */
042public class RegexpHeaderCheck extends AbstractHeaderCheck
043{
044    /** empty array to avoid instantiations. */
045    private static final int[] EMPTY_INT_ARRAY = new int[0];
046
047    /** the compiled regular expressions */
048    private final List<Pattern> headerRegexps = Lists.newArrayList();
049
050    /** the header lines to repeat (0 or more) in the check, sorted. */
051    private int[] multiLines = EMPTY_INT_ARRAY;
052
053    /**
054     * Set the lines numbers to repeat in the header check.
055     * @param list comma separated list of line numbers to repeat in header.
056     */
057    public void setMultiLines(int[] list)
058    {
059        if ((list == null) || (list.length == 0)) {
060            multiLines = EMPTY_INT_ARRAY;
061            return;
062        }
063
064        multiLines = new int[list.length];
065        System.arraycopy(list, 0, multiLines, 0, list.length);
066        Arrays.sort(multiLines);
067    }
068
069    @Override
070    protected void processFiltered(File file, List<String> lines)
071    {
072        final int headerSize = getHeaderLines().size();
073        final int fileSize = lines.size();
074
075        if (headerSize - multiLines.length > fileSize) {
076            log(1, "header.missing");
077        }
078        else {
079            int headerLineNo = 0;
080            int i;
081            for (i = 0; (headerLineNo < headerSize) && (i < fileSize); i++) {
082                final String line = lines.get(i);
083                boolean isMatch = isMatch(line, headerLineNo);
084                while (!isMatch && isMultiLine(headerLineNo)) {
085                    headerLineNo++;
086                    isMatch = (headerLineNo == headerSize)
087                            || isMatch(line, headerLineNo);
088                }
089                if (!isMatch) {
090                    log(i + 1, "header.mismatch", getHeaderLines().get(
091                            headerLineNo));
092                    break; // stop checking
093                }
094                if (!isMultiLine(headerLineNo)) {
095                    headerLineNo++;
096                }
097            }
098            if (i == fileSize) {
099                // if file finished, but we have at least one non-multi-line
100                // header isn't completed
101                for (; headerLineNo < headerSize; headerLineNo++) {
102                    if (!isMultiLine(headerLineNo)) {
103                        log(1, "header.missing");
104                        break;
105                    }
106                }
107            }
108        }
109    }
110
111    /**
112     * Checks if a code line matches the required header line.
113     * @param line the code line
114     * @param headerLineNo the header line number.
115     * @return true if and only if the line matches the required header line.
116     */
117    private boolean isMatch(String line, int headerLineNo)
118    {
119        return headerRegexps.get(headerLineNo).matcher(line).find();
120    }
121
122    /**
123     * @param lineNo a line number
124     * @return if <code>lineNo</code> is one of the repeat header lines.
125     */
126    private boolean isMultiLine(int lineNo)
127    {
128        return (Arrays.binarySearch(multiLines, lineNo + 1) >= 0);
129    }
130
131    @Override
132    protected void postprocessHeaderLines()
133    {
134        final List<String> headerLines = getHeaderLines();
135        headerRegexps.clear();
136        for (String line : headerLines) {
137            try {
138                // TODO: Not sure if cache in Utils is still necessary
139                headerRegexps.add(Utils.getPattern(line));
140            }
141            catch (final PatternSyntaxException ex) {
142                throw new ConversionException("line "
143                        + (headerRegexps.size() + 1)
144                        + " in header specification"
145                        + " is not a regular expression");
146            }
147        }
148    }
149
150}