001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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    /** A key is pointing to the warning message text in "messages.properties" file. */
047    public static final String MSG_REGEXP_EXCEEDED = "regexp.exceeded";
048
049    /** A key is pointing to the warning message text in "messages.properties" file. */
050    public static final String MSG_REGEXP_MINIMUM = "regexp.minimum";
051
052    /** Specify the format of the regular expression to match. */
053    @XdocsPropertyType(PropertyType.PATTERN)
054    private String format = "$.";
055    /**
056     * Specify the message which is used to notify about violations,
057     * if empty then default (hard-coded) message is used.
058     */
059    private String message;
060    /** Specify the minimum number of matches required in each file. */
061    private int minimum;
062    /** Specify the maximum number of matches required in each file. */
063    private int maximum;
064    /** Control whether to ignore case when searching. */
065    private boolean ignoreCase;
066
067    /** The detector to use. */
068    private SinglelineDetector detector;
069
070    /**
071     * Creates a new {@code RegexpSinglelineCheck} instance.
072     */
073    public RegexpSinglelineCheck() {
074        // no code by default
075    }
076
077    @Override
078    public void beginProcessing(String charset) {
079        final DetectorOptions options = DetectorOptions.newBuilder()
080            .reporter(this)
081            .format(format)
082            .message(message)
083            .minimum(minimum)
084            .maximum(maximum)
085            .ignoreCase(ignoreCase)
086            .build();
087        detector = new SinglelineDetector(options,
088                MSG_REGEXP_EXCEEDED, MSG_REGEXP_MINIMUM);
089    }
090
091    @Override
092    protected void processFiltered(File file, FileText fileText) {
093        detector.processLines(fileText);
094    }
095
096    /**
097     * Setter to specify the format of the regular expression to match.
098     *
099     * @param format the format of the regular expression to match.
100     * @since 5.0
101     */
102    public void setFormat(String format) {
103        this.format = format;
104    }
105
106    /**
107     * Setter to specify the message which is used to notify about violations,
108     * if empty then default (hard-coded) message is used.
109     *
110     * @param message the message to report for a match.
111     * @since 5.0
112     */
113    public void setMessage(String message) {
114        this.message = message;
115    }
116
117    /**
118     * Setter to specify the minimum number of matches required in each file.
119     *
120     * @param minimum the minimum number of matches required in each file.
121     * @since 5.0
122     */
123    public void setMinimum(int minimum) {
124        this.minimum = minimum;
125    }
126
127    /**
128     * Setter to specify the maximum number of matches required in each file.
129     *
130     * @param maximum the maximum number of matches required in each file.
131     * @since 5.0
132     */
133    public void setMaximum(int maximum) {
134        this.maximum = maximum;
135    }
136
137    /**
138     * Setter to control whether to ignore case when searching.
139     *
140     * @param ignoreCase whether to ignore case when searching.
141     * @since 5.0
142     */
143    public void setIgnoreCase(boolean ignoreCase) {
144        this.ignoreCase = ignoreCase;
145    }
146
147}