001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2025 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.PropertyType;
025import com.puppycrawl.tools.checkstyle.StatelessCheck;
026import com.puppycrawl.tools.checkstyle.XdocsPropertyType;
027import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
028import com.puppycrawl.tools.checkstyle.api.FileText;
029
030/**
031 * <div>
032 * Checks that a specified pattern matches a single-line in any file type.
033 * </div>
034 *
035 * <p>
036 * Rationale: This check can be used to prototype checks and to find common bad
037 * practice such as calling {@code ex.printStacktrace()},
038 * {@code System.out.println()}, {@code System.exit()}, etc.
039 * </p>
040 *
041 * @since 5.0
042 */
043@StatelessCheck
044public class RegexpSinglelineCheck extends AbstractFileSetCheck {
045
046    /** Specify the format of the regular expression to match. */
047    @XdocsPropertyType(PropertyType.PATTERN)
048    private String format = "$.";
049    /**
050     * Specify the message which is used to notify about violations,
051     * if empty then default (hard-coded) message is used.
052     */
053    private String message;
054    /** Specify the minimum number of matches required in each file. */
055    private int minimum;
056    /** Specify the maximum number of matches required in each file. */
057    private int maximum;
058    /** Control whether to ignore case when searching. */
059    private boolean ignoreCase;
060
061    /** The detector to use. */
062    private SinglelineDetector detector;
063
064    @Override
065    public void beginProcessing(String charset) {
066        final DetectorOptions options = DetectorOptions.newBuilder()
067            .reporter(this)
068            .format(format)
069            .message(message)
070            .minimum(minimum)
071            .maximum(maximum)
072            .ignoreCase(ignoreCase)
073            .build();
074        detector = new SinglelineDetector(options);
075    }
076
077    @Override
078    protected void processFiltered(File file, FileText fileText) {
079        detector.processLines(fileText);
080    }
081
082    /**
083     * Setter to specify the format of the regular expression to match.
084     *
085     * @param format the format of the regular expression to match.
086     * @since 5.0
087     */
088    public void setFormat(String format) {
089        this.format = format;
090    }
091
092    /**
093     * Setter to specify the message which is used to notify about violations,
094     * if empty then default (hard-coded) message is used.
095     *
096     * @param message the message to report for a match.
097     * @since 5.0
098     */
099    public void setMessage(String message) {
100        this.message = message;
101    }
102
103    /**
104     * Setter to specify the minimum number of matches required in each file.
105     *
106     * @param minimum the minimum number of matches required in each file.
107     * @since 5.0
108     */
109    public void setMinimum(int minimum) {
110        this.minimum = minimum;
111    }
112
113    /**
114     * Setter to specify the maximum number of matches required in each file.
115     *
116     * @param maximum the maximum number of matches required in each file.
117     * @since 5.0
118     */
119    public void setMaximum(int maximum) {
120        this.maximum = maximum;
121    }
122
123    /**
124     * Setter to control whether to ignore case when searching.
125     *
126     * @param ignoreCase whether to ignore case when searching.
127     * @since 5.0
128     */
129    public void setIgnoreCase(boolean ignoreCase) {
130        this.ignoreCase = ignoreCase;
131    }
132
133}