001: /* ExcelDataValueAdapter.java
002: *
003: * DDSteps - Data Driven JUnit Test Steps
004: * Copyright (C) 2005 Jayway AB
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 version 2.1 as published by the Free Software Foundation.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, visit
017: * http://www.opensource.org/licenses/lgpl-license.php
018: */
019: package org.ddsteps.dataset.excel;
020:
021: import org.ddsteps.dataset.DataValue;
022:
023: import jxl.BooleanCell;
024: import jxl.Cell;
025: import jxl.CellReferenceHelper;
026: import jxl.CellType;
027: import jxl.DateCell;
028: import jxl.NumberCell;
029:
030: /**
031: * @author Adam
032: * @version $Id: ExcelDataValueAdapter.java,v 1.1 2005/12/03 12:51:40 adamskogman Exp $
033: */
034: class ExcelDataValueAdapter implements DataValue {
035:
036: final private Cell header;
037:
038: final private Cell cell;
039:
040: /**
041: * Construct an adapter from a cell and a header.
042: *
043: * @param header The header cell
044: * @param cell The data cell
045: */
046: public ExcelDataValueAdapter(final Cell header, final Cell cell) {
047: super ();
048: this .header = header;
049: this .cell = cell;
050: }
051:
052: /**
053: * Gets name from header.
054: *
055: * @see org.ddsteps.dataset.DataValue#getName()
056: */
057: public String getName() {
058: return header.getContents();
059: }
060:
061: /**
062: * Get data from the cell.
063: *
064: * @see org.ddsteps.dataset.DataValue#getValue()
065: */
066: public Object getValue() {
067:
068: // Shortcut
069: if (cell == null)
070: return null;
071:
072: CellType type = cell.getType();
073:
074: if (type == CellType.ERROR || type == CellType.FORMULA_ERROR) {
075: // ERROR
076: throw new ExcelDataException(
077: "Error in excel sheet in cell "
078: + CellReferenceHelper.getCellReference(cell
079: .getColumn(), cell.getRow()));
080: } else if (type == CellType.EMPTY) {
081: // EMPTY
082: return null;
083: } else if (type == CellType.LABEL
084: || type == CellType.STRING_FORMULA) {
085: // STRING
086: return cell.getContents();
087: } else if (type == CellType.BOOLEAN
088: || type == CellType.BOOLEAN_FORMULA) {
089: // BOOLEAN
090: BooleanCell boolCell = (BooleanCell) cell;
091: return Boolean.valueOf(boolCell.getValue());
092: } else if (type == CellType.NUMBER
093: || type == CellType.NUMBER_FORMULA) {
094: // NUMBER
095: NumberCell numberCell = (NumberCell) cell;
096: return new Double(numberCell.getValue());
097: } else if (type == CellType.DATE
098: || type == CellType.DATE_FORMULA) {
099: // DATE
100: DateCell dateCell = (DateCell) cell;
101: return dateCell.getDate();
102: } else {
103: // UNKNOWN
104: throw new ExcelDataException(
105: "Unknown data in excel sheet in cell "
106: + CellReferenceHelper.getCellReference(cell
107: .getColumn(), cell.getRow()));
108:
109: }
110: }
111: }
|