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.io.File;
23  import java.util.regex.Pattern;
24  
25  import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
26  import com.puppycrawl.tools.checkstyle.api.FileText;
27  
28  /**
29   * Implementation of a check that looks that matches across multiple lines in
30   * any file type.
31   * @author Oliver Burn
32   */
33  public class RegexpMultilineCheck extends AbstractFileSetCheck {
34  
35      /** The format of the regular expression to match. */
36      private String format = "$.";
37      /** The message to report for a match. */
38      private String message;
39      /** The minimum number of matches required per file. */
40      private int minimum;
41      /** The maximum number of matches required per file. */
42      private int maximum;
43      /** Whether to ignore case when matching. */
44      private boolean ignoreCase;
45  
46      /** The detector to use. */
47      private MultilineDetector detector;
48  
49      @Override
50      public void beginProcessing(String charset) {
51          final DetectorOptions options = DetectorOptions.newBuilder()
52              .reporter(this)
53              .compileFlags(Pattern.MULTILINE)
54              .format(format)
55              .message(message)
56              .minimum(minimum)
57              .maximum(maximum)
58              .ignoreCase(ignoreCase)
59              .build();
60          detector = new MultilineDetector(options);
61      }
62  
63      @Override
64      protected void processFiltered(File file, FileText fileText) {
65          detector.processLines(fileText);
66      }
67  
68      /**
69       * Sets the format of the regular expression to match.
70       * @param format the format of the regular expression to match.
71       */
72      public void setFormat(String format) {
73          this.format = format;
74      }
75  
76      /**
77       * Sets the message to report for a match.
78       * @param message the message to report for a match.
79       */
80      public void setMessage(String message) {
81          this.message = message;
82      }
83  
84      /**
85       * Sets the minimum number of matches required per file.
86       * @param minimum the minimum number of matches required per file.
87       */
88      public void setMinimum(int minimum) {
89          this.minimum = minimum;
90      }
91  
92      /**
93       * Sets the maximum number of matches required per file.
94       * @param maximum the maximum number of matches required per file.
95       */
96      public void setMaximum(int maximum) {
97          this.maximum = maximum;
98      }
99  
100     /**
101      * Sets whether to ignore case when matching.
102      * @param ignoreCase whether to ignore case when matching.
103      */
104     public void setIgnoreCase(boolean ignoreCase) {
105         this.ignoreCase = ignoreCase;
106     }
107 }