/*
* JasperReports - Free Java Reporting Library.
* Copyright (C) 2001 - 2009 Jaspersoft Corporation. All rights reserved.
* http://www.jaspersoft.com
*
* Unless you have purchased a commercial license agreement from Jaspersoft,
* the following license terms apply:
*
* This program is part of JasperReports.
*
* JasperReports is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JasperReports 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Contributors:
* Mirko Wawrowsky - mawawrosky@users.sourceforge.net
*/
package net.sf.jasperreports.engine.export;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;
import java.util.StringTokenizer;
import net.sf.jasperreports.engine.JRAbstractExporter;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRPrintPage;
import net.sf.jasperreports.engine.JRPrintText;
import net.sf.jasperreports.engine.JRStyledTextAttributeSelector;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.util.JRProperties;
import net.sf.jasperreports.engine.util.JRStyledText;
/**
* Exports a JasperReports document to CSV format.
* @author Teodor Danciu (teodord@users.sourceforge.net)
* @version $Id: JRAbstractCsvExporter.java 4110 2010-12-27 12:07:43Z narcism $
*/
public abstract class JRAbstractCsvExporter extends JRAbstractExporter
{
private static final String CSV_EXPORTER_PROPERTIES_PREFIX = JRProperties.PROPERTY_PREFIX + "export.csv.";
/**
*
*/
protected String delimiter;
/**
*
*/
protected String recordDelimiter;
/**
*
*/
protected String encoding;
/**
*
*/
protected Writer writer;
protected JRExportProgressMonitor progressMonitor;
protected ExporterNature nature;
/**
*
*/
public void exportReport() throws JRException
{
progressMonitor = (JRExportProgressMonitor)parameters.get(JRExporterParameter.PROGRESS_MONITOR);
/* */
setOffset();
/* */
setInput();
if (!parameters.containsKey(JRExporterParameter.FILTER))
{
filter = createFilter(CSV_EXPORTER_PROPERTIES_PREFIX);
}
/* */
if (!isModeBatch)
{
setPageRange();
}
setParameters();
StringBuffer sb = (StringBuffer)parameters.get(JRExporterParameter.OUTPUT_STRING_BUFFER);
if (sb != null)
{
try
{
writer = new StringWriter();
exportReportToWriter();
sb.append(writer.toString());
}
catch (IOException e)
{
throw new JRException("Error writing to StringBuffer writer : " + jasperPrint.getName(), e);
}
finally
{
if (writer != null)
{
try
{
writer.close();
}
catch(IOException e)
{
}
}
}
}
else
{
writer = (Writer)parameters.get(JRExporterParameter.OUTPUT_WRITER);
if (writer != null)
{
try
{
exportReportToWriter();
}
catch (IOException e)
{
throw new JRException("Error writing to writer : " + jasperPrint.getName(), e);
}
}
else
{
OutputStream os = (OutputStream)parameters.get(JRExporterParameter.OUTPUT_STREAM);
if (os != null)
{
try
{
writer = new OutputStreamWriter(os, encoding);
exportReportToWriter();
}
catch (IOException e)
{
throw new JRException("Error writing to OutputStream writer : " + jasperPrint.getName(), e);
}
}
else
{
File destFile = (File)parameters.get(JRExporterParameter.OUTPUT_FILE);
if (destFile == null)
{
String fileName = (String)parameters.get(JRExporterParameter.OUTPUT_FILE_NAME);
if (fileName != null)
{
destFile = new File(fileName);
}
else
{
throw new JRException("No output specified for the exporter.");
}
}
try
{
os = new FileOutputStream(destFile);
writer = new OutputStreamWriter(os, encoding);
exportReportToWriter();
}
catch (IOException e)
{
throw new JRException("Error writing to file writer : " + jasperPrint.getName(), e);
}
finally
{
if (writer != null)
{
try
{
writer.close();
}
catch(IOException e)
{
}
}
}
}
}
}
}
/**
*
*/
protected void exportReportToWriter() throws JRException, IOException
{
for(int reportIndex = 0; reportIndex < jasperPrintList.size(); reportIndex++)
{
setJasperPrint((JasperPrint)jasperPrintList.get(reportIndex));
List pages = jasperPrint.getPages();
if (pages != null && pages.size() > 0)
{
if (isModeBatch)
{
startPageIndex = 0;
endPageIndex = pages.size() - 1;
}
for(int i = startPageIndex; i <= endPageIndex; i++)
{
if (Thread.interrupted())
{
throw new JRException("Current thread interrupted.");
}
JRPrintPage page = (JRPrintPage)pages.get(i);
/* */
exportPage(page);
}
}
}
writer.flush();
}
/**
*
*/
protected abstract void exportPage(JRPrintPage page) throws IOException;
/**
*
*/
protected JRStyledText getStyledText(JRPrintText textElement)
{
return textElement.getFullStyledText(JRStyledTextAttributeSelector.NONE);
}
/**
*
*/
protected String prepareText(String source)
{
String str = null;
if (source != null)
{
boolean putQuotes = false;
if (
source.indexOf(delimiter) >= 0
|| source.indexOf(recordDelimiter) >= 0
)
{
putQuotes = true;
}
StringBuffer sbuffer = new StringBuffer();
StringTokenizer tkzer = new StringTokenizer(source, "\"\n", true);
String token = null;
while(tkzer.hasMoreTokens())
{
token = tkzer.nextToken();
if ("\"".equals(token))
{
putQuotes = true;
sbuffer.append("\"\"");
}
else if ("\n".equals(token))
{
//sbuffer.append(" ");
putQuotes = true;
sbuffer.append("\n");
}
else
{
sbuffer.append(token);
}
}
str = sbuffer.toString();
if (putQuotes)
{
str = "\"" + str + "\"";
}
}
return str;
}
protected void setParameters()
{
nature = getExporterNature();
encoding =
getStringParameterOrDefault(
JRExporterParameter.CHARACTER_ENCODING,
JRExporterParameter.PROPERTY_CHARACTER_ENCODING
);
delimiter =
getStringParameterOrDefault(
JRCsvExporterParameter.FIELD_DELIMITER,
JRCsvExporterParameter.PROPERTY_FIELD_DELIMITER
);
recordDelimiter =
getStringParameterOrDefault(
JRCsvExporterParameter.RECORD_DELIMITER,
JRCsvExporterParameter.PROPERTY_RECORD_DELIMITER
);
}
protected ExporterNature getExporterNature()
{
if (nature == null)
{
nature = new JRCsvExporterNature(filter);
}
return nature;
}
protected String getExporterKey()
{
return null;
}
}