View Javadoc
1   /*
2    * Copyright (c) 2011-2024 Qulice.com
3    *
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met: 1) Redistributions of source code must retain the above
9    * copyright notice, this list of conditions and the following
10   * disclaimer. 2) Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following
12   * disclaimer in the documentation and/or other materials provided
13   * with the distribution. 3) Neither the name of the Qulice.com nor
14   * the names of its contributors may be used to endorse or promote
15   * products derived from this software without specific prior written
16   * permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29   * OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Validates source code with PMD.
44   *
45   * @since 0.3
46   */
47  public final class PmdValidator implements ResourceValidator {
48  
49      /**
50       * Environment to use.
51       */
52      private final transient Environment env;
53  
54      /**
55       * Constructor.
56       * @param env Environment to use.
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       * Filters out excluded files from further validation.
92       * @param files Files to validate
93       * @return Relevant source files
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 }