View Javadoc
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 java.io.BufferedWriter;
7   import java.io.File;
8   import java.io.FileWriter;
9   import java.io.IOException;
10  import java.io.OutputStreamWriter;
11  import java.io.Writer;
12  import java.util.ArrayList;
13  import java.util.List;
14  import java.util.Properties;
15  
16  import net.sourceforge.pmd.Report;
17  import net.sourceforge.pmd.renderers.Renderer;
18  import net.sourceforge.pmd.renderers.RendererFactory;
19  import net.sourceforge.pmd.util.StringUtil;
20  
21  import org.apache.tools.ant.BuildException;
22  import org.apache.tools.ant.types.Parameter;
23  
24  public class Formatter {
25  
26      private File toFile;
27      private String type;
28      private boolean toConsole;
29      private boolean showSuppressed;
30      private final List<Parameter> parameters = new ArrayList<>();
31  
32      public void setShowSuppressed(boolean value) {
33          this.showSuppressed = value;
34      }
35  
36      public void setType(String type) {
37          this.type = type;
38      }
39  
40      public void setToFile(File toFile) {
41          this.toFile = toFile;
42      }
43  
44      public void setToConsole(boolean toConsole) {
45          this.toConsole = toConsole;
46      }
47  
48      public void addConfiguredParam(Parameter parameter) {
49  	this.parameters.add(parameter);
50      }
51  
52      private Writer writer;
53  
54      private Renderer renderer;
55  
56      public Renderer getRenderer() {
57          return renderer;
58      }
59  
60      public void start(String baseDir) {
61          try {
62              if (toConsole) {
63                  writer = new BufferedWriter(new OutputStreamWriter(System.out));
64              }
65              if (toFile != null) {
66                  writer = getToFileWriter(baseDir);
67              }
68              renderer = createRenderer();
69              renderer.setWriter(writer);
70              renderer.start();
71          } catch (IOException ioe) {
72              throw new BuildException(ioe.getMessage(), ioe);
73          }
74      }
75  
76      public void end(Report errorReport) {
77          try {
78              renderer.renderFileReport(errorReport);
79              renderer.end();
80              if (toConsole) {
81                  writer.flush();
82              } else {
83                  writer.close();
84              }
85          } catch (IOException ioe) {
86              throw new BuildException(ioe.getMessage(), ioe);
87          }
88      }
89  
90      public boolean isNoOutputSupplied() {
91          return toFile == null && !toConsole;
92      }
93  
94      @Override
95      public String toString() {
96          return "file = " + toFile + "; renderer = " + type;
97      }
98  
99      private static String[] validRendererCodes() {
100         return RendererFactory.REPORT_FORMAT_TO_RENDERER.keySet().toArray(new String[RendererFactory.REPORT_FORMAT_TO_RENDERER.size()]);
101     }
102 
103     private static String unknownRendererMessage(String userSpecifiedType) {
104         String[] typeCodes = validRendererCodes();
105     	StringBuilder sb = new StringBuilder(100);
106         sb.append("Formatter type must be one of: '")
107           .append(typeCodes[0]);
108         for (int i = 1; i < typeCodes.length; i++) {
109             sb.append("', '").append(typeCodes[i]);
110         }
111         sb.append("', or a class name; you specified: ")
112           .append(userSpecifiedType);
113         return sb.toString();
114     }
115 
116     // FIXME - hm, what about this consoleRenderer thing... need a test for this
117     Renderer createRenderer() {
118         if (StringUtil.isEmpty(type)) {
119             throw new BuildException(unknownRendererMessage("<unspecified>"));
120         }
121 
122         Properties properties = createProperties();
123         Renderer renderer = RendererFactory.createRenderer(type, properties);
124         renderer.setShowSuppressedViolations(showSuppressed);
125         return renderer;
126     }
127 
128     private Properties createProperties() {
129 	Properties properties = new Properties();
130 	for (Parameter parameter : parameters) {
131 	    properties.put(parameter.getName(), parameter.getValue());
132 	}
133 	return properties;
134     }
135 
136     private Writer getToFileWriter(String baseDir) throws IOException {
137         if (!toFile.isAbsolute()) {
138             return new BufferedWriter(new FileWriter(new File(baseDir + System.getProperty("file.separator") + toFile.getPath())));
139         }
140         return new BufferedWriter(new FileWriter(toFile));
141     }
142 }