sirius.web.templates.LineBasedProcessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sirius-web Show documentation
Show all versions of sirius-web Show documentation
Provides a modern and scalable web server as SIRIUS module
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.web.templates;
import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import sirius.kernel.async.TaskContext;
import sirius.kernel.commons.BOMReader;
import sirius.kernel.commons.CSVReader;
import sirius.kernel.commons.Doubles;
import sirius.kernel.commons.Strings;
import sirius.kernel.commons.Values;
import sirius.kernel.commons.Watch;
import sirius.kernel.health.Exceptions;
import sirius.kernel.nls.NLS;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Processes line based input files like MS Excel or CSV.
*/
public abstract class LineBasedProcessor {
/**
* Generates an appropriate LineBasedProcessor based on the file extension of the given file.
*
* @param name the name of the file to process
* @param input an input stream containing the data to import
* @return an appropriate processor for the given file
* @throws sirius.kernel.health.HandledException if no processor can handle the given file
*/
public static LineBasedProcessor create(String name, InputStream input) {
if (name.toLowerCase().endsWith("xls")) {
return new XLSProcessor(input, false);
}
if (name.toLowerCase().endsWith("xlsx")) {
return new XLSProcessor(input, true);
}
if (name.toLowerCase().endsWith("csv")) {
return new CSVProcessor(input);
}
throw Exceptions.createHandled().withSystemErrorMessage("Cannot process files of type: %s", name).handle();
}
/**
* Starts processing and sends each line to the given rowProcessor.
*
* @param rowProcessor the processor which handles each row of the file
* @throws Exception in case an error occurred while processing.
*/
public abstract void run(RowProcessor rowProcessor) throws Exception;
/**
* In charge of processing XLS (MS Excel) files.
*/
protected static class XLSProcessor extends LineBasedProcessor {
private InputStream input;
private boolean xslx;
public XLSProcessor(InputStream input, boolean xslx) {
super();
this.input = input;
this.xslx = xslx;
}
@Override
public void run(RowProcessor rowProcessor) throws Exception {
Workbook wb = xslx ? new XSSFWorkbook(input) : new HSSFWorkbook(input);
Sheet sheet = wb.getSheetAt(0);
Iterator iter = sheet.rowIterator();
int current = 0;
TaskContext tc = TaskContext.get();
while (iter.hasNext() && tc.isActive()) {
Watch w = Watch.start();
current++;
Row row = iter.next();
short first = 0;
short last = row.getLastCellNum();
List