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