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 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      public void setShowSuppressed(boolean value) {
    36          this.showSuppressed = value;
    37      }
    38
    39      public void setType(String type) {
    40          this.type = type;
    41      }
    42
    43      public void setLinkPrefix(String linkPrefix) {
    44          this.linkPrefix = linkPrefix;
    45      }
    46
    47      public void setToFile(File toFile) {
    48          this.toFile = toFile;
    49      }
    50
    51      public void setToConsole(boolean toConsole) {
    52          this.toConsole = toConsole;
    53      }
    54
    55      public void outputReport(Report report, String baseDir) {
    56          try {
    57              if (toConsole) {
    58                  outputReportTo(new BufferedWriter(new OutputStreamWriter(System.out)), report, true);
    59              }
    60              if (toFile != null) {
    61                  outputReportTo(getToFileWriter(baseDir), report, false);
    62              }
    63          } catch (IOException ioe) {
    64              throw new BuildException(ioe.getMessage());
    65          }
    66      }
    67
    68      private void outputReportTo(Writer writer, Report report, boolean consoleRenderer) throws IOException {
    69          String renderedReport = getRenderer(consoleRenderer).render(report) + PMD.EOL;
    70          writer.write(renderedReport, 0, renderedReport.length());
    71          writer.close();
    72      }
    73
    74      public boolean isNoOutputSupplied() {
    75          return toFile == null && !toConsole;
    76      }
    77
    78      public String toString() {
    79          return "file = " + toFile + "; renderer = " + type;
    80      }
    81
    82      private Renderer getRenderer(boolean consoleRenderer) {
    83          Renderer renderer;
    84          if (type.equals("xml")) {
    85              renderer = new XMLRenderer();
    86          } else if (type.equals("html")) {
    87              renderer = new HTMLRenderer(linkPrefix);
    88          } else if (type.equals("summaryhtml")) {
    89              renderer = new SummaryHTMLRenderer();
    90          } else if (type.equals("papari")) {
    91              renderer = new PapariTextRenderer();
    92          } else if (type.equals("csv")) {
    93              renderer = new CSVRenderer();
    94          } else if (type.equals("text")) {
    95              renderer = new TextRenderer();
    96          } else if (type.equals("emacs")) {
    97              renderer = new EmacsRenderer();
    98          } else if (type.equals("vbhtml")) {
    99              renderer = new VBHTMLRenderer();
    100         } else if (type.equals("yahtml")) {
    101             renderer = new YAHTMLRenderer();
    102         } else if (!type.equals("")) {
    103             try {
    104                 renderer = (Renderer) Class.forName(type).newInstance();
    105             } catch (Exception e) {
    106                 throw new BuildException("Unable to instantiate custom formatter: " + type);
    107             }
    108         } else {
    109             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         renderer.showSuppressedViolations(showSuppressed);
    112         return renderer;
    113     }
    114
    115     private Writer getToFileWriter(String baseDir) throws IOException {
    116         if (!toFile.isAbsolute()) {
    117             return new BufferedWriter(new FileWriter(new File(baseDir + System.getProperty("file.separator") + toFile.getPath())));
    118         }
    119         return new BufferedWriter(new FileWriter(toFile));
    120     }
    121
    122 }