01: /*
02: *
03: * The DbUnit Database Testing Framework
04: * Copyright (C)2002-2004, DbUnit.org
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * 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:
23: import org.slf4j.Logger;
24: import org.slf4j.LoggerFactory;
25:
26: import org.dbunit.dataset.*;
27:
28: /**
29: * @author Manuel Laflamme
30: * @since Apr 17, 2003
31: * @version $Revision: 558 $
32: */
33: public class DataSetProducerAdapter implements IDataSetProducer {
34:
35: /**
36: * Logger for this class
37: */
38: private static final Logger logger = LoggerFactory
39: .getLogger(DataSetProducerAdapter.class);
40:
41: private static final IDataSetConsumer EMPTY_CONSUMER = new DefaultConsumer();
42:
43: private final ITableIterator _iterator;
44: private IDataSetConsumer _consumer = EMPTY_CONSUMER;
45:
46: public DataSetProducerAdapter(ITableIterator iterator) {
47: _iterator = iterator;
48: }
49:
50: public DataSetProducerAdapter(IDataSet dataSet)
51: throws DataSetException {
52: _iterator = dataSet.iterator();
53: }
54:
55: ////////////////////////////////////////////////////////////////////////////
56: // IDataSetProducer interface
57:
58: public void setConsumer(IDataSetConsumer consumer)
59: throws DataSetException {
60: logger.debug("setConsumer(consumer) - start");
61:
62: _consumer = consumer;
63: }
64:
65: public void produce() throws DataSetException {
66: logger.debug("produce() - start");
67:
68: _consumer.startDataSet();
69: while (_iterator.next()) {
70: ITable table = _iterator.getTable();
71: ITableMetaData metaData = table.getTableMetaData();
72:
73: _consumer.startTable(metaData);
74: try {
75: Column[] columns = metaData.getColumns();
76: if (columns.length == 0) {
77: _consumer.endTable();
78: continue;
79: }
80:
81: for (int i = 0;; i++) {
82: Object[] values = new Object[columns.length];
83: for (int j = 0; j < columns.length; j++) {
84: Column column = columns[j];
85: values[j] = table.getValue(i, column
86: .getColumnName());
87: }
88: _consumer.row(values);
89: }
90: } catch (RowOutOfBoundsException e) {
91: logger.error("produce()", e);
92:
93: // end of table
94: _consumer.endTable();
95: }
96: }
97: _consumer.endDataSet();
98: }
99: }
|