View Javadoc
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  import com.puppycrawl.tools.checkstyle.api.LineColumn;
26  
27  /**
28   * A detector that matches across multiple lines.
29   */
30  class MultilineDetector {
31  
32      /**
33       * A key is pointing to the warning message text in "messages.properties"
34       * file.
35       */
36      public static final String MSG_REGEXP_EXCEEDED = "regexp.exceeded";
37  
38      /**
39       * A key is pointing to the warning message text in "messages.properties"
40       * file.
41       */
42      public static final String MSG_REGEXP_MINIMUM = "regexp.minimum";
43  
44      /**
45       * A key is pointing to the warning message text in "messages.properties"
46       * file.
47       */
48      public static final String MSG_EMPTY = "regexp.empty";
49      /**
50       * A key is pointing to the warning message text in "messages.properties"
51       * file.
52       */
53      public static final String MSG_STACKOVERFLOW = "regexp.StackOverflowError";
54  
55      /** The detection options to use. */
56      private final DetectorOptions options;
57      /** Tracks the number of matches. */
58      private int currentMatches;
59      /** The matcher. */
60      private Matcher matcher;
61      /** The file text content. */
62      private FileText text;
63  
64      /**
65       * Creates an instance.
66       *
67       * @param options the options to use.
68       */
69      /* package */ MultilineDetector(DetectorOptions options) {
70          this.options = options;
71      }
72  
73      /**
74       * Processes an entire text file looking for matches.
75       *
76       * @param fileText the text to process
77       */
78      public void processLines(FileText fileText) {
79          text = new FileText(fileText);
80          resetState();
81  
82          final String format = options.getFormat();
83          if (format == null || format.isEmpty()) {
84              options.getReporter().log(1, MSG_EMPTY);
85          }
86          else {
87              matcher = options.getPattern().matcher(fileText.getFullText());
88              findMatch();
89              finish();
90          }
91      }
92  
93      /** Method that finds the matches. */
94      private void findMatch() {
95          try {
96              boolean foundMatch = matcher.find();
97  
98              while (foundMatch) {
99                  currentMatches++;
100                 if (currentMatches > options.getMaximum()) {
101                     final LineColumn start = text.lineColumn(matcher.start());
102                     if (options.getMessage().isEmpty()) {
103                         options.getReporter().log(start.getLine(),
104                                 MSG_REGEXP_EXCEEDED, matcher.pattern().toString());
105                     }
106                     else {
107                         options.getReporter()
108                                 .log(start.getLine(), options.getMessage());
109                     }
110                 }
111                 foundMatch = matcher.find();
112             }
113         }
114         // see http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6337993 et al.
115         catch (StackOverflowError ignored) {
116             // OK http://blog.igorminar.com/2008/05/catching-stackoverflowerror-and-bug-in.html
117             // http://programmers.stackexchange.com/questions/
118             //        209099/is-it-ever-okay-to-catch-stackoverflowerror-in-java
119             options.getReporter().log(1, MSG_STACKOVERFLOW, matcher.pattern().toString());
120         }
121     }
122 
123     /** Perform processing at the end of a set of lines. */
124     private void finish() {
125         if (currentMatches < options.getMinimum()) {
126             if (options.getMessage().isEmpty()) {
127                 options.getReporter().log(1, MSG_REGEXP_MINIMUM,
128                         options.getMinimum(), options.getFormat());
129             }
130             else {
131                 options.getReporter().log(1, options.getMessage());
132             }
133         }
134     }
135 
136     /**
137      * Reset the state of the detector.
138      */
139     private void resetState() {
140         currentMatches = 0;
141     }
142 
143 }