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