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