org.efaps.dataexporter.DataWriter Maven / Gradle / Ivy
Show all versions of data-exporter Show documentation
/*
* #%L
* data-exporter
* %%
* Copyright (C) 2012 - 2013 http://www.brsanthu.com
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package org.efaps.dataexporter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.lang3.StringEscapeUtils;
import org.efaps.dataexporter.model.BeanRow;
import org.efaps.dataexporter.model.CellDetails;
import org.efaps.dataexporter.model.Column;
import org.efaps.dataexporter.model.DataExporterCallback;
import org.efaps.dataexporter.model.HeaderCellDetails;
import org.efaps.dataexporter.model.Row;
import org.efaps.dataexporter.model.RowDetails;
import org.efaps.dataexporter.model.Table;
import org.efaps.dataexporter.util.Util;
/**
* Workhorse of the library, which coordinates and exports the header, rows and footer as
* appropriate. This method has list of abstract classes, so called hooks, which subclasses
* can make use to write the data at appropriate time.
*
* Subclasses must extend {@link AbstractDataWriter} instead of this class.
*
* @author Santhosh Kumar
*/
public abstract class DataWriter {
protected ExportOptions options = null;
private PrintWriter out = null;
private boolean autoFlush = true;
private AtomicInteger rowIndex = new AtomicInteger();
/**
* Initializes DataWriter with given export options and output stream
*
* @param options the options to use during exporting. Cannot be null
.
* @param out the output stream to write the data to. Cannot be null
.
*/
public DataWriter(ExportOptions options, OutputStream out) {
this(options, new OutputStreamWriter(out));
}
/**
* Initializes the datawriter with given export options and writer.
*
* @param options the options to use during exporting. Cannot be null
.
* @param out the output writer to write the data to. Cannot be null
.
*/
public DataWriter(ExportOptions options, Writer out) {
Util.checkForNotNull(out, "out");
Util.checkForNotNull(options, "options");
this.options = options;
this.out = new PrintWriter(out);
}
public void setOutputStream(PrintWriter out) {
this.out = out;
}
/**
* Returns the currently active options.
*
* @return the current options
*/
public ExportOptions getOptions() {
return options;
}
/**
* This method could be used to export the given table and rows.
*
* @param table the table to export to. Cannot be null
.
* @param rows the list of rows to export to. Cannot be null
.
*/
public void writeTable(Table table, List rows) {
Util.checkForNotNull(rows, "rows");
Util.checkForNotNull(table, "table");
beforeTable(table);
writeHeader(table);
writeRows(table, rows);
afterTable(table);
}
/**
* Writes the given set of rows to the output stream.
*
* @param table the table. Cannot be null
.
* @param rows list of rows to write to. Cannot be null
.
*/
public void writeRows(Table table, List rows) {
Util.checkForNotNull(rows, "rows");
Util.checkForNotNull(table, "table");
for (Row row : rows) {
RowDetails rowDetails = new RowDetails(table, rowIndex.getAndIncrement(), row);
writeRow(rowDetails);
}
}
/**
* Writes the header for given table.
*
* @param table the table whose header needs to be written. Cannot be null
.
*/
public void writeHeader(Table table) {
Util.checkForNotNull(table, "table");
if (options.isPrintHeaders() && table.getColumns() != null) {
beforeHeaderRow(table);
for (int i = 0; i < table.getColumns().size(); i++) {
HeaderCellDetails headerCellDetails = new HeaderCellDetails(table, i);
beforeHeaderCell(headerCellDetails);
writeHeaderCell(headerCellDetails);
afterHeaderCell(headerCellDetails);
}
afterHeaderRow(table);
}
}
/**
* Writes the row from given rowDetails
*
* @param rowDetails the row details of the row being exported. Cannot be null
.
*/
public void writeRow(RowDetails rowDetails) {
Util.checkForNotNull(rowDetails, "rowDetails");
//Generate the row data
generateRowData(rowDetails);
DataExporterCallback callback = rowDetails.getTable().getCallback();
if (callback != null) {
callback.beforeRow(rowDetails);
}
beforeRow(rowDetails);
for (int columnIndex = 0; columnIndex < rowDetails.getTable().getColumns().size(); columnIndex++) {
CellDetails cellDetails = new CellDetails(rowDetails, columnIndex);
cellDetails.setCellValue(rowDetails.getRow().getCellValue(cellDetails));
cellDetails.setCellValue(rowDetails.getRow().getCellValue(cellDetails));
if (callback != null) {
callback.beforeCell(cellDetails);
}
beforeRowCell(cellDetails);
writeRowCell(cellDetails);
afterRowCell(cellDetails);
if (callback != null) {
callback.afterCell(cellDetails);
}
}
afterRow(rowDetails);
if (callback != null) {
callback.afterRow(rowDetails);
}
}
protected void print(char value) {
print("" + value);
}
protected void print(String value) {
out.print(options.isEscapeHtml()?StringEscapeUtils.escapeHtml4(value):value);
if (autoFlush) {
out.flush();
}
}
protected void println(String value) {
out.print(options.isEscapeHtml()?StringEscapeUtils.escapeHtml4(value):value);
println();
if (autoFlush) {
out.flush();
}
}
protected void println() {
out.print(options.getLineSeparatorString());
if (autoFlush) {
out.flush();
}
}
public void flush() {
out.flush();
}
public void finishExporting() {
rowIndex = new AtomicInteger();
}
/**
* Method generates the cell values for columns which generates its own data
* and gives a chance for cell override if there is a call back.
*
* @param rowDetails
*/
protected void generateRowData(RowDetails rowDetails) {
List columns = rowDetails.getTable().getColumns();
List