DataSetProducerAdapter.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.stream;

  22. import org.dbunit.dataset.Column;
  23. import org.dbunit.dataset.DataSetException;
  24. import org.dbunit.dataset.IDataSet;
  25. import org.dbunit.dataset.ITable;
  26. import org.dbunit.dataset.ITableIterator;
  27. import org.dbunit.dataset.ITableMetaData;
  28. import org.dbunit.dataset.RowOutOfBoundsException;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;


  31. /**
  32.  * Implementation of {@link IDataSetProducer} based on a given {@link IDataSet} or a
  33.  * {@link ITableIterator}.
  34.  *
  35.  * @author Manuel Laflamme
  36.  * @author Last changed by: $Author$
  37.  * @version $Revision$ $Date$
  38.  * @since Apr 17, 2003
  39.  */
  40. public class DataSetProducerAdapter implements IDataSetProducer
  41. {

  42.     /**
  43.      * Logger for this class
  44.      */
  45.     private static final Logger logger = LoggerFactory.getLogger(DataSetProducerAdapter.class);

  46.     private static final IDataSetConsumer EMPTY_CONSUMER = new DefaultConsumer();

  47.     private final ITableIterator _iterator;
  48.     private IDataSetConsumer _consumer = EMPTY_CONSUMER;

  49.     public DataSetProducerAdapter(ITableIterator iterator)
  50.     {
  51.         _iterator = iterator;
  52.     }

  53.     public DataSetProducerAdapter(IDataSet dataSet) throws DataSetException
  54.     {
  55.         _iterator = dataSet.iterator();
  56.     }

  57.     ////////////////////////////////////////////////////////////////////////////
  58.     // IDataSetProducer interface

  59.     public void setConsumer(IDataSetConsumer consumer) throws DataSetException
  60.     {
  61.         logger.debug("setConsumer(consumer) - start");

  62.         _consumer = consumer;
  63.     }

  64.     public void produce() throws DataSetException
  65.     {
  66.         logger.debug("produce() - start");

  67.         _consumer.startDataSet();
  68.         while(_iterator.next())
  69.         {
  70.             ITable table = _iterator.getTable();
  71.             ITableMetaData metaData = table.getTableMetaData();

  72.             _consumer.startTable(metaData);
  73.             try
  74.             {
  75.                 Column[] columns = metaData.getColumns();
  76.                 if (columns.length == 0)
  77.                 {
  78.                     _consumer.endTable();
  79.                     continue;
  80.                 }

  81.                 for (int i = 0; ; i++)
  82.                 {
  83.                     Object[] values = new Object[columns.length];
  84.                     for (int j = 0; j < columns.length; j++)
  85.                     {
  86.                         Column column = columns[j];
  87.                         values[j] = table.getValue(i, column.getColumnName());
  88.                     }
  89.                     _consumer.row(values);
  90.                 }
  91.             }
  92.             catch (RowOutOfBoundsException e)
  93.             {
  94.                 // This exception occurs when records are exhausted
  95.                 // and we reach the end of the table.  Ignore this error
  96.                 // and close table.

  97.                 // end of table
  98.                 _consumer.endTable();
  99.             }
  100.         }
  101.         _consumer.endDataSet();
  102.     }
  103. }