View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.ant;
5   
6   import java.util.ArrayList;
7   import java.util.Collection;
8   import java.util.Iterator;
9   import java.util.List;
10  
11  import net.sourceforge.pmd.ant.internal.PMDTaskImpl;
12  
13  import org.apache.tools.ant.BuildException;
14  import org.apache.tools.ant.Task;
15  import org.apache.tools.ant.types.FileSet;
16  import org.apache.tools.ant.types.Path;
17  import org.apache.tools.ant.types.Reference;
18  
19  public class PMDTask extends Task {
20  
21      private Path classpath;
22      private Path auxClasspath;
23      private final List<Formatter> formatters = new ArrayList<>();
24      private final List<FileSet> filesets = new ArrayList<>();
25      private boolean failOnError;
26      private boolean failOnRuleViolation;
27      private boolean shortFilenames;
28      private String suppressMarker;
29      private String rulesetFiles;
30      private String encoding;
31      private int threads;
32      private int minimumPriority;
33      private int maxRuleViolations = 0;
34      private String failuresPropertyName;
35      private SourceLanguage sourceLanguage;
36      private final Collection<RuleSetWrapper> nestedRules = new ArrayList<>();
37  
38      @Override
39      public void execute() throws BuildException {
40          validate();
41  
42          ClassLoader oldClassloader = Thread.currentThread().getContextClassLoader();
43          Thread.currentThread().setContextClassLoader(PMDTask.class.getClassLoader());
44          try {
45              PMDTaskImpl mirror = new PMDTaskImpl(this);
46              mirror.execute();
47          } finally {
48              Thread.currentThread().setContextClassLoader(oldClassloader);
49          }
50      }
51  
52      private void validate() throws BuildException {
53          if (formatters.isEmpty()) {
54              Formatter defaultFormatter = new Formatter();
55              defaultFormatter.setType("text");
56              defaultFormatter.setToConsole(true);
57              formatters.add(defaultFormatter);
58          } else {
59              for (Formatter f : formatters) {
60                  if (f.isNoOutputSupplied()) {
61                      throw new BuildException("toFile or toConsole needs to be specified in Formatter");
62                  }
63              }
64          }
65  
66          if (rulesetFiles == null) {
67              if (nestedRules.isEmpty()) {
68                  throw new BuildException("No rulesets specified");
69              }
70              rulesetFiles = getNestedRuleSetFiles();
71          }
72      }
73  
74      private String getNestedRuleSetFiles() {
75          final StringBuilder sb = new StringBuilder();
76          for (Iterator<RuleSetWrapper> it = nestedRules.iterator(); it.hasNext();) {
77              RuleSetWrapper rs = it.next();
78              sb.append(rs.getFile());
79              if (it.hasNext()) {
80                  sb.append(',');
81              }
82          }
83          return sb.toString();
84      }
85  
86      public void setShortFilenames(boolean reportShortNames) {
87          this.shortFilenames = reportShortNames;
88      }
89  
90      public void setSuppressMarker(String suppressMarker) {
91          this.suppressMarker = suppressMarker;
92      }
93  
94      public void setFailOnError(boolean fail) {
95          this.failOnError = fail;
96      }
97  
98      public void setFailOnRuleViolation(boolean fail) {
99          this.failOnRuleViolation = fail;
100     }
101 
102     public void setMaxRuleViolations(int max) {
103         if (max >= 0) {
104             this.maxRuleViolations = max;
105             this.failOnRuleViolation = true;
106         }
107     }
108 
109     public void setRuleSetFiles(String ruleSets) {
110         this.rulesetFiles = ruleSets;
111     }
112 
113     public void setEncoding(String sourceEncoding) {
114         this.encoding = sourceEncoding;
115     }
116 
117     public void setThreads(int threads) {
118         this.threads = threads;
119     }
120 
121     public void setFailuresPropertyName(String failuresPropertyName) {
122         this.failuresPropertyName = failuresPropertyName;
123     }
124 
125     public void setMinimumPriority(int minPriority) {
126         this.minimumPriority = minPriority;
127     }
128 
129     public void addFileset(FileSet set) {
130         filesets.add(set);
131     }
132 
133     public void addFormatter(Formatter f) {
134         formatters.add(f);
135     }
136 
137     public void addConfiguredSourceLanguage(SourceLanguage version) {
138         this.sourceLanguage = version;
139     }
140 
141     public void setClasspath(Path classpath) {
142         this.classpath = classpath;
143     }
144 
145     public Path getClasspath() {
146         return classpath;
147     }
148 
149     public Path createClasspath() {
150         if (classpath == null) {
151             classpath = new Path(getProject());
152         }
153         return classpath.createPath();
154     }
155 
156     public void setClasspathRef(Reference r) {
157         createClasspath().setRefid(r);
158     }
159 
160     public void setAuxClasspath(Path auxClasspath) {
161         this.auxClasspath = auxClasspath;
162     }
163 
164     public Path getAuxClasspath() {
165         return auxClasspath;
166     }
167 
168     public Path createAuxClasspath() {
169         if (auxClasspath == null) {
170             auxClasspath = new Path(getProject());
171         }
172         return auxClasspath.createPath();
173     }
174 
175     public void setAuxClasspathRef(Reference r) {
176         createAuxClasspath().setRefid(r);
177     }
178 
179     public void addRuleset(RuleSetWrapper r) {
180         nestedRules.add(r);
181     }
182 
183     public List<Formatter> getFormatters() {
184         return formatters;
185     }
186 
187     public List<FileSet> getFilesets() {
188         return filesets;
189     }
190 
191     public boolean isFailOnError() {
192         return failOnError;
193     }
194 
195     public boolean isFailOnRuleViolation() {
196         return failOnRuleViolation;
197     }
198 
199     public boolean isShortFilenames() {
200         return shortFilenames;
201     }
202 
203     public String getSuppressMarker() {
204         return suppressMarker;
205     }
206 
207     public String getRulesetFiles() {
208         return rulesetFiles;
209     }
210 
211     public String getEncoding() {
212         return encoding;
213     }
214 
215     public int getThreads() {
216         return threads;
217     }
218 
219     public int getMinimumPriority() {
220         return minimumPriority;
221     }
222 
223     public int getMaxRuleViolations() {
224         return maxRuleViolations;
225     }
226 
227     public String getFailuresPropertyName() {
228         return failuresPropertyName;
229     }
230 
231     public SourceLanguage getSourceLanguage() {
232         return sourceLanguage;
233     }
234 
235     public Collection<RuleSetWrapper> getNestedRules() {
236         return nestedRules;
237     }
238 
239 }