001: /* ExcelDataSetAdapter.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.Sheet;
024: import jxl.Workbook;
025:
026: import org.apache.commons.lang.ArrayUtils;
027: import org.apache.commons.lang.Validate;
028: import org.ddsteps.dataset.DataSet;
029: import org.ddsteps.dataset.DataTable;
030:
031: /**
032: * @author Adam
033: * @version $Id: ExcelDataSetAdapter.java,v 1.1 2005/08/25 08:32:37 adamskogman
034: * Exp $
035: */
036: class ExcelDataSetAdapter implements DataSet {
037:
038: /**
039: * Serial UID
040: */
041: private static final long serialVersionUID = 6610325365902886269L;
042:
043: final Workbook workbook;
044:
045: /**
046: * Setup an adapter with a workbook
047: *
048: * @param workbook
049: * The workbook
050: */
051: public ExcelDataSetAdapter(Workbook workbook) {
052: super ();
053: Validate
054: .notNull(workbook, "Argument workbook must not be null");
055: this .workbook = workbook;
056: }
057:
058: /**
059: * Lists names of the sheets.
060: *
061: * @see org.ddsteps.dataset.DataSet#getTableNames()
062: */
063: public String[] getTableNames() {
064: String[] sheetNames = workbook.getSheetNames();
065:
066: if (sheetNames == null) {
067: sheetNames = ArrayUtils.EMPTY_STRING_ARRAY;
068: }
069:
070: return sheetNames;
071: }
072:
073: /**
074: * Iterate over sheets.
075: *
076: * @see org.ddsteps.dataset.DataSet#tableIterator()
077: */
078: public Iterator tableIterator() {
079: return new Iterator() {
080:
081: /**
082: * -1 because we haven't even started.
083: */
084: private int current = -1;
085:
086: private final int last = workbook.getNumberOfSheets() - 1;
087:
088: public void remove() {
089: throw new UnsupportedOperationException(
090: "Cannot remove a sheet from an Excel file.");
091: }
092:
093: public boolean hasNext() {
094: return current < last;
095: }
096:
097: public Object next() {
098: if (hasNext()) {
099: current++;
100: Sheet sheet = workbook.getSheet(current);
101: return createDataTableAdapter(sheet);
102: } else {
103: return null;
104: }
105: }
106:
107: };
108: }
109:
110: /**
111: * Get sheet by name
112: *
113: * @see org.ddsteps.dataset.DataSet#getTable(java.lang.String)
114: */
115: public DataTable getTable(String tableName) {
116:
117: Sheet sheet = workbook.getSheet(tableName);
118:
119: if (sheet == null) {
120: return null;
121: }
122:
123: return createDataTableAdapter(sheet);
124: }
125:
126: /**
127: * Factory method.
128: *
129: * @param sheet
130: * @return An ExcelDataTableAdapter.
131: */
132: protected DataTable createDataTableAdapter(Sheet sheet) {
133: return new ExcelDataTableAdapter(sheet);
134: }
135:
136: }
|