001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 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 /** 044 * The detection options to use. 045 */ 046 private final DetectorOptions options; 047 /** 048 * Tracks the number of matches. 049 */ 050 private int currentMatches; 051 052 /** 053 * Creates an instance. 054 * 055 * @param options the options to use. 056 */ 057 /* package */ SinglelineDetector(DetectorOptions options) { 058 this.options = options; 059 } 060 061 /** 062 * Processes a set of lines looking for matches. 063 * 064 * @param fileText {@link FileText} object contains the lines to process. 065 */ 066 public void processLines(FileText fileText) { 067 currentMatches = 0; 068 int lineNo = 0; 069 for (int index = 0; index < fileText.size(); index++) { 070 final String line = fileText.get(index); 071 lineNo++; 072 checkLine(lineNo, options.getPattern().matcher(line)); 073 } 074 finish(); 075 } 076 077 /** 078 * Perform processing at the end of a set of lines. 079 */ 080 private void finish() { 081 if (currentMatches < options.getMinimum()) { 082 if (options.getMessage().isEmpty()) { 083 options.getReporter().log(1, MSG_REGEXP_MINIMUM, 084 options.getMinimum(), options.getFormat()); 085 } 086 else { 087 options.getReporter().log(1, options.getMessage()); 088 } 089 } 090 } 091 092 /** 093 * Check a line for matches. 094 * 095 * @param lineNo the line number of the line to check 096 * @param matcher the matcher to use 097 */ 098 private void checkLine(int lineNo, Matcher matcher) { 099 int startPosition = 0; 100 while (matcher.find(startPosition)) { 101 // match is found, check for intersection with comment 102 final int startCol = matcher.start(0); 103 final int endCol = matcher.end(0); 104 // Note that Matcher.end(int) returns the offset AFTER the 105 // last matched character, but shouldSuppress() 106 // needs column number of the last character. 107 // So we need to use (endCol - 1) here. 108 109 if (options.getSuppressor() 110 .shouldSuppress(lineNo, startCol, lineNo, endCol - 1)) { 111 startPosition = endCol; 112 } 113 else { 114 currentMatches++; 115 if (currentMatches > options.getMaximum()) { 116 if (options.getMessage().isEmpty()) { 117 options.getReporter().log(lineNo, MSG_REGEXP_EXCEEDED, 118 matcher.pattern().toString()); 119 } 120 else { 121 options.getReporter().log(lineNo, options.getMessage()); 122 } 123 } 124 break; 125 } 126 } 127 } 128}