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