1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.puppycrawl.tools.checkstyle.api;
20
21 import java.io.File;
22 import java.util.List;
23 import java.util.TreeSet;
24
25 import com.puppycrawl.tools.checkstyle.Utils;
26
27
28
29
30
31
32
33 public abstract class AbstractFileSetCheck
34 extends AbstractViolationReporter
35 implements FileSetCheck
36 {
37
38 private MessageDispatcher dispatcher;
39
40
41 private String[] fileExtensions = {};
42
43
44 private final LocalizedMessages messages = new LocalizedMessages();
45
46
47
48
49
50
51 protected abstract void processFiltered(File file, List<String> lines);
52
53
54 @Override
55 public void init()
56 {
57 }
58
59
60 @Override
61 public void destroy()
62 {
63 }
64
65
66 @Override
67 public void beginProcessing(String charset)
68 {
69 }
70
71
72 @Override
73 public final TreeSet<LocalizedMessage> process(File file,
74 List<String> lines)
75 {
76 getMessageCollector().reset();
77
78 if (Utils.fileExtensionMatches(file, fileExtensions)) {
79 processFiltered(file, lines);
80 }
81 return getMessageCollector().getMessages();
82 }
83
84
85 @Override
86 public void finishProcessing()
87 {
88 }
89
90
91 @Override
92 public final void setMessageDispatcher(MessageDispatcher dispatcher)
93 {
94 this.dispatcher = dispatcher;
95 }
96
97
98
99
100
101
102
103 protected final MessageDispatcher getMessageDispatcher()
104 {
105 return dispatcher;
106 }
107
108
109
110
111
112
113
114 public final void setFileExtensions(String[] extensions)
115 {
116 if (extensions == null) {
117 fileExtensions = null;
118 return;
119 }
120
121 fileExtensions = new String[extensions.length];
122 for (int i = 0; i < extensions.length; i++) {
123 final String extension = extensions[i];
124 if (extension.startsWith(".")) {
125 fileExtensions[i] = extension;
126 }
127 else {
128 fileExtensions[i] = "." + extension;
129 }
130 }
131 }
132
133
134
135
136
137
138
139
140 protected final LocalizedMessages getMessageCollector()
141 {
142 return messages;
143 }
144
145 @Override
146 public final void log(int line, String key, Object... args)
147 {
148 log(line, 0, key, args);
149 }
150
151 @Override
152 public final void log(int lineNo, int colNo, String key,
153 Object... args)
154 {
155 getMessageCollector().add(
156 new LocalizedMessage(lineNo,
157 colNo,
158 getMessageBundle(),
159 key,
160 args,
161 getSeverityLevel(),
162 getId(),
163 this.getClass(),
164 this.getCustomMessages().get(key)));
165 }
166
167
168
169
170
171
172
173 protected final void fireErrors(String fileName)
174 {
175 final TreeSet<LocalizedMessage> errors = getMessageCollector()
176 .getMessages();
177 getMessageCollector().reset();
178 getMessageDispatcher().fireErrors(fileName, errors);
179 }
180 }