io.github.relbraun.poi.ss.reader.AbstractSpreadsheetReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of poi-object-mapper Show documentation
Show all versions of poi-object-mapper Show documentation
Objects mapper for Office formats - Excel files, Spreadsheets, etc.
The newest version!
package io.github.relbraun.poi.ss.reader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.github.relbraun.poi.SpreadsheetReadException;
import io.github.relbraun.poi.ss.handler.RowListener;
/**
* A abstract implementation of {@link SpreadsheetReader}.
*/
abstract class AbstractSpreadsheetReader implements SpreadsheetReader {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractSpreadsheetReader.class);
// Abstract Methods
// ------------------------------------------------------------------------
// Methods
// ------------------------------------------------------------------------
@Override
public void read(Class beanClz, File file, RowListener callback) throws SpreadsheetReadException {
// Closeble
try (InputStream fis = new FileInputStream(file)) {
// chain
this.read(beanClz, fis, callback);
} catch (IOException ex) {
String errMsg = String.format("Failed to read file as Stream : %s", ex.getMessage());
throw new SpreadsheetReadException(errMsg, ex);
}
}
@Override
public void read(Class beanClz, File file, int sheetNo, RowListener callback)
throws SpreadsheetReadException {
// Sanity checks
try {
InputStream fis = new FileInputStream(file);
// chain
this.read(beanClz, fis, sheetNo, callback);
} catch (IOException ex) {
String errMsg = String.format("Failed to read file as Stream : %s", ex.getMessage());
throw new SpreadsheetReadException(errMsg, ex);
}
}
@Override
public List read(Class beanClz, File file) throws SpreadsheetReadException {
// Closeble
try (InputStream fis = new FileInputStream(file)) {
return this.read(beanClz, fis);
} catch (IOException ex) {
String errMsg = String.format("Failed to read file as Stream : %s", ex.getMessage());
throw new SpreadsheetReadException(errMsg, ex);
}
}
@Override
public List read(Class beanClz, InputStream is) throws SpreadsheetReadException {
// Result
final List sheetBeans = new ArrayList();
// Read with callback to fill list
this.read(beanClz, is, new RowListener() {
@Override
public void row(int rowNum, T rowObj) {
if (rowObj == null) {
LOGGER.error("Null object returned for row : {}", rowNum);
return;
}
sheetBeans.add(rowObj);
}
});
return sheetBeans;
}
@Override
public List read(Class beanClz, File file, int sheetNo) throws SpreadsheetReadException {
// Closeble
try (InputStream fis = new FileInputStream(file)) {
return this.read(beanClz, fis, sheetNo);
} catch (IOException ex) {
String errMsg = String.format("Failed to read file as Stream : %s", ex.getMessage());
throw new SpreadsheetReadException(errMsg, ex);
}
}
@Override
public List read(Class beanClz, InputStream is, int sheetNo) throws SpreadsheetReadException {
// Result
final List sheetBeans = new ArrayList();
// Read with callback to fill list
this.read(beanClz, is, sheetNo, new RowListener() {
@Override
public void row(int rowNum, T rowObj) {
if (rowObj == null) {
LOGGER.error("Null object returned for row : {}", rowNum);
return;
}
sheetBeans.add(rowObj);
}
});
return sheetBeans;
}
}