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 * @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        final DetectorOptions options = DetectorOptions.newBuilder()
050            .reporter(this)
051            .compileFlags(0)
052            .format(format)
053            .message(message)
054            .minimum(minimum)
055            .maximum(maximum)
056            .ignoreCase(ignoreCase)
057            .build();
058        detector = new SinglelineDetector(options);
059    }
060
061    @Override
062    protected void processFiltered(File file, FileText fileText) {
063        detector.processLines(fileText);
064    }
065
066    /**
067     * Set the format of the regular expression to match.
068     * @param format the format of the regular expression to match.
069     */
070    public void setFormat(String format) {
071        this.format = format;
072    }
073
074    /**
075     * Set the message to report for a match.
076     * @param message the message to report for a match.
077     */
078    public void setMessage(String message) {
079        this.message = message;
080    }
081
082    /**
083     * Set the minimum number of matches required per file.
084     * @param minimum the minimum number of matches required per file.
085     */
086    public void setMinimum(int minimum) {
087        this.minimum = minimum;
088    }
089
090    /**
091     * Set the maximum number of matches required per file.
092     * @param maximum the maximum number of matches required per file.
093     */
094    public void setMaximum(int maximum) {
095        this.maximum = maximum;
096    }
097
098    /**
099     * Set whether to ignore case when matching.
100     * @param ignoreCase whether to ignore case when matching.
101     */
102    public void setIgnoreCase(boolean ignoreCase) {
103        this.ignoreCase = ignoreCase;
104    }
105
106}