View Javadoc

1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  package org.dbunit.dataset.excel;
22  
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.OutputStream;
28  
29  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
30  import org.dbunit.dataset.AbstractDataSet;
31  import org.dbunit.dataset.DataSetException;
32  import org.dbunit.dataset.DefaultTableIterator;
33  import org.dbunit.dataset.IDataSet;
34  import org.dbunit.dataset.ITable;
35  import org.dbunit.dataset.ITableIterator;
36  import org.dbunit.dataset.OrderedTableNameMap;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  /**
41   * This dataset implementation can read and write MS Excel documents. Each
42   * sheet represents a table. The first row of a sheet defines the columns names
43   * and remaining rows contains the data.
44   *
45   * @author Manuel Laflamme
46   * @since Feb 21, 2003
47   * @version $Revision: 875 $
48   */
49  public class XlsDataSet extends AbstractDataSet
50  {
51      
52      /**
53       * Logger for this class
54       */
55      private static final Logger logger = LoggerFactory.getLogger(XlsDataSet.class);
56  
57      private final OrderedTableNameMap _tables;
58  
59  
60      /**
61       * Creates a new XlsDataSet object that loads the specified Excel document.
62       */
63      public XlsDataSet(File file) throws IOException, DataSetException
64      {
65          this(new FileInputStream(file));
66      }
67  
68      /**
69       * Creates a new XlsDataSet object that loads the specified Excel document.
70       */
71      public XlsDataSet(InputStream in) throws IOException, DataSetException
72      {
73          _tables = super.createTableNameMap();
74          
75          HSSFWorkbook workbook = new HSSFWorkbook(in);
76          int sheetCount = workbook.getNumberOfSheets();
77          for (int i = 0; i < sheetCount; i++)
78          {
79              ITable table = new XlsTable(workbook.getSheetName(i),
80                      workbook.getSheetAt(i));
81              _tables.add(table.getTableMetaData().getTableName(), table);            
82          }
83      }
84  
85      /**
86       * Write the specified dataset to the specified Excel document.
87       */
88      public static void write(IDataSet dataSet, OutputStream out)
89              throws IOException, DataSetException
90      {
91          logger.debug("write(dataSet={}, out={}) - start", dataSet, out);
92  
93          new XlsDataSetWriter().write(dataSet, out);
94      }
95  
96  
97      ////////////////////////////////////////////////////////////////////////////
98      // AbstractDataSet class
99  
100     protected ITableIterator createIterator(boolean reversed)
101             throws DataSetException
102     {
103     	if(logger.isDebugEnabled())
104     		logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));
105 
106     	ITable[] tables = (ITable[]) _tables.orderedValues().toArray(new ITable[0]);
107         return new DefaultTableIterator(tables, reversed);
108     }
109 }