001: /* ExcelDataRowAdapter.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 java.util.Iterator;
022:
023: import jxl.Cell;
024: import jxl.CellType;
025:
026: import org.apache.commons.collections.Predicate;
027: import org.apache.commons.collections.Transformer;
028: import org.apache.commons.collections.iterators.FilterIterator;
029: import org.apache.commons.collections.iterators.TransformIterator;
030: import org.ddsteps.dataset.DataRow;
031: import org.ddsteps.dataset.DataValue;
032:
033: /**
034: * @author Adam
035: * @version $Id: ExcelDataRowAdapter.java,v 1.1 2005/12/03 12:51:40 adamskogman Exp $
036: */
037: class ExcelDataRowAdapter implements DataRow {
038:
039: /**
040: * Predicate for filtering out headers whose cell is empty.
041: *
042: * @author Adam
043: * @version $Id: ExcelDataRowAdapter.java,v 1.1 2005/12/03 12:51:40 adamskogman Exp $
044: */
045: class CellNotEmptyPredicate implements Predicate {
046:
047: /**
048: * True if the DATA cell is not empty.
049: *
050: * @param object The HEADER cell.
051: * @see org.apache.commons.collections.Predicate#evaluate(java.lang.Object)
052: */
053: public boolean evaluate(Object object) {
054:
055: Cell header = (Cell) object;
056:
057: // Get data cell
058: Cell cell = table.getSheet().getCell(header.getColumn(),
059: row);
060:
061: if (cell == null || cell.getType() == CellType.EMPTY) {
062: return false;
063: }
064:
065: return true;
066: }
067:
068: }
069:
070: class HeaderToCellTransformer implements Transformer {
071:
072: /**
073: * Transforms a header to a cell on the current row
074: *
075: * @see org.apache.commons.collections.Transformer#transform(java.lang.Object)
076: */
077: public Object transform(Object object) {
078: Cell header = (Cell) object;
079:
080: // Get data cell
081: Cell cell = table.getSheet().getCell(header.getColumn(),
082: row);
083:
084: return createDataValueAdapter(header, cell);
085: }
086:
087: }
088:
089: final ExcelDataTableAdapter table;
090:
091: /**
092: * ZERO-based row index.
093: */
094: final int row;
095:
096: /**
097: * @param table
098: * @param row
099: */
100: public ExcelDataRowAdapter(ExcelDataTableAdapter table,
101: final int row) {
102: super ();
103: this .row = row;
104: this .table = table;
105: }
106:
107: /**
108: * Excel rows are numbered starting from 1.
109: *
110: * @see org.ddsteps.dataset.DataRow#getId()
111: */
112: public String getId() {
113: return "Excel row " + (row + 1);
114: }
115:
116: /**
117: * Iterate over values. Really iterates over headers, filtering out any cell which are empty.
118: *
119: * @see org.ddsteps.dataset.DataRow#iterator()
120: */
121: public Iterator iterator() {
122:
123: // First wrap the headers to filter out empty cell in this row
124: FilterIterator nonEmptyHeadersIter = new FilterIterator(table
125: .headerCellsIterator(), new CellNotEmptyPredicate());
126:
127: // Then transform to convert to datavalues
128: TransformIterator dataValueIter = new TransformIterator(
129: nonEmptyHeadersIter, new HeaderToCellTransformer());
130:
131: return dataValueIter;
132:
133: }
134:
135: /**
136: * Factory method for creating value adapters
137: *
138: * @param header
139: * @param cell
140: * @return Adapter for datavalue
141: */
142: protected DataValue createDataValueAdapter(Cell header, Cell cell) {
143: return new ExcelDataValueAdapter(header, cell);
144: }
145:
146: }
|