View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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  public class SinglelineDetector {
30  
31      /** The detection options to use. */
32      private final DetectorOptions options;
33      /** The message key for exceeded matches. */
34      private final String exceededMessage;
35      /** The message key for minimum matches not met. */
36      private final String minimumMessage;
37      /** Tracks the number of matches. */
38      private int currentMatches;
39  
40      /**
41       * Creates an instance.
42       *
43       * @param options the options to use.
44       * @param exceededMessage the message key for exceeded matches.
45       * @param minimumMessage the message key for minimum matches not met.
46       */
47      /* package */ SinglelineDetector(DetectorOptions options,
48              String exceededMessage, String minimumMessage) {
49          this.options = options;
50          this.exceededMessage = exceededMessage;
51          this.minimumMessage = minimumMessage;
52      }
53  
54      /**
55       * Processes a set of lines looking for matches.
56       *
57       * @param fileText {@link FileText} object contains the lines to process.
58       */
59      public void processLines(FileText fileText) {
60          currentMatches = 0;
61          int lineNo = 0;
62          for (int index = 0; index < fileText.size(); index++) {
63              final String line = fileText.get(index);
64              lineNo++;
65              checkLine(lineNo, options.getPattern().matcher(line));
66          }
67          finish();
68      }
69  
70      /**
71       * Perform processing at the end of a set of lines.
72       */
73      private void finish() {
74          if (currentMatches < options.getMinimum()) {
75              if (options.getMessage().isEmpty()) {
76                  options.getReporter().log(1, minimumMessage,
77                          options.getMinimum(), options.getFormat());
78              }
79              else {
80                  options.getReporter().log(1, options.getMessage());
81              }
82          }
83      }
84  
85      /**
86       * Check a line for matches.
87       *
88       * @param lineNo        the line number of the line to check
89       * @param matcher       the matcher to use
90       */
91      private void checkLine(int lineNo, Matcher matcher) {
92          int startPosition = 0;
93          while (matcher.find(startPosition)) {
94              // match is found, check for intersection with comment
95              final int startCol = matcher.start(0);
96              final int endCol = matcher.end(0);
97              // Note that Matcher.end(int) returns the offset AFTER the
98              // last matched character, but shouldSuppress()
99              // needs column number of the last character.
100             // So we need to use (endCol - 1) here.
101 
102             if (options.getSuppressor()
103                     .shouldSuppress(lineNo, startCol, lineNo, endCol - 1)) {
104                 startPosition = endCol;
105             }
106             else {
107                 currentMatches++;
108                 if (currentMatches > options.getMaximum()) {
109                     if (options.getMessage().isEmpty()) {
110                         options.getReporter().log(lineNo, exceededMessage,
111                                 matcher.pattern().toString());
112                     }
113                     else {
114                         options.getReporter().log(lineNo, options.getMessage());
115                     }
116                 }
117                 break;
118             }
119         }
120     }
121 
122 }