01: package org.acm.seguin.pmd.ant;
02:
03: import org.acm.seguin.pmd.renderers.HTMLRenderer;
04: import org.acm.seguin.pmd.renderers.Renderer;
05: import org.acm.seguin.pmd.renderers.TextRenderer;
06: import org.acm.seguin.pmd.renderers.XMLRenderer;
07: import org.acm.seguin.pmd.renderers.CSVRenderer;
08: import org.apache.tools.ant.BuildException;
09:
10: import java.io.BufferedWriter;
11: import java.io.File;
12: import java.io.FileWriter;
13: import java.io.IOException;
14: import java.io.Writer;
15:
16: public class Formatter {
17:
18: private Renderer renderer;
19: private File toFile;
20:
21: public void setType(String type) {
22: if (type.equals("xml")) {
23: renderer = new XMLRenderer();
24: } else if (type.equals("html")) {
25: renderer = new HTMLRenderer();
26: } else if (type.equals("text")) {
27: renderer = new TextRenderer();
28: } else if (type.equals("csv")) {
29: renderer = new CSVRenderer();
30: } else if (!type.equals("")) {
31: try {
32: renderer = (Renderer) Class.forName(type).newInstance();
33: } catch (Exception e) {
34: e.printStackTrace();
35: throw new BuildException(
36: "Unable to instantiate custom formatter: "
37: + type);
38: }
39: } else {
40: throw new BuildException(
41: "Formatter type must be 'xml', 'text', 'html', or a class name; you specified "
42: + type);
43: }
44: }
45:
46: public void setToFile(File toFile) {
47: this .toFile = toFile;
48: }
49:
50: public Renderer getRenderer() {
51: return renderer;
52: }
53:
54: public boolean isToFileNull() {
55: return toFile == null;
56: }
57:
58: public Writer getToFileWriter(String baseDir) throws IOException {
59: if (!toFile.isAbsolute()) {
60: return new BufferedWriter(new FileWriter(new File(baseDir
61: + System.getProperty("file.separator")
62: + toFile.getPath())));
63: }
64: return new BufferedWriter(new FileWriter(toFile));
65: }
66:
67: public String toString() {
68: return "file = " + toFile + "; renderer = "
69: + renderer.getClass().getName();
70: }
71: }
|