View Javadoc

    1   /**
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.cpd;
    5
    6   import java.io.BufferedWriter;
    7   import java.io.File;
    8   import java.io.FileOutputStream;
    9   import java.io.IOException;
    10  import java.io.OutputStream;
    11  import java.io.OutputStreamWriter;
    12  import java.io.Writer;
    13
    14  import net.sourceforge.pmd.util.IOUtil;
    15
    16  /**
    17   * @author Philippe T'Seyen
    18   */
    19  public class FileReporter {
    20      private File reportFile;
    21      private String encoding;
    22
    23      public FileReporter(String encoding) {
    24          this(null, encoding);
    25      }
    26
    27      public FileReporter(File reportFile) {
    28          this(reportFile, System.getProperty("file.encoding"));
    29      }
    30
    31      public FileReporter(File reportFile, String encoding) {
    32          this.reportFile = reportFile;
    33          this.encoding = encoding;
    34      }
    35
    36      public void report(String content) throws ReportException {
    37          try {
    38              Writer writer = null;
    39              try {
    40              	OutputStream outputStream;
    41              	if (reportFile == null) {
    42              		outputStream = System.out;
    43              	} else {
    44              		outputStream = new FileOutputStream(reportFile);
    45              	}
    46                  writer = new BufferedWriter(new OutputStreamWriter(outputStream, encoding));
    47                  writer.write(content);
    48              } finally {
    49                  IOUtil.closeQuietly(writer);
    50              }
    51          } catch (IOException ioe) {
    52              throw new ReportException(ioe);
    53          }
    54      }
    55  }