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