Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT FRAMES NO FRAMES |

file stats: | LOC: | 274 |  | Methods: | 21
| NCLOC: | 231 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL
PMDTask.java | 9.6% | 9.6% | 23.8% | 11.2% |

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 net.sourceforge.pmd.PMD;
7 |  | import net.sourceforge.pmd.PMDException;
8 |  | import net.sourceforge.pmd.Report;
9 |  | import net.sourceforge.pmd.Rule;
10 |  | import net.sourceforge.pmd.RuleContext;
11 |  | import net.sourceforge.pmd.RuleSet;
12 |  | import net.sourceforge.pmd.RuleSetFactory;
13 |  | import net.sourceforge.pmd.RuleSetNotFoundException;
14 |  | import net.sourceforge.pmd.RuleSets;
15 |  | import net.sourceforge.pmd.SimpleRuleSetNameMapper;
16 |  | import net.sourceforge.pmd.SourceType;
17 |  | import org.apache.tools.ant.AntClassLoader;
18 |  | import org.apache.tools.ant.BuildException;
19 |  | import org.apache.tools.ant.DirectoryScanner;
20 |  | import org.apache.tools.ant.Project;
21 |  | import org.apache.tools.ant.Task;
22 |  | import org.apache.tools.ant.types.FileSet;
23 |  | import org.apache.tools.ant.types.Path;
24 |  | import org.apache.tools.ant.types.Reference;
25 |  |
26 |  | import java.io.BufferedInputStream;
27 |  | import java.io.File;
28 |  | import java.io.FileInputStream;
29 |  | import java.io.FileNotFoundException;
30 |  | import java.io.PrintWriter;
31 |  | import java.io.StringWriter;
32 |  | import java.util.ArrayList;
33 |  | import java.util.Collection;
34 |  | import java.util.Iterator;
35 |  | import java.util.List;
36 |  |
37 |  | public class PMDTask extends Task {
38 |  |
39 |  | private Path classpath;
40 |  | private List formatters = new ArrayList();
41 |  | private List filesets = new ArrayList();
42 |  | private int minPriority = Rule.LOWEST_PRIORITY;
43 |  | private boolean shortFilenames;
44 |  | private String ruleSetFiles;
45 |  | private String encoding = System.getProperty("file.encoding");
46 |  | private boolean failOnError;
47 |  | private boolean failOnRuleViolation;
48 |  | private String targetJDK = "1.4";
49 |  | private String failuresPropertyName;
50 |  | private String excludeMarker;
51 |  | private final Collection nestedRules = new ArrayList();
52 |  |
53 | 0 | public void setShortFilenames(boolean value) {
54 | 0 | this.shortFilenames = value;
55 |  | }
56 |  |
57 | 1 | public void setTargetJDK(String value) {
58 | 1 | this.targetJDK = value;
59 |  | }
60 |  |
61 | 0 | public void setExcludeMarker(String value) {
62 | 0 | this.excludeMarker = value;
63 |  | }
64 |  |
65 | 0 | public void setFailOnError(boolean fail) {
66 | 0 | this.failOnError = fail;
67 |  | }
68 |  |
69 | 0 | public void setFailOnRuleViolation(boolean fail) {
70 | 0 | this.failOnRuleViolation = fail;
71 |  | }
72 |  |
73 | 0 | public void setRuleSetFiles(String ruleSetFiles) {
74 | 0 | this.ruleSetFiles = ruleSetFiles;
75 |  | }
76 |  |
77 | 0 | public void setEncoding(String encoding) {
78 | 0 | this.encoding = encoding;
79 |  | }
80 |  |
81 | 0 | public void setFailuresPropertyName(String failuresPropertyName) {
82 | 0 | this.failuresPropertyName = failuresPropertyName;
83 |  | }
84 |  |
85 | 0 | public void setMinimumPriority(int minPriority) {
86 | 0 | this.minPriority = minPriority;
87 |  | }
88 |  |
89 | 0 | public void addFileset(FileSet set) {
90 | 0 | filesets.add(set);
91 |  | }
92 |  |
93 | 2 | public void addFormatter(Formatter f) {
94 | 2 | formatters.add(f);
95 |  | }
96 |  |
97 | 0 | public void setClasspath(Path classpath) {
98 | 0 | this.classpath = classpath;
99 |  | }
100 |  |
101 | 0 | public Path getClasspath() {
102 | 0 | return classpath;
103 |  | }
104 |  |
105 | 0 | public Path createClasspath() {
106 | 0 | if (classpath == null) {
107 | 0 | classpath = new Path(getProject());
108 |  | }
109 | 0 | return classpath.createPath();
110 |  | }
111 |  |
112 | 0 | public void setClasspathRef(Reference r) {
113 | 0 | createLongClasspath().setRefid(r);
114 |  | }
115 |  |
116 | 5 | public void execute() throws BuildException {
117 | 5 | validate();
118 |  |
119 | 0 | ruleSetFiles = new SimpleRuleSetNameMapper(ruleSetFiles).getRuleSets();
120 | 0 | RuleSets rules;
121 | 0 | try {
122 | 0 | RuleSetFactory ruleSetFactory = new RuleSetFactory();
123 | 0 | ruleSetFactory.setMinimumPriority(minPriority);
124 | 0 | if (classpath == null) {
125 | 0 | log("Using the normal ClassLoader", Project.MSG_VERBOSE);
126 | 0 | rules = ruleSetFactory.createRuleSets(ruleSetFiles);
127 |  | } else {
128 | 0 | log("Using the AntClassLoader", Project.MSG_VERBOSE);
129 | 0 | rules = ruleSetFactory.createRuleSets(ruleSetFiles, new AntClassLoader(getProject(), classpath));
130 |  | }
131 |  | } catch (RuleSetNotFoundException e) {
132 | 0 | throw new BuildException(e.getMessage());
133 |  | }
134 | 0 | logRulesUsed(rules);
135 |  |
136 | 0 | PMD pmd;
137 | 0 | if (targetJDK.equals("1.3")) {
138 | 0 | log("Targeting Java language version 1.3", Project.MSG_VERBOSE);
139 | 0 | pmd = new PMD();
140 | 0 | pmd.setJavaVersion(SourceType.JAVA_13);
141 | 0 | } else if (targetJDK.equals("1.5")) {
142 | 0 | log("Targeting Java language version 1.5", Project.MSG_VERBOSE);
143 | 0 | pmd = new PMD();
144 | 0 | pmd.setJavaVersion(SourceType.JAVA_15);
145 | 0 | } else if(targetJDK.equals("jsp")){
146 | 0 | log("Targeting JSP", Project.MSG_VERBOSE);
147 | 0 | pmd = new PMD();
148 | 0 | pmd.setJavaVersion(SourceType.JSP);
149 |  | } else {
150 | 0 | log("Targeting Java language version 1.4", Project.MSG_VERBOSE);
151 | 0 | pmd = new PMD();
152 |  | }
153 |  |
154 | 0 | if (excludeMarker != null) {
155 | 0 | log("Setting exclude marker to be " + excludeMarker, Project.MSG_VERBOSE);
156 | 0 | pmd.setExcludeMarker(excludeMarker);
157 |  | }
158 |  |
159 | 0 | RuleContext ctx = new RuleContext();
160 | 0 | Report report = new Report();
161 | 0 | ctx.setReport(report);
162 | 0 | report.start();
163 | 0 | for (Iterator i = filesets.iterator(); i.hasNext();) {
164 | 0 | FileSet fs = (FileSet) i.next();
165 | 0 | DirectoryScanner ds = fs.getDirectoryScanner(getProject());
166 | 0 | String[] srcFiles = ds.getIncludedFiles();
167 | 0 | for (int j = 0; j < srcFiles.length; j++) {
168 | 0 | File file = new File(ds.getBasedir() + System.getProperty("file.separator") + srcFiles[j]);
169 | 0 | log("Processing file " + file.getAbsoluteFile().toString(), Project.MSG_VERBOSE);
170 | 0 | ctx.setSourceCodeFilename(shortFilenames ? srcFiles[j] : file.getAbsolutePath());
171 | 0 | try {
172 | 0 | pmd.processFile(new BufferedInputStream(new FileInputStream(file)), encoding, rules, ctx);
173 |  | } catch (FileNotFoundException fnfe) {
174 | 0 | if (failOnError) {
175 | 0 | throw new BuildException(fnfe);
176 |  | }
177 |  | } catch (PMDException pmde) {
178 | 0 | log(pmde.toString(), Project.MSG_VERBOSE);
179 | 0 | if (pmde.getReason() != null) {
180 | 0 | StringWriter strWriter = new StringWriter();
181 | 0 | PrintWriter printWriter = new PrintWriter(strWriter);
182 | 0 | pmde.getReason().printStackTrace(printWriter);
183 | 0 | log(strWriter.toString(), Project.MSG_VERBOSE);
184 |  | }
185 | 0 | if (pmde.getReason() != null && pmde.getReason().getMessage() != null) {
186 | 0 | log(pmde.getReason().getMessage(), Project.MSG_VERBOSE);
187 |  | }
188 | 0 | if (failOnError) {
189 | 0 | throw new BuildException(pmde);
190 |  | }
191 | 0 | ctx.getReport().addError(new Report.ProcessingError(pmde.getMessage(), ctx.getSourceCodeFilename()));
192 |  | }
193 |  | }
194 |  | }
195 | 0 | report.end();
196 |  |
197 | 0 | log(ctx.getReport().size() + " problems found", Project.MSG_VERBOSE);
198 |  |
199 | 0 | for (Iterator i = formatters.iterator(); i.hasNext();) {
200 | 0 | Formatter formatter = (Formatter) i.next();
201 | 0 | log("Sending a report to " + formatter, Project.MSG_VERBOSE);
202 | 0 | formatter.outputReport(ctx.getReport(), getProject().getBaseDir().toString());
203 |  | }
204 |  |
205 | 0 | if (failuresPropertyName != null && ctx.getReport().size() > 0) {
206 | 0 | getProject().setProperty(failuresPropertyName, String.valueOf(ctx.getReport().size()));
207 | 0 | log("Setting property " + failuresPropertyName + " to " + String.valueOf(ctx.getReport().size()), Project.MSG_VERBOSE);
208 |  | }
209 |  |
210 | 0 | if (failOnRuleViolation && ctx.getReport().size() > 0) {
211 | 0 | throw new BuildException("Stopping build since PMD found " + ctx.getReport().size() + " rule violations in the code");
212 |  | }
213 |  | }
214 |  |
215 | 0 | private void logRulesUsed(net.sourceforge.pmd.RuleSets rules) {
216 | 0 | log("Using these rulesets: " + ruleSetFiles, Project.MSG_VERBOSE);
217 |  |
218 | 0 | RuleSet[] ruleSets = rules.getAllRuleSets();
219 | 0 | for (int j = 0; j < ruleSets.length; j++) {
220 | 0 | RuleSet ruleSet = ruleSets[j];
221 |  |
222 | 0 | for (Iterator i = ruleSet.getRules().iterator(); i.hasNext();) {
223 | 0 | Rule rule = (Rule) i.next();
224 | 0 | log("Using rule " + rule.getName(), Project.MSG_VERBOSE);
225 |  | }
226 |  | }
227 |  | }
228 |  |
229 | 5 | private void validate() throws BuildException {
230 |  | // TODO - check for empty Formatters List here?
231 | 5 | for (Iterator i = formatters.iterator(); i.hasNext();) {
232 | 2 | Formatter f = (Formatter) i.next();
233 | 2 | if (f.isNoOutputSupplied()) {
234 | 2 | throw new BuildException("toFile or toConsole needs to be specified in Formatter");
235 |  | }
236 |  | }
237 |  |
238 | 3 | if (ruleSetFiles == null) {
239 | 3 | if (nestedRules.isEmpty()) {
240 | 3 | throw new BuildException("No rulesets specified");
241 |  | }
242 | 0 | ruleSetFiles = getNestedRuleSetFiles();
243 |  | }
244 |  |
245 | 0 | if (!targetJDK.equals("1.3") && !targetJDK.equals("1.4") && !targetJDK.equals("1.5") && !targetJDK.equals("jsp")) {
246 | 0 | throw new BuildException("The targetjdk attribute, if used, must be set to either '1.3', '1.4', or '1.5', or 'jsp'");
247 |  | }
248 |  | }
249 |  |
250 | 0 | private String getNestedRuleSetFiles() {
251 | 0 | final StringBuffer sb = new StringBuffer();
252 | 0 | for (Iterator it = nestedRules.iterator(); it.hasNext();) {
253 | 0 | RuleSetWrapper rs = (RuleSetWrapper) it.next();
254 | 0 | sb.append(rs.getFile());
255 | 0 | if (it.hasNext()) {
256 | 0 | sb.append(',');
257 |  | }
258 |  | }
259 | 0 | return sb.toString();
260 |  | }
261 |  |
262 | 0 | private Path createLongClasspath() {
263 | 0 | if (classpath == null) {
264 | 0 | classpath = new Path(getProject());
265 |  | }
266 | 0 | return classpath.createPath();
267 |  | }
268 |  |
269 | 2 | public void addRuleset(RuleSetWrapper r) {
270 | 2 | nestedRules.add(r);
271 |  | }
272 |  |
273 |  | }
274 |  |