|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| package net.sourceforge.pmd; |
|
5 |
| |
|
6 |
| import net.sourceforge.pmd.ast.ParseException; |
|
7 |
| import net.sourceforge.pmd.cpd.FileFinder; |
|
8 |
| import net.sourceforge.pmd.cpd.SourceFileOrDirectoryFilter; |
|
9 |
| import net.sourceforge.pmd.parsers.Parser; |
|
10 |
| import net.sourceforge.pmd.renderers.Renderer; |
|
11 |
| import net.sourceforge.pmd.sourcetypehandlers.SourceTypeHandler; |
|
12 |
| import net.sourceforge.pmd.sourcetypehandlers.SourceTypeHandlerBroker; |
|
13 |
| |
|
14 |
| import java.io.BufferedInputStream; |
|
15 |
| import java.io.File; |
|
16 |
| import java.io.FileNotFoundException; |
|
17 |
| import java.io.IOException; |
|
18 |
| import java.io.InputStream; |
|
19 |
| import java.io.InputStreamReader; |
|
20 |
| import java.io.Reader; |
|
21 |
| import java.io.UnsupportedEncodingException; |
|
22 |
| import java.util.ArrayList; |
|
23 |
| import java.util.Enumeration; |
|
24 |
| import java.util.Iterator; |
|
25 |
| import java.util.List; |
|
26 |
| import java.util.StringTokenizer; |
|
27 |
| import java.util.zip.ZipEntry; |
|
28 |
| import java.util.zip.ZipFile; |
|
29 |
| |
|
30 |
| public class PMD { |
|
31 |
| public static final String EOL = System.getProperty("line.separator", "\n"); |
|
32 |
| public static final String VERSION = "3.7"; |
|
33 |
| |
|
34 |
| private String excludeMarker = ExcludeLines.EXCLUDE_MARKER; |
|
35 |
| private SourceTypeDiscoverer sourceTypeDiscoverer = new SourceTypeDiscoverer(); |
|
36 |
| private SourceTypeHandlerBroker sourceTypeHandlerBroker = new SourceTypeHandlerBroker(); |
|
37 |
| |
|
38 |
1054
| public PMD() {
|
|
39 |
| } |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
1
| public PMD(TargetJDKVersion targetJDKVersion) {
|
|
46 |
1
| if (targetJDKVersion instanceof TargetJDK1_3) {
|
|
47 |
0
| setJavaVersion(SourceType.JAVA_13);
|
|
48 |
1
| } else if (targetJDKVersion instanceof TargetJDK1_5) {
|
|
49 |
0
| setJavaVersion(SourceType.JAVA_15);
|
|
50 |
| } |
|
51 |
| } |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
3
| public void processFile(Reader reader, RuleSets ruleSets, RuleContext ctx)
|
|
63 |
| throws PMDException { |
|
64 |
3
| SourceType sourceType = getSourceTypeOfFile(ctx.getSourceCodeFilename());
|
|
65 |
| |
|
66 |
3
| processFile(reader, ruleSets, ctx, sourceType);
|
|
67 |
| } |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
1055
| public void processFile(Reader reader, RuleSets ruleSets, RuleContext ctx,
|
|
80 |
| SourceType sourceType) throws PMDException { |
|
81 |
1055
| try {
|
|
82 |
1055
| SourceTypeHandler sourceTypeHandler = sourceTypeHandlerBroker
|
|
83 |
| .getVisitorsFactoryForSourceType(sourceType); |
|
84 |
| |
|
85 |
1055
| ExcludeLines excluder = new ExcludeLines(reader, excludeMarker);
|
|
86 |
1055
| ctx.excludeLines(excluder.getLinesToExclude());
|
|
87 |
| |
|
88 |
1055
| Parser parser = sourceTypeHandler.getParser();
|
|
89 |
1055
| Object rootNode = parser.parse(excluder.getCopyReader());
|
|
90 |
1055
| Thread.yield();
|
|
91 |
| |
|
92 |
| |
|
93 |
1055
| sourceTypeHandler.getSymbolFacade().start(rootNode);
|
|
94 |
| |
|
95 |
1055
| Language language = SourceTypeToRuleLanguageMapper.getMappedLanguage(sourceType);
|
|
96 |
| |
|
97 |
1055
| if (ruleSets.usesDFA(language)) {
|
|
98 |
0
| sourceTypeHandler.getDataFlowFacade().start(rootNode);
|
|
99 |
| } |
|
100 |
| |
|
101 |
1055
| List acus = new ArrayList();
|
|
102 |
1055
| acus.add(rootNode);
|
|
103 |
| |
|
104 |
1055
| ruleSets.apply(acus, ctx, language);
|
|
105 |
| } catch (ParseException pe) { |
|
106 |
0
| throw new PMDException("Error while parsing "
|
|
107 |
| + ctx.getSourceCodeFilename(), pe); |
|
108 |
| } catch (Exception e) { |
|
109 |
0
| throw new PMDException("Error while processing "
|
|
110 |
| + ctx.getSourceCodeFilename(), e); |
|
111 |
| } finally { |
|
112 |
1055
| try {
|
|
113 |
1055
| reader.close();
|
|
114 |
| } catch (IOException e) { |
|
115 |
0
| throw new PMDException("Error while closing "
|
|
116 |
| + ctx.getSourceCodeFilename(), e); |
|
117 |
| } |
|
118 |
| } |
|
119 |
| } |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
3
| private SourceType getSourceTypeOfFile(String fileName) {
|
|
132 |
3
| SourceType sourceType = sourceTypeDiscoverer.getSourceTypeOfFile(fileName);
|
|
133 |
3
| if (sourceType == null) {
|
|
134 |
| |
|
135 |
| |
|
136 |
3
| sourceType = sourceTypeDiscoverer.getSourceTypeOfJavaFiles();
|
|
137 |
| } |
|
138 |
3
| return sourceType;
|
|
139 |
| } |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
3
| public void processFile(Reader reader, RuleSet ruleSet, RuleContext ctx)
|
|
151 |
| throws PMDException { |
|
152 |
3
| processFile(reader, new RuleSets(ruleSet), ctx);
|
|
153 |
| } |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
0
| public void processFile(InputStream fileContents, String encoding,
|
|
167 |
| RuleSet ruleSet, RuleContext ctx) throws PMDException { |
|
168 |
0
| try {
|
|
169 |
0
| processFile(new InputStreamReader(fileContents, encoding), ruleSet, ctx);
|
|
170 |
| } catch (UnsupportedEncodingException uee) { |
|
171 |
0
| throw new PMDException("Unsupported encoding exception: "
|
|
172 |
| + uee.getMessage()); |
|
173 |
| } |
|
174 |
| } |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
0
| public void processFile(InputStream fileContents, String encoding,
|
|
188 |
| RuleSets ruleSets, RuleContext ctx) throws PMDException { |
|
189 |
0
| try {
|
|
190 |
0
| processFile(new InputStreamReader(fileContents, encoding), ruleSets, ctx);
|
|
191 |
| } catch (UnsupportedEncodingException uee) { |
|
192 |
0
| throw new PMDException("Unsupported encoding exception: "
|
|
193 |
| + uee.getMessage()); |
|
194 |
| } |
|
195 |
| } |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
0
| public void processFile(InputStream fileContents, RuleSet ruleSet,
|
|
209 |
| RuleContext ctx) throws PMDException { |
|
210 |
0
| processFile(fileContents, System.getProperty("file.encoding"), ruleSet, ctx);
|
|
211 |
| } |
|
212 |
| |
|
213 |
0
| public void setExcludeMarker(String marker) {
|
|
214 |
0
| this.excludeMarker = marker;
|
|
215 |
| } |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
0
| public void setJavaVersion(SourceType javaVersion) {
|
|
223 |
0
| sourceTypeDiscoverer.setSourceTypeOfJavaFiles(javaVersion);
|
|
224 |
| } |
|
225 |
| |
|
226 |
0
| public static void main(String[] args) {
|
|
227 |
0
| CommandLineOptions opts = new CommandLineOptions(args);
|
|
228 |
| |
|
229 |
0
| SourceFileSelector fileSelector = new SourceFileSelector();
|
|
230 |
| |
|
231 |
0
| fileSelector.setSelectJavaFiles(opts.isCheckJavaFiles());
|
|
232 |
0
| fileSelector.setSelectJspFiles(opts.isCheckJspFiles());
|
|
233 |
| |
|
234 |
0
| List files;
|
|
235 |
0
| if (opts.containsCommaSeparatedFileList()) {
|
|
236 |
0
| files = collectFromCommaDelimitedString(opts.getInputPath(),
|
|
237 |
| fileSelector); |
|
238 |
| } else { |
|
239 |
0
| files = collectFilesFromOneName(opts.getInputPath(), fileSelector);
|
|
240 |
| } |
|
241 |
| |
|
242 |
0
| PMD pmd = new PMD();
|
|
243 |
0
| if (opts.getTargetJDK().equals("1.3")) {
|
|
244 |
0
| if (opts.debugEnabled())
|
|
245 |
0
| System.out.println("In JDK 1.3 mode");
|
|
246 |
0
| pmd.setJavaVersion(SourceType.JAVA_13);
|
|
247 |
0
| } else if (opts.getTargetJDK().equals("1.5")) {
|
|
248 |
0
| if (opts.debugEnabled())
|
|
249 |
0
| System.out.println("In JDK 1.5 mode");
|
|
250 |
0
| pmd.setJavaVersion(SourceType.JAVA_15);
|
|
251 |
| } else { |
|
252 |
0
| if (opts.debugEnabled())
|
|
253 |
0
| System.out.println("In JDK 1.4 mode");
|
|
254 |
0
| pmd.setJavaVersion(SourceType.JAVA_14);
|
|
255 |
| } |
|
256 |
0
| pmd.setExcludeMarker(opts.getExcludeMarker());
|
|
257 |
| |
|
258 |
0
| RuleContext ctx = new RuleContext();
|
|
259 |
0
| Report report = new Report();
|
|
260 |
0
| ctx.setReport(report);
|
|
261 |
0
| report.start();
|
|
262 |
| |
|
263 |
0
| try {
|
|
264 |
0
| RuleSetFactory ruleSetFactory = new RuleSetFactory();
|
|
265 |
0
| RuleSets rulesets = ruleSetFactory.createRuleSets(opts.getRulesets());
|
|
266 |
0
| printRuleNamesInDebug(opts.debugEnabled(), rulesets);
|
|
267 |
| |
|
268 |
0
| pmd.processFiles(files, ctx, rulesets, opts.debugEnabled(), opts
|
|
269 |
| .shortNamesEnabled(), opts.getInputPath(), opts.getEncoding()); |
|
270 |
| } catch (FileNotFoundException fnfe) { |
|
271 |
0
| System.out.println(opts.usage());
|
|
272 |
0
| fnfe.printStackTrace();
|
|
273 |
| } catch (RuleSetNotFoundException rsnfe) { |
|
274 |
0
| System.out.println(opts.usage());
|
|
275 |
0
| rsnfe.printStackTrace();
|
|
276 |
| } catch (IOException ioe) { |
|
277 |
0
| System.out.println(opts.usage());
|
|
278 |
0
| ioe.printStackTrace();
|
|
279 |
| } |
|
280 |
0
| report.end();
|
|
281 |
| |
|
282 |
0
| try {
|
|
283 |
0
| Renderer r = opts.createRenderer();
|
|
284 |
0
| System.out.println(r.render(ctx.getReport()));
|
|
285 |
| } catch (Exception e) { |
|
286 |
0
| System.out.println(e.getMessage());
|
|
287 |
0
| System.out.println(opts.usage());
|
|
288 |
0
| if (opts.debugEnabled()) {
|
|
289 |
0
| e.printStackTrace();
|
|
290 |
| } |
|
291 |
| } |
|
292 |
| } |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
0
| public void processFiles(List files, RuleContext ctx, RuleSets rulesets,
|
|
308 |
| boolean debugEnabled, boolean shortNamesEnabled, String inputPath, |
|
309 |
| String encoding) throws IOException { |
|
310 |
0
| for (Iterator i = files.iterator(); i.hasNext();) {
|
|
311 |
0
| DataSource dataSource = (DataSource) i.next();
|
|
312 |
| |
|
313 |
0
| String niceFileName = dataSource.getNiceFileName(shortNamesEnabled,
|
|
314 |
| inputPath); |
|
315 |
0
| ctx.setSourceCodeFilename(niceFileName);
|
|
316 |
0
| if (debugEnabled) {
|
|
317 |
0
| System.out.println("Processing " + ctx.getSourceCodeFilename());
|
|
318 |
| } |
|
319 |
| |
|
320 |
0
| try {
|
|
321 |
0
| InputStream stream = new BufferedInputStream(dataSource
|
|
322 |
| .getInputStream()); |
|
323 |
0
| processFile(stream, encoding, rulesets, ctx);
|
|
324 |
| } catch (PMDException pmde) { |
|
325 |
0
| if (debugEnabled) {
|
|
326 |
0
| pmde.getReason().printStackTrace();
|
|
327 |
| } |
|
328 |
0
| ctx.getReport().addError(new Report.ProcessingError(pmde.getMessage(), niceFileName));
|
|
329 |
| } |
|
330 |
| } |
|
331 |
| } |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
| |
|
338 |
| |
|
339 |
0
| private static void printRuleNamesInDebug(boolean debugEnabled, RuleSets rulesets) {
|
|
340 |
0
| if (debugEnabled) {
|
|
341 |
0
| for (Iterator i = rulesets.getAllRules().iterator(); i.hasNext();) {
|
|
342 |
0
| Rule r = (Rule) i.next();
|
|
343 |
0
| System.out.println("Loaded rule " + r.getName());
|
|
344 |
| } |
|
345 |
| } |
|
346 |
| } |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
| |
|
351 |
| |
|
352 |
| |
|
353 |
| |
|
354 |
| |
|
355 |
| |
|
356 |
0
| private static List collectFilesFromOneName(String inputFileName,
|
|
357 |
| SourceFileSelector fileSelector) { |
|
358 |
0
| return collect(inputFileName, fileSelector);
|
|
359 |
| } |
|
360 |
| |
|
361 |
| |
|
362 |
| |
|
363 |
| |
|
364 |
| |
|
365 |
| |
|
366 |
| |
|
367 |
| |
|
368 |
0
| private static List collectFromCommaDelimitedString(String fileList,
|
|
369 |
| SourceFileSelector fileSelector) { |
|
370 |
0
| List files = new ArrayList();
|
|
371 |
0
| for (StringTokenizer st = new StringTokenizer(fileList, ","); st
|
|
372 |
| .hasMoreTokens();) { |
|
373 |
0
| files.addAll(collect(st.nextToken(), fileSelector));
|
|
374 |
| } |
|
375 |
0
| return files;
|
|
376 |
| } |
|
377 |
| |
|
378 |
| |
|
379 |
| |
|
380 |
| |
|
381 |
| |
|
382 |
| |
|
383 |
| |
|
384 |
| |
|
385 |
| |
|
386 |
0
| private static List collect(String filename, SourceFileSelector fileSelector) {
|
|
387 |
0
| File inputFile = new File(filename);
|
|
388 |
0
| if (!inputFile.exists()) {
|
|
389 |
0
| throw new RuntimeException("File " + inputFile.getName()
|
|
390 |
| + " doesn't exist"); |
|
391 |
| } |
|
392 |
0
| List dataSources = new ArrayList();
|
|
393 |
0
| if (!inputFile.isDirectory()) {
|
|
394 |
0
| if (filename.endsWith(".zip") || filename.endsWith(".jar")) {
|
|
395 |
0
| ZipFile zipFile;
|
|
396 |
0
| try {
|
|
397 |
0
| zipFile = new ZipFile(inputFile);
|
|
398 |
0
| Enumeration e = zipFile.entries();
|
|
399 |
0
| while (e.hasMoreElements()) {
|
|
400 |
0
| ZipEntry zipEntry = (ZipEntry) e.nextElement();
|
|
401 |
0
| if (fileSelector.isWantedFile(zipEntry.getName())) {
|
|
402 |
0
| dataSources.add(new ZipDataSource(zipFile, zipEntry));
|
|
403 |
| } |
|
404 |
| } |
|
405 |
| } catch (IOException ze) { |
|
406 |
0
| throw new RuntimeException("Zip file " + inputFile.getName()
|
|
407 |
| + " can't be opened"); |
|
408 |
| } |
|
409 |
| } else { |
|
410 |
0
| dataSources.add(new FileDataSource(inputFile));
|
|
411 |
| } |
|
412 |
| } else { |
|
413 |
0
| FileFinder finder = new FileFinder();
|
|
414 |
0
| List files = finder.findFilesFrom(inputFile.getAbsolutePath(),
|
|
415 |
| new SourceFileOrDirectoryFilter(fileSelector), true); |
|
416 |
0
| for (Iterator i = files.iterator(); i.hasNext();) {
|
|
417 |
0
| dataSources.add(new FileDataSource((File) i.next()));
|
|
418 |
| } |
|
419 |
| } |
|
420 |
0
| return dataSources;
|
|
421 |
| } |
|
422 |
| |
|
423 |
| } |