Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SinglelineDetector |
|
| 3.0;3 |
1 | //////////////////////////////////////////////////////////////////////////////// | |
2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
3 | // Copyright (C) 2001-2014 Oliver Burn | |
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 | package com.puppycrawl.tools.checkstyle.checks.regexp; | |
20 | ||
21 | import java.util.List; | |
22 | import java.util.regex.Matcher; | |
23 | ||
24 | /** | |
25 | * A detector that matches individual lines. | |
26 | * @author oliver | |
27 | */ | |
28 | class SinglelineDetector | |
29 | { | |
30 | /** The detection options to use. */ | |
31 | private final DetectorOptions mOptions; | |
32 | /** Tracks the number of matches. */ | |
33 | private int mCurrentMatches; | |
34 | ||
35 | /** | |
36 | * Creates an instance. | |
37 | * @param aOptions the options to use. | |
38 | */ | |
39 | public SinglelineDetector(DetectorOptions aOptions) | |
40 | 22 | { |
41 | 22 | mOptions = aOptions; |
42 | 22 | } |
43 | ||
44 | /** | |
45 | * Processes a set of lines looking for matches. | |
46 | * @param aLines the lines to process. | |
47 | */ | |
48 | public void processLines(List<String> aLines) | |
49 | { | |
50 | 22 | resetState(); |
51 | 22 | int lineno = 0; |
52 | 22 | for (String line : aLines) { |
53 | 2475 | lineno++; |
54 | 2475 | checkLine(lineno, line, mOptions.getPattern().matcher(line), 0); |
55 | } | |
56 | 22 | finish(); |
57 | 22 | } |
58 | ||
59 | /** Perform processing at the end of a set of lines. */ | |
60 | private void finish() | |
61 | { | |
62 | 22 | if (mCurrentMatches < mOptions.getMinimum()) { |
63 | 1 | if ("".equals(mOptions.getMessage())) { |
64 | 1 | mOptions.getReporter().log(0, "regexp.minimum", |
65 | mOptions.getMinimum(), mOptions.getFormat()); | |
66 | } | |
67 | else { | |
68 | 0 | mOptions.getReporter().log(0, mOptions.getMessage()); |
69 | } | |
70 | } | |
71 | 22 | } |
72 | ||
73 | /** | |
74 | * Reset the state of the detector. | |
75 | */ | |
76 | private void resetState() | |
77 | { | |
78 | 22 | mCurrentMatches = 0; |
79 | 22 | } |
80 | ||
81 | /** | |
82 | * Check a line for matches. | |
83 | * @param aLineno the line number of the line to check | |
84 | * @param aLine the line to check | |
85 | * @param aMatcher the matcher to use | |
86 | * @param aStartPosition the position to start searching from. | |
87 | */ | |
88 | private void checkLine(int aLineno, String aLine, Matcher aMatcher, | |
89 | int aStartPosition) | |
90 | { | |
91 | 2481 | final boolean foundMatch = aMatcher.find(aStartPosition); |
92 | 2481 | if (!foundMatch) { |
93 | 2459 | return; |
94 | } | |
95 | ||
96 | // match is found, check for intersection with comment | |
97 | 22 | final int startCol = aMatcher.start(0); |
98 | 22 | final int endCol = aMatcher.end(0); |
99 | // Note that Matcher.end(int) returns the offset AFTER the | |
100 | // last matched character, but shouldSuppress() | |
101 | // needs column number of the last character. | |
102 | // So we need to use (endCol - 1) here. | |
103 | 22 | if (mOptions.getSuppressor() |
104 | .shouldSuppress(aLineno, startCol, aLineno, endCol - 1)) | |
105 | { | |
106 | 7 | if (endCol < aLine.length()) { |
107 | // check if the expression is on the rest of the line | |
108 | 6 | checkLine(aLineno, aLine, aMatcher, endCol); |
109 | } | |
110 | 7 | return; // end processing here |
111 | } | |
112 | ||
113 | 15 | mCurrentMatches++; |
114 | 15 | if (mCurrentMatches > mOptions.getMaximum()) { |
115 | 10 | if ("".equals(mOptions.getMessage())) { |
116 | 8 | mOptions.getReporter().log(aLineno, "regexp.exceeded", |
117 | aMatcher.pattern().toString()); | |
118 | } | |
119 | else { | |
120 | 2 | mOptions.getReporter().log(aLineno, mOptions.getMessage()); |
121 | } | |
122 | } | |
123 | 15 | } |
124 | } |