1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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       * @param options the options to use.
67       */
68      /* package */ MultilineDetector(DetectorOptions options) {
69          this.options = options;
70      }
71  
72      /**
73       * Processes an entire text file looking for matches.
74       * @param fileText the text to process
75       */
76      public void processLines(FileText fileText) {
77          text = new FileText(fileText);
78          resetState();
79  
80          final String format = options.getFormat();
81          if (format == null || format.isEmpty()) {
82              options.getReporter().log(1, MSG_EMPTY);
83          }
84          else {
85              matcher = options.getPattern().matcher(fileText.getFullText());
86              findMatch();
87              finish();
88          }
89      }
90  
91      /** Method that finds the matches. */
92      private void findMatch() {
93          try {
94              boolean foundMatch = matcher.find();
95  
96              while (foundMatch) {
97                  currentMatches++;
98                  if (currentMatches > options.getMaximum()) {
99                      final LineColumn start = text.lineColumn(matcher.start());
100                     if (options.getMessage().isEmpty()) {
101                         options.getReporter().log(start.getLine(),
102                                 MSG_REGEXP_EXCEEDED, matcher.pattern().toString());
103                     }
104                     else {
105                         options.getReporter()
106                                 .log(start.getLine(), options.getMessage());
107                     }
108                 }
109                 foundMatch = matcher.find();
110             }
111         }
112         // see http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6337993 et al.
113         catch (StackOverflowError ignored) {
114             // OK http://blog.igorminar.com/2008/05/catching-stackoverflowerror-and-bug-in.html
115             // http://programmers.stackexchange.com/questions/
116             //        209099/is-it-ever-okay-to-catch-stackoverflowerror-in-java
117             options.getReporter().log(1, MSG_STACKOVERFLOW, matcher.pattern().toString());
118         }
119     }
120 
121     /** Perform processing at the end of a set of lines. */
122     private void finish() {
123         if (currentMatches < options.getMinimum()) {
124             if (options.getMessage().isEmpty()) {
125                 options.getReporter().log(1, MSG_REGEXP_MINIMUM,
126                         options.getMinimum(), options.getFormat());
127             }
128             else {
129                 options.getReporter().log(1, options.getMessage());
130             }
131         }
132     }
133 
134     /**
135      * Reset the state of the detector.
136      */
137     private void resetState() {
138         currentMatches = 0;
139     }
140 
141 }