Applied patch 1615546 - Added option to command line to write directly to a file

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4882 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Allan Caplan
2006-12-15 00:17:27 +00:00
parent 2ad29feced
commit e2f2298ffa
3 changed files with 38 additions and 6 deletions

View File

@ -27,6 +27,7 @@ Applied patch 1574988 - false + in OverrideBothEqualsAndHashcode
Applied patch 1583167 - Better test code management. Internal JUnits can now be written in XML's
Applied patch 1613674 - Support classpaths with spaces in pmd.bat
Applied patch 1615519 - controversial/DefaultPackage XPath rule is wrong
Applied patch 1615546 - Added option to command line to write directly to a file
Implemented RFE 1566313 - Command Line now takes minimumpriority attribute to filter out rulesets
PMD now requires JDK 1.4 to run
- PMD will still analyze code from earlier JDKs

View File

@ -28,6 +28,7 @@ public class CommandLineOptions {
private String excludeMarker = PMD.EXCLUDE_MARKER;
private String inputPath;
private String reportFormat;
private String reportFile;
private String ruleSets;
private String encoding = new InputStreamReader(System.in).getEncoding();
private String linePrefix;
@ -87,6 +88,8 @@ public class CommandLineOptions {
"minimumpriority parameter must be a whole number, {0} received",
new String[] { args[i] }));
}
} else if (args[i].equals("-reportfile")) {
reportFile = args[++i];
}
}
@ -141,6 +144,10 @@ public class CommandLineOptions {
return this.reportFormat;
}
public String getReportFile() {
return this.reportFile;
}
public String getRulesets() {
return this.ruleSets;
}
@ -180,15 +187,16 @@ public class CommandLineOptions {
"c:\\> java -jar pmd-" + PMD.VERSION + ".jar c:\\my\\source\\code html unusedcode" + PMD.EOL +
PMD.EOL +
"Optional arguments that may be put after the mandatory arguments are: " + PMD.EOL +
"-debug: prints debugging information " + PMD.EOL +
"-debug: prints debugging information" + PMD.EOL +
"-targetjdk: specifies a language version to target - 1.3, 1.4, 1.5 or 1.6" + PMD.EOL +
"-cpus: specifies the number of threads to create" + PMD.EOL +
"-encoding: specifies the character set encoding of the source code files PMD is reading (i.e., UTF-8)" + PMD.EOL +
"-excludemarker: specifies the String that marks the a line which PMD should ignore; default is NOPMD" + PMD.EOL +
"-shortnames: prints shortened filenames in the report" + PMD.EOL +
"-linkprefix: path to HTML source, for summary html renderer only." + PMD.EOL +
"-lineprefix: custom anchor to affected line in the source file, for summary html renderer only." + PMD.EOL +
"-minimumpriority: The rule priority threshold; rules with lower priority than they will not be used." + PMD.EOL +
"-linkprefix: path to HTML source, for summary html renderer only" + PMD.EOL +
"-lineprefix: custom anchor to affected line in the source file, for summary html renderer only" + PMD.EOL +
"-minimumpriority: rule priority threshold; rules with lower priority than they will not be used" + PMD.EOL +
"-reportfile: send report output to a file; default to System.out" + PMD.EOL +
PMD.EOL +
"For example: " + PMD.EOL +
"c:\\> java -jar pmd-" + PMD.VERSION + ".jar c:\\my\\source\\code text unusedcode,imports -targetjdk 1.5 -debug" + PMD.EOL +
@ -212,3 +220,5 @@ public class CommandLineOptions {
}

View File

@ -12,13 +12,16 @@ import net.sourceforge.pmd.sourcetypehandlers.SourceTypeHandler;
import net.sourceforge.pmd.sourcetypehandlers.SourceTypeHandlerBroker;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
@ -275,18 +278,34 @@ public class PMD {
}
report.end();
Writer w = null;
try {
Renderer r = opts.createRenderer();
OutputStreamWriter w = new OutputStreamWriter(System.out);
if (opts.getReportFile() != null) {
w = new BufferedWriter(new FileWriter(opts.getReportFile()));
} else {
w = new OutputStreamWriter(System.out);
}
r.render(w, ctx.getReport());
w.write(EOL);
w.flush();
System.out.println();
if (opts.getReportFile() != null) {
w.close();
}
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(opts.usage());
if (opts.debugEnabled()) {
e.printStackTrace();
}
} finally {
if (opts.getReportFile() != null && w != null) {
try {
w.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
@ -560,3 +579,5 @@ public class PMD {
}
}