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  
22  package org.dbunit.dataset.xml;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.OutputStream;
27  import java.io.OutputStreamWriter;
28  import java.io.Reader;
29  import java.io.Writer;
30  
31  import org.dbunit.dataset.AbstractDataSet;
32  import org.dbunit.dataset.DataSetException;
33  import org.dbunit.dataset.DefaultTable;
34  import org.dbunit.dataset.DefaultTableIterator;
35  import org.dbunit.dataset.IDataSet;
36  import org.dbunit.dataset.ITable;
37  import org.dbunit.dataset.ITableIterator;
38  import org.dbunit.dataset.ITableMetaData;
39  import org.dbunit.dataset.NoSuchTableException;
40  import org.dbunit.dataset.OrderedTableNameMap;
41  import org.dbunit.dataset.stream.IDataSetConsumer;
42  import org.dbunit.dataset.stream.IDataSetProducer;
43  import org.slf4j.Logger;
44  import org.slf4j.LoggerFactory;
45  import org.xml.sax.InputSource;
46  
47  /**
48   * @author Manuel Laflamme
49   * @author Last changed by: $Author$
50   * @version $Revision$ $Date$
51   * @since 1.0 (Apr 4, 2002)
52   */
53  public class FlatDtdDataSet extends AbstractDataSet implements IDataSetConsumer
54  {
55  
56      /**
57       * Logger for this class
58       */
59      private static final Logger logger = LoggerFactory.getLogger(FlatDtdDataSet.class);
60  
61      private OrderedTableNameMap _tableMap;
62      private boolean _ready = false;
63  
64      public FlatDtdDataSet()
65      {
66          initialize();
67      }
68  
69      public FlatDtdDataSet(InputStream in) throws DataSetException, IOException
70      {
71          this(new FlatDtdProducer(new InputSource(in)));
72      }
73  
74      public FlatDtdDataSet(Reader reader) throws DataSetException, IOException
75      {
76          this(new FlatDtdProducer(new InputSource(reader)));
77      }
78  
79      public FlatDtdDataSet(IDataSetProducer producer) throws DataSetException
80      {
81          initialize();
82          producer.setConsumer(this);
83          producer.produce();
84      }
85  
86      private void initialize()
87      {
88          _tableMap = super.createTableNameMap();
89      }
90  
91      /**
92       * Write the specified dataset to the specified output stream as DTD.
93       * @see FlatDtdWriter
94       */
95      public static void write(IDataSet dataSet, OutputStream out)
96              throws IOException, DataSetException
97      {
98          logger.debug("write(dataSet={}, out={}) - start", dataSet, out);
99          write(dataSet, new OutputStreamWriter(out));
100     }
101 
102     /**
103      * Write the specified dataset to the specified writer as DTD.
104      * @see FlatDtdWriter
105      */
106     public static void write(IDataSet dataSet, Writer out)
107             throws IOException, DataSetException
108     {
109         logger.debug("write(dataSet={}, out={}) - start", dataSet, out);
110         FlatDtdWriter datasetWriter = new FlatDtdWriter(out);
111         datasetWriter.write(dataSet);
112     }
113 
114     ////////////////////////////////////////////////////////////////////////////
115     // AbstractDataSet class
116 
117     protected ITableIterator createIterator(boolean reversed)
118             throws DataSetException
119     {
120     	if(logger.isDebugEnabled())
121     		logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));
122 
123         // Verify producer notifications completed
124         if (!_ready)
125         {
126             throw new IllegalStateException("Not ready!");
127         }
128 
129         String[] names = _tableMap.getTableNames();
130         ITable[] tables = new ITable[names.length];
131         for (int i = 0; i < names.length; i++)
132         {
133             String tableName = names[i];
134             ITable table = (ITable)_tableMap.get(tableName);
135             if (table == null)
136             {
137                 throw new NoSuchTableException(tableName);
138             }
139 
140             tables[i] = table;
141         }
142 
143         return new DefaultTableIterator(tables, reversed);
144     }
145 
146     ////////////////////////////////////////////////////////////////////////////
147     // IDataSet interface
148 
149     public String[] getTableNames() throws DataSetException
150     {
151         logger.debug("getTableNames() - start");
152 
153         // Verify producer notifications completed
154         if (!_ready)
155         {
156             throw new IllegalStateException("Not ready!");
157         }
158 
159         return _tableMap.getTableNames();
160     }
161 
162     public ITableMetaData getTableMetaData(String tableName) throws DataSetException
163     {
164         logger.debug("getTableMetaData(tableName={}) - start", tableName);
165 
166         // Verify producer notifications completed
167         if (!_ready)
168         {
169             throw new IllegalStateException("Not ready!");
170         }
171 
172         if (_tableMap.containsTable(tableName))
173         {
174             ITable table = (ITable)_tableMap.get(tableName);
175             return table.getTableMetaData();
176         }
177 
178         throw new NoSuchTableException(tableName);
179     }
180 
181     public ITable getTable(String tableName) throws DataSetException
182     {
183         logger.debug("getTable(tableName={}) - start", tableName);
184 
185         // Verify producer notifications completed
186         if (!_ready)
187         {
188             throw new IllegalStateException("Not ready!");
189         }
190 
191         if (_tableMap.containsTable(tableName))
192         {
193             return (ITable)_tableMap.get(tableName);
194         }
195 
196         throw new NoSuchTableException(tableName);
197     }
198 
199     ////////////////////////////////////////////////////////////////////////
200     // IDataSetConsumer interface
201 
202     public void startDataSet() throws DataSetException
203     {
204         logger.debug("startDataSet() - start");
205 
206         _ready = false;
207     }
208 
209     public void endDataSet() throws DataSetException
210     {
211         logger.debug("endDataSet() - start");
212 
213         _ready = true;
214     }
215 
216     public void startTable(ITableMetaData metaData) throws DataSetException
217     {
218         logger.debug("startTable(metaData={}) - start", metaData);
219 
220         String tableName = metaData.getTableName();
221         _tableMap.add(tableName, new DefaultTable(metaData));
222     }
223 
224     public void endTable() throws DataSetException
225     {
226         // no op
227     }
228 
229     public void row(Object[] values) throws DataSetException
230     {
231         // no op
232     }
233     
234     
235     public String toString()
236     {
237     	StringBuffer sb = new StringBuffer();
238     	sb.append(getClass().getName()).append("[");
239     	sb.append("_ready=").append(this._ready);
240     	sb.append(", _tableMap=").append(this._tableMap);
241     	sb.append("]");
242     	return sb.toString();
243     }
244 
245 }