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