Clover coverage report - PMD - 3.9
Coverage timestamp: Tue Dec 19 2006 09:38:44 EST
file stats: LOC: 278   Methods: 21
NCLOC: 235   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PMDTask.java 9.3% 9.3% 23.8% 10.9%
coverage coverage
 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("1.6")) {
 146  0 log("Targeting Java language version 1.6", Project.MSG_VERBOSE);
 147  0 pmd = new PMD();
 148  0 pmd.setJavaVersion(SourceType.JAVA_16);
 149  0 } else if(targetJDK.equals("jsp")){
 150  0 log("Targeting JSP", Project.MSG_VERBOSE);
 151  0 pmd = new PMD();
 152  0 pmd.setJavaVersion(SourceType.JSP);
 153    } else {
 154  0 log("Targeting Java language version 1.4", Project.MSG_VERBOSE);
 155  0 pmd = new PMD();
 156    }
 157   
 158  0 if (excludeMarker != null) {
 159  0 log("Setting exclude marker to be " + excludeMarker, Project.MSG_VERBOSE);
 160  0 pmd.setExcludeMarker(excludeMarker);
 161    }
 162   
 163  0 RuleContext ctx = new RuleContext();
 164  0 Report report = new Report();
 165  0 ctx.setReport(report);
 166  0 report.start();
 167  0 for (Iterator i = filesets.iterator(); i.hasNext();) {
 168  0 FileSet fs = (FileSet) i.next();
 169  0 DirectoryScanner ds = fs.getDirectoryScanner(getProject());
 170  0 String[] srcFiles = ds.getIncludedFiles();
 171  0 for (int j = 0; j < srcFiles.length; j++) {
 172  0 File file = new File(ds.getBasedir() + System.getProperty("file.separator") + srcFiles[j]);
 173  0 log("Processing file " + file.getAbsoluteFile().toString(), Project.MSG_VERBOSE);
 174  0 ctx.setSourceCodeFilename(shortFilenames ? srcFiles[j] : file.getAbsolutePath());
 175  0 try {
 176  0 pmd.processFile(new BufferedInputStream(new FileInputStream(file)), encoding, rules, ctx);
 177    } catch (FileNotFoundException fnfe) {
 178  0 if (failOnError) {
 179  0 throw new BuildException(fnfe);
 180    }
 181    } catch (PMDException pmde) {
 182  0 log(pmde.toString(), Project.MSG_VERBOSE);
 183  0 if (pmde.getReason() != null) {
 184  0 StringWriter strWriter = new StringWriter();
 185  0 PrintWriter printWriter = new PrintWriter(strWriter);
 186  0 pmde.getReason().printStackTrace(printWriter);
 187  0 log(strWriter.toString(), Project.MSG_VERBOSE);
 188    }
 189  0 if (pmde.getReason() != null && pmde.getReason().getMessage() != null) {
 190  0 log(pmde.getReason().getMessage(), Project.MSG_VERBOSE);
 191    }
 192  0 if (failOnError) {
 193  0 throw new BuildException(pmde);
 194    }
 195  0 ctx.getReport().addError(new Report.ProcessingError(pmde.getMessage(), ctx.getSourceCodeFilename()));
 196    }
 197    }
 198    }
 199  0 report.end();
 200   
 201  0 log(ctx.getReport().size() + " problems found", Project.MSG_VERBOSE);
 202   
 203  0 for (Iterator i = formatters.iterator(); i.hasNext();) {
 204  0 Formatter formatter = (Formatter) i.next();
 205  0 log("Sending a report to " + formatter, Project.MSG_VERBOSE);
 206  0 formatter.outputReport(ctx.getReport(), getProject().getBaseDir().toString());
 207    }
 208   
 209  0 if (failuresPropertyName != null && ctx.getReport().size() > 0) {
 210  0 getProject().setProperty(failuresPropertyName, String.valueOf(ctx.getReport().size()));
 211  0 log("Setting property " + failuresPropertyName + " to " + ctx.getReport().size(), Project.MSG_VERBOSE);
 212    }
 213   
 214  0 if (failOnRuleViolation && ctx.getReport().size() > 0) {
 215  0 throw new BuildException("Stopping build since PMD found " + ctx.getReport().size() + " rule violations in the code");
 216    }
 217    }
 218   
 219  0 private void logRulesUsed(net.sourceforge.pmd.RuleSets rules) {
 220  0 log("Using these rulesets: " + ruleSetFiles, Project.MSG_VERBOSE);
 221   
 222  0 RuleSet[] ruleSets = rules.getAllRuleSets();
 223  0 for (int j = 0; j < ruleSets.length; j++) {
 224  0 RuleSet ruleSet = ruleSets[j];
 225   
 226  0 for (Iterator i = ruleSet.getRules().iterator(); i.hasNext();) {
 227  0 Rule rule = (Rule) i.next();
 228  0 log("Using rule " + rule.getName(), Project.MSG_VERBOSE);
 229    }
 230    }
 231    }
 232   
 233  5 private void validate() throws BuildException {
 234    // TODO - check for empty Formatters List here?
 235  5 for (Iterator i = formatters.iterator(); i.hasNext();) {
 236  2 Formatter f = (Formatter) i.next();
 237  2 if (f.isNoOutputSupplied()) {
 238  2 throw new BuildException("toFile or toConsole needs to be specified in Formatter");
 239    }
 240    }
 241   
 242  3 if (ruleSetFiles == null) {
 243  3 if (nestedRules.isEmpty()) {
 244  3 throw new BuildException("No rulesets specified");
 245    }
 246  0 ruleSetFiles = getNestedRuleSetFiles();
 247    }
 248   
 249  0 if (!targetJDK.equals("1.3") && !targetJDK.equals("1.4") && !targetJDK.equals("1.5") && !targetJDK.equals("1.6") && !targetJDK.equals("jsp")) {
 250  0 throw new BuildException("The targetjdk attribute, if used, must be set to either '1.3', '1.4', '1.5', '1.6' or 'jsp'");
 251    }
 252    }
 253   
 254  0 private String getNestedRuleSetFiles() {
 255  0 final StringBuffer sb = new StringBuffer();
 256  0 for (Iterator it = nestedRules.iterator(); it.hasNext();) {
 257  0 RuleSetWrapper rs = (RuleSetWrapper) it.next();
 258  0 sb.append(rs.getFile());
 259  0 if (it.hasNext()) {
 260  0 sb.append(',');
 261    }
 262    }
 263  0 return sb.toString();
 264    }
 265   
 266  0 private Path createLongClasspath() {
 267  0 if (classpath == null) {
 268  0 classpath = new Path(getProject());
 269    }
 270  0 return classpath.createPath();
 271    }
 272   
 273  2 public void addRuleset(RuleSetWrapper r) {
 274  2 nestedRules.add(r);
 275    }
 276   
 277    }
 278