Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT FRAMES NO FRAMES |
file stats: | LOC: | 122 |  | Methods: | 11
| NCLOC: | 103 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
Formatter.java | 0% | 4.4% | 18.2% | 4.9% |

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.Report;
8 |  | import net.sourceforge.pmd.renderers.CSVRenderer;
9 |  | import net.sourceforge.pmd.renderers.EmacsRenderer;
10 |  | import net.sourceforge.pmd.renderers.HTMLRenderer;
11 |  | import net.sourceforge.pmd.renderers.PapariTextRenderer;
12 |  | import net.sourceforge.pmd.renderers.Renderer;
13 |  | import net.sourceforge.pmd.renderers.SummaryHTMLRenderer;
14 |  | import net.sourceforge.pmd.renderers.TextRenderer;
15 |  | import net.sourceforge.pmd.renderers.VBHTMLRenderer;
16 |  | import net.sourceforge.pmd.renderers.XMLRenderer;
17 |  | import net.sourceforge.pmd.renderers.YAHTMLRenderer;
18 |  | import org.apache.tools.ant.BuildException;
19 |  |
20 |  | import java.io.BufferedWriter;
21 |  | import java.io.File;
22 |  | import java.io.FileWriter;
23 |  | import java.io.IOException;
24 |  | import java.io.OutputStreamWriter;
25 |  | import java.io.Writer;
26 |  |
27 |  | public class Formatter {
28 |  |
29 |  | private File toFile;
30 |  | private String linkPrefix;
31 |  | private String type;
32 |  | private boolean toConsole;
33 |  | private boolean showSuppressed;
34 |  |
35 | 0 | public void setShowSuppressed(boolean value) {
36 | 0 | this.showSuppressed = value;
37 |  | }
38 |  |
39 | 0 | public void setType(String type) {
40 | 0 | this.type = type;
41 |  | }
42 |  |
43 | 0 | public void setLinkPrefix(String linkPrefix) {
44 | 0 | this.linkPrefix = linkPrefix;
45 |  | }
46 |  |
47 | 1 | public void setToFile(File toFile) {
48 | 1 | this.toFile = toFile;
49 |  | }
50 |  |
51 | 0 | public void setToConsole(boolean toConsole) {
52 | 0 | this.toConsole = toConsole;
53 |  | }
54 |  |
55 | 0 | public void outputReport(Report report, String baseDir) {
56 | 0 | try {
57 | 0 | if (toConsole) {
58 | 0 | outputReportTo(new BufferedWriter(new OutputStreamWriter(System.out)), report, true);
59 |  | }
60 | 0 | if (toFile != null) {
61 | 0 | outputReportTo(getToFileWriter(baseDir), report, false);
62 |  | }
63 |  | } catch (IOException ioe) {
64 | 0 | throw new BuildException(ioe.getMessage());
65 |  | }
66 |  | }
67 |  |
68 | 0 | private void outputReportTo(Writer writer, Report report, boolean consoleRenderer) throws IOException {
69 | 0 | String renderedReport = getRenderer(consoleRenderer).render(report) + PMD.EOL;
70 | 0 | writer.write(renderedReport, 0, renderedReport.length());
71 | 0 | writer.close();
72 |  | }
73 |  |
74 | 4 | public boolean isNoOutputSupplied() {
75 | 4 | return toFile == null && !toConsole;
76 |  | }
77 |  |
78 | 0 | public String toString() {
79 | 0 | return "file = " + toFile + "; renderer = " + type;
80 |  | }
81 |  |
82 | 0 | private Renderer getRenderer(boolean consoleRenderer) {
83 | 0 | Renderer renderer;
84 | 0 | if (type.equals("xml")) {
85 | 0 | renderer = new XMLRenderer();
86 | 0 | } else if (type.equals("html")) {
87 | 0 | renderer = new HTMLRenderer(linkPrefix);
88 | 0 | } else if (type.equals("summaryhtml")) {
89 | 0 | renderer = new SummaryHTMLRenderer();
90 | 0 | } else if (type.equals("papari")) {
91 | 0 | renderer = new PapariTextRenderer();
92 | 0 | } else if (type.equals("csv")) {
93 | 0 | renderer = new CSVRenderer();
94 | 0 | } else if (type.equals("text")) {
95 | 0 | renderer = new TextRenderer();
96 | 0 | } else if (type.equals("emacs")) {
97 | 0 | renderer = new EmacsRenderer();
98 | 0 | } else if (type.equals("vbhtml")) {
99 | 0 | renderer = new VBHTMLRenderer();
100 | 0 | } else if (type.equals("yahtml")) {
101 | 0 | renderer = new YAHTMLRenderer();
102 | 0 | } else if (!type.equals("")) {
103 | 0 | try {
104 | 0 | renderer = (Renderer) Class.forName(type).newInstance();
105 |  | } catch (Exception e) {
106 | 0 | throw new BuildException("Unable to instantiate custom formatter: " + type);
107 |  | }
108 |  | } else {
109 | 0 | throw new BuildException("Formatter type must be 'xml', 'text', 'html', 'emacs', 'summaryhtml', 'papari', 'csv', 'vbhtml', 'yahtml', or a class name; you specified " + type);
110 |  | }
111 | 0 | renderer.showSuppressedViolations(showSuppressed);
112 | 0 | return renderer;
113 |  | }
114 |  |
115 | 0 | private Writer getToFileWriter(String baseDir) throws IOException {
116 | 0 | if (!toFile.isAbsolute()) {
117 | 0 | return new BufferedWriter(new FileWriter(new File(baseDir + System.getProperty("file.separator") + toFile.getPath())));
118 |  | }
119 | 0 | return new BufferedWriter(new FileWriter(toFile));
120 |  | }
121 |  |
122 |  | }