001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021: package org.dbunit.dataset.excel;
022:
023: import org.slf4j.Logger;
024: import org.slf4j.LoggerFactory;
025:
026: import org.apache.poi.hssf.usermodel.HSSFCell;
027: import org.apache.poi.hssf.usermodel.HSSFDateUtil;
028: import org.apache.poi.hssf.usermodel.HSSFRow;
029: import org.apache.poi.hssf.usermodel.HSSFSheet;
030: import org.dbunit.dataset.*;
031: import org.dbunit.dataset.datatype.DataType;
032: import org.dbunit.dataset.datatype.DataTypeException;
033:
034: import java.math.BigDecimal;
035: import java.util.ArrayList;
036: import java.util.List;
037:
038: /**
039: * @author Manuel Laflamme
040: * @since Feb 21, 2003
041: * @version $Revision: 554 $
042: */
043: class XlsTable extends AbstractTable {
044:
045: /**
046: * Logger for this class
047: */
048: private static final Logger logger = LoggerFactory
049: .getLogger(XlsTable.class);
050:
051: private final ITableMetaData _metaData;
052: private final HSSFSheet _sheet;
053:
054: public XlsTable(String sheetName, HSSFSheet sheet)
055: throws DataSetException {
056: int rowCount = sheet.getLastRowNum();
057: if (rowCount > 0 && sheet.getRow(0) != null) {
058: _metaData = createMetaData(sheetName, sheet.getRow(0));
059: } else {
060: _metaData = new DefaultTableMetaData(sheetName,
061: new Column[0]);
062: }
063:
064: _sheet = sheet;
065: }
066:
067: static ITableMetaData createMetaData(String tableName,
068: HSSFRow sampleRow) {
069: logger.debug("createMetaData(tableName=" + tableName
070: + ", sampleRow=" + sampleRow + ") - start");
071:
072: List columnList = new ArrayList();
073: for (int i = 0;; i++) {
074: HSSFCell cell = sampleRow.getCell((short) i);
075: if (cell == null) {
076: break;
077: }
078:
079: Column column = new Column(cell.getStringCellValue(),
080: DataType.UNKNOWN);
081: columnList.add(column);
082: }
083: Column[] columns = (Column[]) columnList.toArray(new Column[0]);
084: return new DefaultTableMetaData(tableName, columns);
085: }
086:
087: ////////////////////////////////////////////////////////////////////////////
088: // ITable interface
089:
090: public int getRowCount() {
091: logger.debug("getRowCount() - start");
092:
093: return _sheet.getLastRowNum();
094: }
095:
096: public ITableMetaData getTableMetaData() {
097: logger.debug("getTableMetaData() - start");
098:
099: return _metaData;
100: }
101:
102: public Object getValue(int row, String column)
103: throws DataSetException {
104: logger.debug("getValue(row=" + row + ", column=" + column
105: + ") - start");
106:
107: assertValidRowIndex(row);
108:
109: int columnIndex = getColumnIndex(column);
110: HSSFCell cell = _sheet.getRow(row + 1).getCell(
111: (short) columnIndex);
112: if (cell == null) {
113: return null;
114: }
115:
116: int type = cell.getCellType();
117: switch (type) {
118: case HSSFCell.CELL_TYPE_NUMERIC:
119: if (HSSFDateUtil.isCellDateFormatted(cell)) {
120: return cell.getDateCellValue();
121: }
122: return new BigDecimal(cell.getNumericCellValue());
123:
124: case HSSFCell.CELL_TYPE_STRING:
125: return cell.getStringCellValue();
126:
127: case HSSFCell.CELL_TYPE_FORMULA:
128: throw new DataTypeException("Formula not supported at row="
129: + row + ", column=" + column);
130:
131: case HSSFCell.CELL_TYPE_BLANK:
132: return null;
133:
134: case HSSFCell.CELL_TYPE_BOOLEAN:
135: return cell.getBooleanCellValue() ? Boolean.TRUE
136: : Boolean.FALSE;
137:
138: case HSSFCell.CELL_TYPE_ERROR:
139: throw new DataTypeException("Error at row=" + row
140: + ", column=" + column);
141:
142: default:
143: throw new DataTypeException("Unsupported type at row="
144: + row + ", column=" + column);
145: }
146: }
147: }
|