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