001: package com.canoo.ant.table;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.FileNotFoundException;
006: import java.io.IOException;
007: import java.util.ArrayList;
008: import java.util.LinkedList;
009: import java.util.List;
010: import java.util.Properties;
011:
012: import org.apache.log4j.Logger;
013: import org.apache.poi.hssf.usermodel.HSSFCell;
014: import org.apache.poi.hssf.usermodel.HSSFRow;
015: import org.apache.poi.hssf.usermodel.HSSFSheet;
016: import org.apache.poi.hssf.usermodel.HSSFWorkbook;
017: import org.apache.poi.poifs.filesystem.POIFSFileSystem;
018:
019: public class ExcelPropertyTable extends APropertyTable {
020:
021: private static final Logger LOG = Logger
022: .getLogger(ExcelPropertyTable.class);
023:
024: public ExcelPropertyTable() {
025: }
026:
027: protected boolean hasJoinTable() {
028: final Object sheet;
029: try {
030: sheet = getWorkbook().getSheet(KEY_JOIN);
031: } catch (final IOException e) {
032: throw new RuntimeException("Failed to read container: >"
033: + getContainer() + "<", e);
034: }
035: return sheet != null;
036: }
037:
038: private HSSFWorkbook getWorkbook() throws IOException {
039: final File file = getContainer();
040: if (!file.exists()) {
041: throw new FileNotFoundException("File not found >"
042: + file.getAbsolutePath() + "< " + getContainer());
043: } else if (!file.isFile() || !file.canRead()) {
044: throw new IllegalArgumentException(
045: "No a regular readable file: >"
046: + file.getAbsolutePath() + "<");
047: }
048: final POIFSFileSystem excelFile = new POIFSFileSystem(
049: new FileInputStream(file));
050: return new HSSFWorkbook(excelFile);
051: }
052:
053: protected List read(final String sheetName) throws IOException {
054: final HSSFWorkbook workbook = getWorkbook();
055: final HSSFSheet sheet;
056: if (sheetName == null) {
057: sheet = workbook.getSheetAt(0); // no name specified, take the first sheet
058: } else {
059: sheet = workbook.getSheet(sheetName);
060: }
061: if (null == sheet) {
062: String msg = "No sheet \"" + sheetName
063: + "\" found in file " + getContainer()
064: + ". Available sheets: ";
065: for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
066: if (i != 0)
067: msg += ", ";
068: msg += workbook.getSheetName(i);
069: }
070: throw new IllegalArgumentException(msg);
071: }
072:
073: final int lastRowNum = sheet.getLastRowNum();
074: final List header = new ArrayList();
075: final HSSFRow headerRow = sheet.getRow(0);
076: for (short i = 0; i < headerRow.getLastCellNum(); i++) {
077: final HSSFCell cell = headerRow.getCell(i);
078: if (cell != null)
079: header.add(stringValueOf(cell));
080: else
081: header.add(null);
082: }
083: final List result = new LinkedList();
084: for (int rowNo = 1; rowNo <= lastRowNum; rowNo++) { // last Row is included
085: final HSSFRow row = sheet.getRow(rowNo);
086: if (row != null) // surprising, but row can be null
087: {
088: final Properties props = new Properties();
089: for (short i = 0; i < header.size(); i++) {
090: final String headerName = (String) header.get(i);
091: if (headerName != null) // handle empty cols
092: {
093: final HSSFCell cell = row.getCell(i);
094: final String value = stringValueOf(cell);
095: putValue(value, headerName, props);
096: }
097: }
098: result.add(props);
099: }
100: }
101:
102: return result;
103: }
104:
105: protected void putValue(String value, Object key, Properties props) {
106: if (!EMPTY.equals(value)) { // do not add empty values to allow proper default handling
107: props.put(key, value);
108: }
109: }
110:
111: private String stringValueOf(final HSSFCell cell) {
112: if (null == cell) {
113: return EMPTY;
114: }
115: switch (cell.getCellType()) {
116: case (HSSFCell.CELL_TYPE_STRING):
117: return cell.getRichStringCellValue().getString();
118: case (HSSFCell.CELL_TYPE_NUMERIC):
119: return "" + cell.getNumericCellValue();
120: case (HSSFCell.CELL_TYPE_BLANK):
121: return "";
122: case (HSSFCell.CELL_TYPE_BOOLEAN):
123: return "" + cell.getBooleanCellValue();
124: default:
125: LOG.warn("Cell Type not supported: " + cell.getCellType());
126: return EMPTY;
127: }
128: }
129: }
|