View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.PropertyType;
25  import com.puppycrawl.tools.checkstyle.StatelessCheck;
26  import com.puppycrawl.tools.checkstyle.XdocsPropertyType;
27  import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
28  import com.puppycrawl.tools.checkstyle.api.FileText;
29  
30  /**
31   * <p>
32   * Checks that a specified pattern matches a single-line in any file type.
33   * </p>
34   * <p>
35   * Rationale: This check can be used to prototype checks and to find common bad
36   * practice such as calling {@code ex.printStacktrace()},
37   * {@code System.out.println()}, {@code System.exit()}, etc.
38   * </p>
39   * <ul>
40   * <li>
41   * Property {@code fileExtensions} - Specify the file extensions of the files to process.
42   * Type is {@code java.lang.String[]}.
43   * Default value is {@code ""}.
44   * </li>
45   * <li>
46   * Property {@code format} - Specify the format of the regular expression to match.
47   * Type is {@code java.util.regex.Pattern}.
48   * Default value is {@code "$."}.
49   * </li>
50   * <li>
51   * Property {@code ignoreCase} - Control whether to ignore case when searching.
52   * Type is {@code boolean}.
53   * Default value is {@code false}.
54   * </li>
55   * <li>
56   * Property {@code maximum} - Specify the maximum number of matches required in each file.
57   * Type is {@code int}.
58   * Default value is {@code 0}.
59   * </li>
60   * <li>
61   * Property {@code message} - Specify the message which is used to notify about
62   * violations, if empty then default (hard-coded) message is used.
63   * Type is {@code java.lang.String}.
64   * Default value is {@code null}.
65   * </li>
66   * <li>
67   * Property {@code minimum} - Specify the minimum number of matches required in each file.
68   * Type is {@code int}.
69   * Default value is {@code 0}.
70   * </li>
71   * </ul>
72   * <p>
73   * Parent is {@code com.puppycrawl.tools.checkstyle.Checker}
74   * </p>
75   * <p>
76   * Violation Message Keys:
77   * </p>
78   * <ul>
79   * <li>
80   * {@code regexp.exceeded}
81   * </li>
82   * <li>
83   * {@code regexp.minimum}
84   * </li>
85   * </ul>
86   *
87   * @since 5.0
88   */
89  @StatelessCheck
90  public class RegexpSinglelineCheck extends AbstractFileSetCheck {
91  
92      /** Specify the format of the regular expression to match. */
93      @XdocsPropertyType(PropertyType.PATTERN)
94      private String format = "$.";
95      /**
96       * Specify the message which is used to notify about violations,
97       * if empty then default (hard-coded) message is used.
98       */
99      private String message;
100     /** Specify the minimum number of matches required in each file. */
101     private int minimum;
102     /** Specify the maximum number of matches required in each file. */
103     private int maximum;
104     /** Control whether to ignore case when searching. */
105     private boolean ignoreCase;
106 
107     /** The detector to use. */
108     private SinglelineDetector detector;
109 
110     @Override
111     public void beginProcessing(String charset) {
112         final DetectorOptions options = DetectorOptions.newBuilder()
113             .reporter(this)
114             .format(format)
115             .message(message)
116             .minimum(minimum)
117             .maximum(maximum)
118             .ignoreCase(ignoreCase)
119             .build();
120         detector = new SinglelineDetector(options);
121     }
122 
123     @Override
124     protected void processFiltered(File file, FileText fileText) {
125         detector.processLines(fileText);
126     }
127 
128     /**
129      * Setter to specify the format of the regular expression to match.
130      *
131      * @param format the format of the regular expression to match.
132      * @since 5.0
133      */
134     public void setFormat(String format) {
135         this.format = format;
136     }
137 
138     /**
139      * Setter to specify the message which is used to notify about violations,
140      * if empty then default (hard-coded) message is used.
141      *
142      * @param message the message to report for a match.
143      * @since 5.0
144      */
145     public void setMessage(String message) {
146         this.message = message;
147     }
148 
149     /**
150      * Setter to specify the minimum number of matches required in each file.
151      *
152      * @param minimum the minimum number of matches required in each file.
153      * @since 5.0
154      */
155     public void setMinimum(int minimum) {
156         this.minimum = minimum;
157     }
158 
159     /**
160      * Setter to specify the maximum number of matches required in each file.
161      *
162      * @param maximum the maximum number of matches required in each file.
163      * @since 5.0
164      */
165     public void setMaximum(int maximum) {
166         this.maximum = maximum;
167     }
168 
169     /**
170      * Setter to control whether to ignore case when searching.
171      *
172      * @param ignoreCase whether to ignore case when searching.
173      * @since 5.0
174      */
175     public void setIgnoreCase(boolean ignoreCase) {
176         this.ignoreCase = ignoreCase;
177     }
178 
179 }