Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractFileSetCheck |
|
| 1.9285714285714286;1.929 |
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.api; | |
20 | ||
21 | import java.io.File; | |
22 | import java.util.List; | |
23 | import java.util.TreeSet; | |
24 | ||
25 | /** | |
26 | * Provides common functionality for many FileSetChecks. | |
27 | * | |
28 | * @author lkuehne | |
29 | * @author oliver | |
30 | */ | |
31 | 595 | public abstract class AbstractFileSetCheck |
32 | extends AbstractViolationReporter | |
33 | implements FileSetCheck | |
34 | { | |
35 | /** The dispatcher errors are fired to. */ | |
36 | private MessageDispatcher mDispatcher; | |
37 | ||
38 | /** the file extensions that are accepted by this filter */ | |
39 | 595 | private String[] mFileExtensions = {}; |
40 | ||
41 | /** collects the error messages */ | |
42 | 595 | private final LocalizedMessages mMessages = new LocalizedMessages(); |
43 | ||
44 | /** | |
45 | * Called to process a file that matches the specified file extensions. | |
46 | * @param aFile the file to be processed | |
47 | * @param aLines an immutable list of the contents of the file. | |
48 | */ | |
49 | protected abstract void processFiltered(File aFile, List<String> aLines); | |
50 | ||
51 | /** {@inheritDoc} */ | |
52 | public void init() | |
53 | { | |
54 | 0 | } |
55 | ||
56 | /** {@inheritDoc} */ | |
57 | public void destroy() | |
58 | { | |
59 | 582 | } |
60 | ||
61 | /** {@inheritDoc} */ | |
62 | public void beginProcessing(String aCharset) | |
63 | { | |
64 | 583 | } |
65 | ||
66 | /** {@inheritDoc} */ | |
67 | public final TreeSet<LocalizedMessage> process(File aFile, | |
68 | List<String> aLines) | |
69 | { | |
70 | 586 | getMessageCollector().reset(); |
71 | // Process only what interested in | |
72 | 586 | if (fileExtensionMatches(aFile)) { |
73 | 586 | processFiltered(aFile, aLines); |
74 | } | |
75 | 586 | return getMessageCollector().getMessages(); |
76 | } | |
77 | ||
78 | /** {@inheritDoc} */ | |
79 | public void finishProcessing() | |
80 | { | |
81 | 583 | } |
82 | ||
83 | /** {@inheritDoc} */ | |
84 | public final void setMessageDispatcher(MessageDispatcher aDispatcher) | |
85 | { | |
86 | 583 | mDispatcher = aDispatcher; |
87 | 583 | } |
88 | ||
89 | /** | |
90 | * A message dispatcher is used to fire violation messages to | |
91 | * interested audit listeners. | |
92 | * | |
93 | * @return the current MessageDispatcher. | |
94 | */ | |
95 | protected final MessageDispatcher getMessageDispatcher() | |
96 | { | |
97 | 16 | return mDispatcher; |
98 | } | |
99 | ||
100 | /** | |
101 | * Sets the file extensions that identify the files that pass the | |
102 | * filter of this FileSetCheck. | |
103 | * @param aExtensions the set of file extensions. A missing | |
104 | * initial '.' character of an extension is automatically added. | |
105 | */ | |
106 | public final void setFileExtensions(String[] aExtensions) | |
107 | { | |
108 | 555 | if (aExtensions == null) { |
109 | 0 | mFileExtensions = null; |
110 | 0 | return; |
111 | } | |
112 | ||
113 | 555 | mFileExtensions = new String[aExtensions.length]; |
114 | 1110 | for (int i = 0; i < aExtensions.length; i++) { |
115 | 555 | final String extension = aExtensions[i]; |
116 | 555 | if (extension.startsWith(".")) { |
117 | 0 | mFileExtensions[i] = extension; |
118 | } | |
119 | else { | |
120 | 555 | mFileExtensions[i] = "." + extension; |
121 | } | |
122 | } | |
123 | 555 | } |
124 | ||
125 | /** | |
126 | * Returns the collector for violation messages. | |
127 | * Subclasses can use the collector to find out the violation | |
128 | * messages to fire via the message dispatcher. | |
129 | * | |
130 | * @return the collector for localized messages. | |
131 | */ | |
132 | protected final LocalizedMessages getMessageCollector() | |
133 | { | |
134 | 2869 | return mMessages; |
135 | } | |
136 | ||
137 | @Override | |
138 | public final void log(int aLine, String aKey, Object... aArgs) | |
139 | { | |
140 | 34 | log(aLine, 0, aKey, aArgs); |
141 | 34 | } |
142 | ||
143 | @Override | |
144 | public final void log(int aLineNo, int aColNo, String aKey, | |
145 | Object... aArgs) | |
146 | { | |
147 | 43 | getMessageCollector().add( |
148 | new LocalizedMessage(aLineNo, | |
149 | aColNo, | |
150 | getMessageBundle(), | |
151 | aKey, | |
152 | aArgs, | |
153 | getSeverityLevel(), | |
154 | getId(), | |
155 | this.getClass(), | |
156 | this.getCustomMessages().get(aKey))); | |
157 | 43 | } |
158 | ||
159 | /** | |
160 | * Notify all listeners about the errors in a file. | |
161 | * Calls <code>MessageDispatcher.fireErrors()</code> with | |
162 | * all logged errors and than clears errors' list. | |
163 | * @param aFileName the audited file | |
164 | */ | |
165 | protected final void fireErrors(String aFileName) | |
166 | { | |
167 | 8 | final TreeSet<LocalizedMessage> errors = getMessageCollector() |
168 | .getMessages(); | |
169 | 8 | getMessageCollector().reset(); |
170 | 8 | getMessageDispatcher().fireErrors(aFileName, errors); |
171 | 8 | } |
172 | ||
173 | /** | |
174 | * Returns whether the file extension matches what we are meant to | |
175 | * process. | |
176 | * @param aFile the file to be checked. | |
177 | * @return whether there is a match. | |
178 | */ | |
179 | private boolean fileExtensionMatches(File aFile) | |
180 | { | |
181 | 586 | if ((null == mFileExtensions) || (mFileExtensions.length == 0)) { |
182 | 34 | return true; |
183 | } | |
184 | ||
185 | // normalize extensions so all of them have a leading dot | |
186 | 552 | final String[] withDotExtensions = new String[mFileExtensions.length]; |
187 | 1104 | for (int i = 0; i < mFileExtensions.length; i++) { |
188 | 552 | final String extension = mFileExtensions[i]; |
189 | 552 | if (extension.startsWith(".")) { |
190 | 552 | withDotExtensions[i] = extension; |
191 | } | |
192 | else { | |
193 | 0 | withDotExtensions[i] = "." + extension; |
194 | } | |
195 | } | |
196 | ||
197 | 552 | final String fileName = aFile.getName(); |
198 | 552 | for (final String fileExtension : withDotExtensions) { |
199 | 552 | if (fileName.endsWith(fileExtension)) { |
200 | 552 | return true; |
201 | } | |
202 | } | |
203 | ||
204 | 0 | return false; |
205 | } | |
206 | } |