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 com.puppycrawl.tools.checkstyle.StatelessCheck;
23  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24  import com.puppycrawl.tools.checkstyle.api.DetailAST;
25  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
26  
27  /**
28   * Implementation of a check that looks for a single line in Java files.
29   * Supports ignoring comments for matches.
30   * @author Oliver Burn
31   */
32  @StatelessCheck
33  public class RegexpSinglelineJavaCheck extends AbstractCheck {
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      /** Suppress comments. **/
46      private boolean ignoreComments;
47  
48      @Override
49      public int[] getDefaultTokens() {
50          return getAcceptableTokens();
51      }
52  
53      @Override
54      public int[] getAcceptableTokens() {
55          return CommonUtils.EMPTY_INT_ARRAY;
56      }
57  
58      @Override
59      public int[] getRequiredTokens() {
60          return getAcceptableTokens();
61      }
62  
63      @Override
64      public void beginTree(DetailAST rootAST) {
65          MatchSuppressor suppressor = null;
66          if (ignoreComments) {
67              suppressor = new CommentSuppressor(getFileContents());
68          }
69  
70          final DetectorOptions options = DetectorOptions.newBuilder()
71              .reporter(this)
72              .compileFlags(0)
73              .suppressor(suppressor)
74              .format(format)
75              .message(message)
76              .minimum(minimum)
77              .maximum(maximum)
78              .ignoreCase(ignoreCase)
79              .build();
80          final SinglelineDetector detector = new SinglelineDetector(options);
81          detector.processLines(getFileContents().getText());
82      }
83  
84      /**
85       * Set the format of the regular expression to match.
86       * @param format the format of the regular expression to match.
87       */
88      public void setFormat(String format) {
89          this.format = format;
90      }
91  
92      /**
93       * Set the message to report for a match.
94       * @param message the message to report for a match.
95       */
96      public void setMessage(String message) {
97          this.message = message;
98      }
99  
100     /**
101      * Set the minimum number of matches required per file.
102      * @param minimum the minimum number of matches required per file.
103      */
104     public void setMinimum(int minimum) {
105         this.minimum = minimum;
106     }
107 
108     /**
109      * Set the maximum number of matches required per file.
110      * @param maximum the maximum number of matches required per file.
111      */
112     public void setMaximum(int maximum) {
113         this.maximum = maximum;
114     }
115 
116     /**
117      * Set whether to ignore case when matching.
118      * @param ignoreCase whether to ignore case when matching.
119      */
120     public void setIgnoreCase(boolean ignoreCase) {
121         this.ignoreCase = ignoreCase;
122     }
123 
124     /**
125      * Set whether to ignore comments when matching.
126      * @param ignore whether to ignore comments when matching.
127      */
128     public void setIgnoreComments(boolean ignore) {
129         ignoreComments = ignore;
130     }
131 }