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 import java.util.regex.Pattern;
24
25 import com.puppycrawl.tools.checkstyle.StatelessCheck;
26 import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
27 import com.puppycrawl.tools.checkstyle.api.FileText;
28
29 /**
30 * Implementation of a check that looks that matches across multiple lines in
31 * any file type.
32 */
33 @StatelessCheck
34 public class RegexpMultilineCheck extends AbstractFileSetCheck {
35
36 /** The format of the regular expression to match. */
37 private String format = "$.";
38 /** The message to report for a match. */
39 private String message;
40 /** The minimum number of matches required per file. */
41 private int minimum;
42 /** The maximum number of matches required per file. */
43 private int maximum;
44 /** Whether to ignore case when matching. */
45 private boolean ignoreCase;
46
47 /** The detector to use. */
48 private MultilineDetector detector;
49
50 @Override
51 public void beginProcessing(String charset) {
52 final DetectorOptions options = DetectorOptions.newBuilder()
53 .reporter(this)
54 .compileFlags(Pattern.MULTILINE)
55 .format(format)
56 .message(message)
57 .minimum(minimum)
58 .maximum(maximum)
59 .ignoreCase(ignoreCase)
60 .build();
61 detector = new MultilineDetector(options);
62 }
63
64 @Override
65 protected void processFiltered(File file, FileText fileText) {
66 detector.processLines(fileText);
67 }
68
69 /**
70 * Sets the format of the regular expression to match.
71 * @param format the format of the regular expression to match.
72 */
73 public void setFormat(String format) {
74 this.format = format;
75 }
76
77 /**
78 * Sets the message to report for a match.
79 * @param message the message to report for a match.
80 */
81 public void setMessage(String message) {
82 this.message = message;
83 }
84
85 /**
86 * Sets the minimum number of matches required per file.
87 * @param minimum the minimum number of matches required per file.
88 */
89 public void setMinimum(int minimum) {
90 this.minimum = minimum;
91 }
92
93 /**
94 * Sets the maximum number of matches required per file.
95 * @param maximum the maximum number of matches required per file.
96 */
97 public void setMaximum(int maximum) {
98 this.maximum = maximum;
99 }
100
101 /**
102 * Sets whether to ignore case when matching.
103 * @param ignoreCase whether to ignore case when matching.
104 */
105 public void setIgnoreCase(boolean ignoreCase) {
106 this.ignoreCase = ignoreCase;
107 }
108
109 }