1 /////////////////////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules. 3 // Copyright (C) 2001-2024 the original author or authors. 4 // 5 // This library is free software; you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 2.1 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library; if not, write to the Free Software 17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 /////////////////////////////////////////////////////////////////////////////////////////////// 19 20 package com.puppycrawl.tools.checkstyle.checks.regexp; 21 22 import java.util.regex.Matcher; 23 24 import com.puppycrawl.tools.checkstyle.api.FileText; 25 26 /** 27 * A detector that matches individual lines. 28 */ 29 class SinglelineDetector { 30 31 /** 32 * A key is pointing to the warning message text in "messages.properties" 33 * file. 34 */ 35 public static final String MSG_REGEXP_EXCEEDED = "regexp.exceeded"; 36 37 /** 38 * A key is pointing to the warning message text in "messages.properties" 39 * file. 40 */ 41 public static final String MSG_REGEXP_MINIMUM = "regexp.minimum"; 42 43 /** 44 * The detection options to use. 45 */ 46 private final DetectorOptions options; 47 /** 48 * Tracks the number of matches. 49 */ 50 private int currentMatches; 51 52 /** 53 * Creates an instance. 54 * 55 * @param options the options to use. 56 */ 57 /* package */ SinglelineDetector(DetectorOptions options) { 58 this.options = options; 59 } 60 61 /** 62 * Processes a set of lines looking for matches. 63 * 64 * @param fileText {@link FileText} object contains the lines to process. 65 */ 66 public void processLines(FileText fileText) { 67 currentMatches = 0; 68 int lineNo = 0; 69 for (int index = 0; index < fileText.size(); index++) { 70 final String line = fileText.get(index); 71 lineNo++; 72 checkLine(lineNo, options.getPattern().matcher(line)); 73 } 74 finish(); 75 } 76 77 /** 78 * Perform processing at the end of a set of lines. 79 */ 80 private void finish() { 81 if (currentMatches < options.getMinimum()) { 82 if (options.getMessage().isEmpty()) { 83 options.getReporter().log(1, MSG_REGEXP_MINIMUM, 84 options.getMinimum(), options.getFormat()); 85 } 86 else { 87 options.getReporter().log(1, options.getMessage()); 88 } 89 } 90 } 91 92 /** 93 * Check a line for matches. 94 * 95 * @param lineNo the line number of the line to check 96 * @param matcher the matcher to use 97 */ 98 private void checkLine(int lineNo, Matcher matcher) { 99 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 }