com.greenpepper.server.rpc.runner.report.FileReportGenerator Maven / Gradle / Ivy
/**
* Copyright (c) 2008 Pyxis Technologies inc.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA,
* or see the FSF site: http://www.fsf.org.
*
* @author oaouattara
* @version $Id: $Id
*/
package com.greenpepper.server.rpc.runner.report;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import com.greenpepper.util.Factory;
import com.greenpepper.util.IOUtil;
public class FileReportGenerator
implements ReportGenerator
{
private final File reportsDirectory;
private Class reportClass;
private boolean automaticExtension;
/**
* Constructor for FileReportGenerator.
*
* @param outputDir a {@link java.io.File} object.
*/
public FileReportGenerator(File outputDir)
{
this.reportsDirectory = outputDir;
this.reportClass = HtmlReport.class;
}
/**
* Setter for the field reportClass
.
*
* @param reportClass a {@link java.lang.Class} object.
*/
public void setReportClass(Class reportClass)
{
this.reportClass = reportClass;
}
/**
* adjustReportFilesExtensions.
*
* @param enable a boolean.
*/
public void adjustReportFilesExtensions(boolean enable)
{
this.automaticExtension = enable;
}
/** {@inheritDoc} */
public Report openReport(String name)
{
Factory factory = new Factory(reportClass);
return factory.newInstance(name);
}
/** {@inheritDoc} */
public void closeReport(Report report)
throws IOException
{
FileWriter out = null;
try
{
File reportFile = new File(reportsDirectory, outputNameOf(report));
IOUtil.createDirectoryTree(reportFile.getParentFile());
out = new FileWriter(reportFile);
report.printTo(out);
out.flush();
}
finally
{
IOUtil.closeQuietly(out);
}
}
private String outputNameOf(Report report)
{
String name = report.getName();
if (automaticExtension && !name.endsWith(extensionOf(report)))
{
name += extensionOf(report);
}
return name;
}
private String extensionOf(Report report)
{
return "." + report.getType();
}
}