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