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