modified Ant task to use formatter element vs separate reportFile and format attributes

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1276 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2002-12-09 15:36:04 +00:00
parent 3cb8d95d4e
commit 32be595c10
4 changed files with 59 additions and 51 deletions

View File

@ -58,8 +58,10 @@
<target name="pmd">
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>
<!--<pmd reportFile="c:\jdk-1.4.html" rulesetfiles="rulesets/tmp.xml" format="html">-->
<pmd reportFile="c:\jdk-1.4.html" rulesetfiles="rulesets/tmp.xml" format="html">
<fileset dir="c:\j2sdk1.4.1_01\src\java\">
<pmd rulesetfiles="rulesets/imports.xml">
<formatter type="xml" toFile="c:\foo.xml"/>
<formatter type="html" toFile="c:\foo.html"/>
<fileset dir="c:\j2sdk1.4.1_01\src\java\lang\reflect">
<include name="**/*.java"/>
<!--<exclude name="**/ast**/"/>-->
</fileset>

View File

@ -1,7 +1,8 @@
???? ?? 2002 - 1.02:
???? ?? ???? - 1.02:
Added new rule: ImportFromSamePackageRule
Updated LooseCouplingRule to check for usage of Vector; thx to Vladimir for the good catch.
Updated AvoidDuplicateLiteralsRule to report the line number of the first occurrence of the duplicate String.
Modified Ant task to use a formatter element; this lets you render a report in several formats without having to rerun PMD.
November 07 2002 - 1.01:
Fixed bug 633879: EmptyFinallyBlockRule now handles multiple catch blocks followed by a finally block.

View File

@ -27,12 +27,28 @@ import java.util.List;
public class PMDTask extends Task {
public static class Formatter {
private Renderer renderer;
private String toFile;
public void setType(String type) {
if (type.equals("xml")) {
renderer = new XMLRenderer();
} else if (type.equals("html")) {
renderer = new HTMLRenderer();
} else {
throw new BuildException("Formatter type must be either 'xml' or 'html'; you specified " + type);
}
}
public void setToFile(String toFile) {this.toFile = toFile;}
public Renderer getRenderer() {return renderer;}
public String getToFile() {return toFile;}
}
private List formatters = new ArrayList();
private List filesets = new ArrayList();
private String reportFile;
private boolean verbose;
private boolean printToConsole;
private String ruleSetFiles;
private String format;
private boolean failOnError;
/**
@ -60,21 +76,13 @@ public class PMDTask extends Task {
filesets.add(set);
}
public void setReportFile(String reportFile) {
this.reportFile = reportFile;
}
public void setFormat(String format) {
this.format = format;
public void addFormatter(Formatter f) {
formatters.add(f);
}
public void execute() throws BuildException {
if (reportFile == null) {
throw new BuildException("No report file specified");
}
if (format == null || (!format.equals("xml") && !format.equals("html"))) {
throw new BuildException("Renderer format must be either 'xml' or 'html'; you specified " + format);
if (formatters.isEmpty()) {
throw new BuildException("No formatter specified");
}
RuleSet rules = null;
@ -97,7 +105,7 @@ public class PMDTask extends Task {
for (int j=0; j<srcFiles.length; j++) {
try {
File file = new File(ds.getBasedir() + System.getProperty("file.separator") + srcFiles[j]);
if (verbose) System.out.println(file.getAbsoluteFile());
printIfVerbose (file.getAbsoluteFile().toString());
ctx.setSourceCodeFilename(file.getAbsolutePath());
@ -108,31 +116,31 @@ public class PMDTask extends Task {
}
}
StringBuffer buf = new StringBuffer();
if (!ctx.getReport().isEmpty()) {
Renderer rend = null;
if (format.equals("xml")) {
rend = new XMLRenderer();
} else {
rend = new HTMLRenderer();
for (Iterator i = formatters.iterator(); i.hasNext();) {
Formatter formatter = (Formatter)i.next();
String buffer = formatter.getRenderer().render(ctx.getReport()) + EOL;
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(formatter.getToFile())));
writer.write(buffer, 0, buffer.length());
writer.close();
} catch (IOException ioe) {
throw new BuildException(ioe.getMessage());
}
}
buf.append(rend.render(ctx.getReport()));
buf.append(EOL);
if (printToConsole) {
Renderer r = new TextRenderer();
System.out.println(r.render(report));
}
}
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(reportFile)));
writer.write(buf.toString(), 0, buf.length());
writer.close();
} catch (IOException ioe) {
throw new BuildException(ioe.getMessage());
if (!ctx.getReport().isEmpty() && printToConsole) {
Renderer r = new TextRenderer();
System.out.println(r.render(report));
}
if (failOnError && !ctx.getReport().isEmpty()) {
throw new BuildException("Stopping build since PMD found problems in the code");
}
}
private void printIfVerbose(String in) {
if (verbose) System.out.println(in);
}
}

View File

@ -10,7 +10,7 @@
<body>
<section name="PMD">
<subsection name="Description">
<p>Runs a set of static code analysis rules on some Java source code files and generates a list of problems found.</p>
<p>Runs a set of static code analysis rules on Java source code files and generates a list of problems found.</p>
</subsection>
<subsection name="Parameters">
<table border="1" cellpadding="2" cellspacing="0">
@ -20,27 +20,17 @@
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">reportfile</td>
<td valign="top">The file to which the report is written.</td>
<td valign="top">formatter</td>
<td valign="top">Specifies formats and files to which the report is written. Can be of type 'xml' or 'html'. See example below.</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">verbose</td>
<td valign="top">Verbose output - names of files processed, rule counts, etc.</td>
<td valign="top" align="center">No</td>
</tr>
<tr>
<td valign="top">rulesetfiles</td>
<td valign="top">A comma delimited list of ruleset files ('rulesets/basic.xml,rulesets/design.xml'). If you write
your own ruleset files, you can put them on the classpath and plug them in here.</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">format</td>
<td valign="top">The report format (xml, html)</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">failonerror</td>
<td valign="top">Whether or not to fail the build if PMD finds any problems</td>
@ -51,6 +41,11 @@
<td valign="top">Whether or not to print any problems found to the Ant log/console</td>
<td valign="top" align="center">No</td>
</tr>
<tr>
<td valign="top">verbose</td>
<td valign="top">Verbose output - names of files processed, rule counts, etc.</td>
<td valign="top" align="center">No</td>
</tr>
</table>
</subsection>
@ -59,7 +54,8 @@
<![CDATA[
<target name="pmd">
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" />
<pmd reportFile="C:\foo.xml" rulesetfiles="rulesets/unusedcode.xml" format="xml">
<pmd rulesetfiles="rulesets/unusedcode.xml">
<formatter type="html" toFile="c:\jdk-report.html"/>
<fileset dir="C:\j2sdk1.4.0\src\">
<include name="**/*.java"/>
</fileset>
@ -68,7 +64,8 @@
<target name="pmd">
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" />
<pmd reportFile="C:\foo.xml" verbose="false" rulesetfiles="rulesets/basic.xml,rulesets/design.xml" format="xml" failonerror="no">
<pmd verbose="true" rulesetfiles="rulesets/basic.xml,rulesets/design.xml" failonerror="no">
<formatter type="xml" toFile="c:\jdk-report.xml"/>
<fileset dir="C:\j2sdk1.4.0\src\">
<include name="**/*.java"/>
</fileset>