001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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.regexp;
021
022import java.io.File;
023import java.util.List;
024import java.util.regex.Pattern;
025
026import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
027import com.puppycrawl.tools.checkstyle.api.FileText;
028
029/**
030 * Implementation of a check that looks that matches across multiple lines in
031 * any file type.
032 * @author Oliver Burn
033 */
034public class RegexpMultilineCheck extends AbstractFileSetCheck {
035
036    /** The format of the regular expression to match. */
037    private String format = "$.";
038    /** The message to report for a match. */
039    private String message;
040    /** The minimum number of matches required per file. */
041    private int minimum;
042    /** The maximum number of matches required per file. */
043    private int maximum;
044    /** Whether to ignore case when matching. */
045    private boolean ignoreCase;
046
047    /** The detector to use. */
048    private MultilineDetector detector;
049
050    @Override
051    public void beginProcessing(String charset) {
052        super.beginProcessing(charset);
053        final DetectorOptions options = DetectorOptions.newBuilder()
054            .reporter(this)
055            .compileFlags(Pattern.MULTILINE)
056            .format(format)
057            .message(message)
058            .minimum(minimum)
059            .maximum(maximum)
060            .ignoreCase(ignoreCase)
061            .build();
062        detector = new MultilineDetector(options);
063    }
064
065    @Override
066    protected void processFiltered(File file, List<String> lines) {
067        detector.processLines(FileText.fromLines(file, lines));
068    }
069
070    /**
071     * Sets the format of the regular expression to match.
072     * @param format the format of the regular expression to match.
073     */
074    public void setFormat(String format) {
075        this.format = format;
076    }
077
078    /**
079     * Sets the message to report for a match.
080     * @param message the message to report for a match.
081     */
082    public void setMessage(String message) {
083        this.message = message;
084    }
085
086    /**
087     * Sets the minimum number of matches required per file.
088     * @param minimum the minimum number of matches required per file.
089     */
090    public void setMinimum(int minimum) {
091        this.minimum = minimum;
092    }
093
094    /**
095     * Sets the maximum number of matches required per file.
096     * @param maximum the maximum number of matches required per file.
097     */
098    public void setMaximum(int maximum) {
099        this.maximum = maximum;
100    }
101
102    /**
103     * Sets whether to ignore case when matching.
104     * @param ignoreCase whether to ignore case when matching.
105     */
106    public void setIgnoreCase(boolean ignoreCase) {
107        this.ignoreCase = ignoreCase;
108    }
109}