1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.api;
21
22 import java.io.File;
23 import java.util.Arrays;
24 import java.util.SortedSet;
25 import java.util.TreeSet;
26
27 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
28
29
30
31
32
33
34
35
36
37 public abstract class AbstractFileSetCheck
38 extends AbstractViolationReporter
39 implements FileSetCheck {
40
41
42 private static final String EXTENSION_SEPARATOR = ".";
43
44
45
46
47
48
49
50
51 private final ThreadLocal<FileContext> context = ThreadLocal.withInitial(FileContext::new);
52
53
54 private MessageDispatcher messageDispatcher;
55
56
57
58
59
60 private String[] fileExtensions;
61
62
63
64
65
66 private int tabWidth;
67
68
69
70
71
72
73
74
75 protected abstract void processFiltered(File file, FileText fileText)
76 throws CheckstyleException;
77
78 @Override
79 public void init() {
80
81 }
82
83 @Override
84 public void destroy() {
85 context.remove();
86 }
87
88 @Override
89 public void beginProcessing(String charset) {
90
91 }
92
93 @Override
94 public final SortedSet<Violation> process(File file, FileText fileText)
95 throws CheckstyleException {
96 final FileContext fileContext = context.get();
97 fileContext.fileContents = new FileContents(fileText);
98 fileContext.violations.clear();
99
100 if (CommonUtil.matchesFileExtension(file, fileExtensions)) {
101 processFiltered(file, fileText);
102 }
103 final SortedSet<Violation> result = new TreeSet<>(fileContext.violations);
104 fileContext.violations.clear();
105 return result;
106 }
107
108 @Override
109 public void finishProcessing() {
110
111 }
112
113 @Override
114 public final void setMessageDispatcher(MessageDispatcher messageDispatcher) {
115 this.messageDispatcher = messageDispatcher;
116 }
117
118
119
120
121
122
123
124 protected final MessageDispatcher getMessageDispatcher() {
125 return messageDispatcher;
126 }
127
128
129
130
131
132
133 public SortedSet<Violation> getViolations() {
134 return new TreeSet<>(context.get().violations);
135 }
136
137
138
139
140
141
142 public final void setFileContents(FileContents contents) {
143 context.get().fileContents = contents;
144 }
145
146
147
148
149
150
151 protected final FileContents getFileContents() {
152 return context.get().fileContents;
153 }
154
155
156
157
158
159
160
161 public String[] getFileExtensions() {
162 return Arrays.copyOf(fileExtensions, fileExtensions.length);
163 }
164
165
166
167
168
169
170
171
172 public final void setFileExtensions(String... extensions) {
173 if (extensions == null) {
174 throw new IllegalArgumentException("Extensions array can not be null");
175 }
176
177 fileExtensions = new String[extensions.length];
178 for (int i = 0; i < extensions.length; i++) {
179 final String extension = extensions[i];
180 if (extension.startsWith(EXTENSION_SEPARATOR)) {
181 fileExtensions[i] = extension;
182 }
183 else {
184 fileExtensions[i] = EXTENSION_SEPARATOR + extension;
185 }
186 }
187 }
188
189
190
191
192
193
194 protected final int getTabWidth() {
195 return tabWidth;
196 }
197
198
199
200
201
202
203 public final void setTabWidth(int tabWidth) {
204 this.tabWidth = tabWidth;
205 }
206
207
208
209
210
211
212 protected void addViolations(SortedSet<Violation> violations) {
213 context.get().violations.addAll(violations);
214 }
215
216 @Override
217 public final void log(int line, String key, Object... args) {
218 context.get().violations.add(
219 new Violation(line,
220 getMessageBundle(),
221 key,
222 args,
223 getSeverityLevel(),
224 getId(),
225 getClass(),
226 getCustomMessages().get(key)));
227 }
228
229 @Override
230 public final void log(int lineNo, int colNo, String key,
231 Object... args) {
232 final FileContext fileContext = context.get();
233 final int col = 1 + CommonUtil.lengthExpandedTabs(
234 fileContext.fileContents.getLine(lineNo - 1), colNo, tabWidth);
235 fileContext.violations.add(
236 new Violation(lineNo,
237 col,
238 getMessageBundle(),
239 key,
240 args,
241 getSeverityLevel(),
242 getId(),
243 getClass(),
244 getCustomMessages().get(key)));
245 }
246
247
248
249
250
251
252
253
254 protected final void fireErrors(String fileName) {
255 final FileContext fileContext = context.get();
256 final SortedSet<Violation> errors = new TreeSet<>(fileContext.violations);
257 fileContext.violations.clear();
258 messageDispatcher.fireErrors(fileName, errors);
259 }
260
261
262
263
264 private static final class FileContext {
265
266
267 private final SortedSet<Violation> violations = new TreeSet<>();
268
269
270 private FileContents fileContents;
271
272 }
273
274 }