XlsDataSet.java

  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. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.OutputStream;

  27. import org.apache.poi.EncryptedDocumentException;
  28. import org.apache.poi.ss.usermodel.Workbook;
  29. import org.apache.poi.ss.usermodel.WorkbookFactory;
  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.  * This dataset implementation can read and write MS Excel documents. Each
  41.  * sheet represents a table. The first row of a sheet defines the columns names
  42.  * and remaining rows contains the data.
  43.  *
  44.  * @author Manuel Laflamme
  45.  * @since Feb 21, 2003
  46.  * @version $Revision$
  47.  */
  48. public class XlsDataSet extends AbstractDataSet
  49. {
  50.    
  51.     /**
  52.      * Logger for this class
  53.      */
  54.     private static final Logger logger = LoggerFactory.getLogger(XlsDataSet.class);

  55.     private final OrderedTableNameMap _tables;


  56.     /**
  57.      * Creates a new XlsDataSet object that loads the specified Excel document.
  58.      */
  59.     public XlsDataSet(File file) throws IOException, DataSetException
  60.     {
  61.         this(new FileInputStream(file));
  62.     }

  63.     /**
  64.      * Creates a new XlsDataSet object that loads the specified Excel document.
  65.      */
  66.     public XlsDataSet(InputStream in) throws IOException, DataSetException
  67.     {
  68.         _tables = super.createTableNameMap();

  69.         Workbook workbook;
  70.         try {
  71.             workbook = WorkbookFactory.create(in);
  72.         } catch (EncryptedDocumentException e) {
  73.             throw new IOException(e);
  74.         }
  75.        
  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.      * Write the specified dataset to the specified Excel document.
  86.      */
  87.     public static void write(IDataSet dataSet, OutputStream out)
  88.             throws IOException, DataSetException
  89.     {
  90.         logger.debug("write(dataSet={}, out={}) - start", dataSet, out);

  91.         new XlsDataSetWriter().write(dataSet, out);
  92.     }


  93.     ////////////////////////////////////////////////////////////////////////////
  94.     // AbstractDataSet class

  95.     protected ITableIterator createIterator(boolean reversed)
  96.             throws DataSetException
  97.     {
  98.         if(logger.isDebugEnabled())
  99.             logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));

  100.         ITable[] tables = (ITable[]) _tables.orderedValues().toArray(new ITable[0]);
  101.         return new DefaultTableIterator(tables, reversed);
  102.     }
  103. }