1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package com.qulice.pmd;
32
33 import com.qulice.spi.Environment;
34 import com.qulice.spi.ResourceValidator;
35 import com.qulice.spi.Violation;
36 import java.io.File;
37 import java.util.Collection;
38 import java.util.LinkedList;
39 import net.sourceforge.pmd.util.datasource.DataSource;
40 import net.sourceforge.pmd.util.datasource.FileDataSource;
41
42
43
44
45
46
47 public final class PmdValidator implements ResourceValidator {
48
49
50
51
52 private final transient Environment env;
53
54
55
56
57
58 public PmdValidator(final Environment env) {
59 this.env = env;
60 }
61
62 @Override
63 @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
64 public Collection<Violation> validate(final Collection<File> files) {
65 final SourceValidator validator = new SourceValidator(this.env);
66 final Collection<DataSource> sources = this.getNonExcludedFiles(files);
67 final Collection<PmdError> errors = validator.validate(
68 sources, this.env.basedir().getPath()
69 );
70 final Collection<Violation> violations = new LinkedList<>();
71 for (final PmdError error : errors) {
72 violations.add(
73 new Violation.Default(
74 this.name(),
75 error.name(),
76 error.fileName(),
77 error.lines(),
78 error.description()
79 )
80 );
81 }
82 return violations;
83 }
84
85 @Override
86 public String name() {
87 return "PMD";
88 }
89
90
91
92
93
94
95 public Collection<DataSource> getNonExcludedFiles(final Collection<File> files) {
96 final Collection<DataSource> sources = new LinkedList<>();
97 for (final File file : files) {
98 final String name = file.getPath().substring(
99 this.env.basedir().toString().length()
100 );
101 if (this.env.exclude("pmd", name)) {
102 continue;
103 }
104 if (!name.matches("^.*\\.java$")) {
105 continue;
106 }
107 sources.add(new FileDataSource(file));
108 }
109 return sources;
110 }
111 }