1
2
3
4
5 package com.qulice.pmd;
6
7 import com.jcabi.log.Logger;
8 import com.qulice.spi.Environment;
9 import com.qulice.spi.Relative;
10 import com.qulice.spi.ResourceValidator;
11 import com.qulice.spi.Violation;
12 import java.io.File;
13 import java.util.Collection;
14 import java.util.LinkedList;
15 import java.util.Locale;
16
17
18
19
20
21 public final class PmdValidator implements ResourceValidator {
22
23
24
25
26 private final transient Environment env;
27
28
29
30
31
32 public PmdValidator(final Environment env) {
33 this.env = env;
34 }
35
36 @Override
37 public Collection<Violation> validate(final Collection<File> files) {
38 final Collection<File> sources = this.getNonExcludedFiles(files);
39 final Collection<Violation> violations = new LinkedList<>();
40 if (sources.isEmpty()) {
41 Logger.debug(
42 this,
43 "No files to check with PMD, all %d are excluded",
44 files.size()
45 );
46 } else {
47 final SourceValidator validator = new SourceValidator(this.env.encoding());
48 final Collection<PmdError> errors = validator.validate(
49 sources, this.env.basedir().getPath()
50 );
51 for (final PmdError error : errors) {
52 violations.add(
53 new Violation.Default(
54 this.name(),
55 error.name(),
56 error.fileName(),
57 error.lines(),
58 error.description()
59 )
60 );
61 }
62 }
63 return violations;
64 }
65
66 @Override
67 public String name() {
68 return "PMD";
69 }
70
71
72
73
74
75
76 public Collection<File> getNonExcludedFiles(final Collection<File> files) {
77 final Collection<File> sources = new LinkedList<>();
78 for (final File file : files) {
79 final String name = new Relative(this.env.basedir(), file).path();
80 if (this.env.exclude("pmd", name)) {
81 continue;
82 }
83 if (!name.toLowerCase(Locale.ROOT).endsWith(".java")) {
84 continue;
85 }
86 sources.add(file);
87 }
88 return sources;
89 }
90 }