/
AbstractFileSetCheck.java
178 lines (156 loc) · 6.02 KB
/
AbstractFileSetCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2016 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.api;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.SortedSet;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
/**
* Provides common functionality for many FileSetChecks.
*
* @author lkuehne
* @author oliver
*/
public abstract class AbstractFileSetCheck
extends AbstractViolationReporter
implements FileSetCheck {
/** Collects the error messages. */
private final LocalizedMessages messageCollector = new LocalizedMessages();
/** The dispatcher errors are fired to. */
private MessageDispatcher messageDispatcher;
/** The file extensions that are accepted by this filter. */
private String[] fileExtensions = CommonUtils.EMPTY_STRING_ARRAY;
/**
* Called to process a file that matches the specified file extensions.
* @param file the file to be processed
* @param lines an immutable list of the contents of the file.
* @throws CheckstyleException if error condition within Checkstyle occurs.
*/
protected abstract void processFiltered(File file, List<String> lines)
throws CheckstyleException;
@Override
public void init() {
// No code by default, should be overridden only by demand at subclasses
}
@Override
public void destroy() {
// No code by default, should be overridden only by demand at subclasses
}
@Override
public void beginProcessing(String charset) {
// No code by default, should be overridden only by demand at subclasses
}
@Override
public final SortedSet<LocalizedMessage> process(File file, List<String> lines)
throws CheckstyleException {
messageCollector.reset();
// Process only what interested in
if (CommonUtils.matchesFileExtension(file, fileExtensions)) {
processFiltered(file, lines);
}
return messageCollector.getMessages();
}
@Override
public void finishProcessing() {
// No code by default, should be overridden only by demand at subclasses
}
@Override
public final void setMessageDispatcher(MessageDispatcher messageDispatcher) {
this.messageDispatcher = messageDispatcher;
}
/**
* A message dispatcher is used to fire violation messages to
* interested audit listeners.
*
* @return the current MessageDispatcher.
*/
protected final MessageDispatcher getMessageDispatcher() {
return messageDispatcher;
}
/**
* @return file extensions that identify the files that pass the
* filter of this FileSetCheck.
*/
public String[] getFileExtensions() {
return Arrays.copyOf(fileExtensions, fileExtensions.length);
}
/**
* Sets the file extensions that identify the files that pass the
* filter of this FileSetCheck.
* @param extensions the set of file extensions. A missing
* initial '.' character of an extension is automatically added.
* @throws IllegalArgumentException is argument is null
*/
public final void setFileExtensions(String... extensions) {
if (extensions == null) {
throw new IllegalArgumentException("Extensions array can not be null");
}
fileExtensions = new String[extensions.length];
for (int i = 0; i < extensions.length; i++) {
final String extension = extensions[i];
if (CommonUtils.startsWithChar(extension, '.')) {
fileExtensions[i] = extension;
}
else {
fileExtensions[i] = "." + extension;
}
}
}
/**
* Returns the collector for violation messages.
* Subclasses can use the collector to find out the violation
* messages to fire via the message dispatcher.
*
* @return the collector for localized messages.
*/
protected final LocalizedMessages getMessageCollector() {
return messageCollector;
}
@Override
public final void log(int line, String key, Object... args) {
log(line, 0, key, args);
}
@Override
public final void log(int lineNo, int colNo, String key,
Object... args) {
messageCollector.add(
new LocalizedMessage(lineNo,
colNo,
getMessageBundle(),
key,
args,
getSeverityLevel(),
getId(),
getClass(),
getCustomMessages().get(key)));
}
/**
* Notify all listeners about the errors in a file.
* Calls {@code MessageDispatcher.fireErrors()} with
* all logged errors and than clears errors' list.
* @param fileName the audited file
*/
protected final void fireErrors(String fileName) {
final SortedSet<LocalizedMessage> errors = messageCollector
.getMessages();
messageCollector.reset();
getMessageDispatcher().fireErrors(fileName, errors);
}
}