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