001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2021 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.util.regex.Matcher;
023
024import com.puppycrawl.tools.checkstyle.api.FileText;
025
026/**
027 * A detector that matches individual lines.
028 */
029class SinglelineDetector {
030
031    /**
032     * A key is pointing to the warning message text in "messages.properties"
033     * file.
034     */
035    public static final String MSG_REGEXP_EXCEEDED = "regexp.exceeded";
036
037    /**
038     * A key is pointing to the warning message text in "messages.properties"
039     * file.
040     */
041    public static final String MSG_REGEXP_MINIMUM = "regexp.minimum";
042
043    /** The detection options to use. */
044    private final DetectorOptions options;
045    /** Tracks the number of matches. */
046    private int currentMatches;
047
048    /**
049     * Creates an instance.
050     *
051     * @param options the options to use.
052     */
053    /* package */ SinglelineDetector(DetectorOptions options) {
054        this.options = options;
055    }
056
057    /**
058     * Processes a set of lines looking for matches.
059     *
060     * @param fileText {@link FileText} object contains the lines to process.
061     */
062    public void processLines(FileText fileText) {
063        resetState();
064        int lineNo = 0;
065        for (int index = 0; index < fileText.size(); index++) {
066            final String line = fileText.get(index);
067            lineNo++;
068            checkLine(lineNo, line, options.getPattern().matcher(line), 0);
069        }
070        finish();
071    }
072
073    /** Perform processing at the end of a set of lines. */
074    private void finish() {
075        if (currentMatches < options.getMinimum()) {
076            if (options.getMessage().isEmpty()) {
077                options.getReporter().log(1, MSG_REGEXP_MINIMUM,
078                        options.getMinimum(), options.getFormat());
079            }
080            else {
081                options.getReporter().log(1, options.getMessage());
082            }
083        }
084    }
085
086    /**
087     * Reset the state of the detector.
088     */
089    private void resetState() {
090        currentMatches = 0;
091    }
092
093    /**
094     * Check a line for matches.
095     *
096     * @param lineNo the line number of the line to check
097     * @param line the line to check
098     * @param matcher the matcher to use
099     * @param startPosition the position to start searching from.
100     */
101    private void checkLine(int lineNo, String line, Matcher matcher,
102            int startPosition) {
103        final boolean foundMatch = matcher.find(startPosition);
104        if (foundMatch) {
105            // match is found, check for intersection with comment
106            final int startCol = matcher.start(0);
107            final int endCol = matcher.end(0);
108            // Note that Matcher.end(int) returns the offset AFTER the
109            // last matched character, but shouldSuppress()
110            // needs column number of the last character.
111            // So we need to use (endCol - 1) here.
112            if (options.getSuppressor()
113                    .shouldSuppress(lineNo, startCol, lineNo, endCol - 1)) {
114                checkLine(lineNo, line, matcher, endCol);
115            }
116            else {
117                currentMatches++;
118                if (currentMatches > options.getMaximum()) {
119                    if (options.getMessage().isEmpty()) {
120                        options.getReporter().log(lineNo, MSG_REGEXP_EXCEEDED,
121                                matcher.pattern().toString());
122                    }
123                    else {
124                        options.getReporter().log(lineNo, options.getMessage());
125                    }
126                }
127            }
128        }
129    }
130
131}