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     	if(logger.isDebugEnabled())
117     		logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));
118 
119         // Verify producer notifications completed
120         if (!_ready)
121         {
122             throw new IllegalStateException("Not ready!");
123         }
124 
125         String[] names = _orderedTableNameMap.getTableNames();
126         ITable[] tables = new ITable[names.length];
127         for (int i = 0; i < names.length; i++)
128         {
129             String tableName = names[i];
130             ITable table = (ITable)_orderedTableNameMap.get(tableName);
131             if (table == null)
132             {
133                 throw new NoSuchTableException(tableName);
134             }
135 
136             tables[i] = table;
137         }
138 
139         return new DefaultTableIterator(tables, reversed);
140     }
141 
142     ////////////////////////////////////////////////////////////////////////////
143     // IDataSet interface
144 
145     public String[] getTableNames() throws DataSetException
146     {
147         logger.debug("getTableNames() - start");
148 
149         // Verify producer notifications completed
150         if (!_ready)
151         {
152             throw new IllegalStateException("Not ready!");
153         }
154 
155         return _orderedTableNameMap.getTableNames();
156     }
157 
158     public ITableMetaData getTableMetaData(String tableName) throws DataSetException
159     {
160         logger.debug("getTableMetaData(tableName={}) - start", tableName);
161 
162         // Verify producer notifications completed
163         if (!_ready)
164         {
165             throw new IllegalStateException("Not ready!");
166         }
167 
168         return super.getTableMetaData(tableName);
169     }
170 
171     public ITable getTable(String tableName) throws DataSetException
172     {
173         logger.debug("getTable(tableName={}) - start", tableName);
174 
175         // Verify producer notifications completed
176         if (!_ready)
177         {
178             throw new IllegalStateException("Not ready!");
179         }
180 
181         return super.getTable(tableName);
182     }
183 
184     ////////////////////////////////////////////////////////////////////////
185     // IDataSetConsumer interface
186 
187     public void startDataSet() throws DataSetException
188     {
189         logger.debug("startDataSet() - start");
190 
191         _ready = false;
192     }
193 
194     public void endDataSet() throws DataSetException
195     {
196         logger.debug("endDataSet() - start");
197 
198         _ready = true;
199     }
200 
201     public void startTable(ITableMetaData metaData) throws DataSetException
202     {
203         logger.debug("startTable(metaData={}) - start", metaData);
204 
205         String tableName = metaData.getTableName();
206         _orderedTableNameMap.add(tableName, new DefaultTable(metaData));
207     }
208 
209     public void endTable() throws DataSetException
210     {
211         // no op
212     }
213 
214     public void row(Object[] values) throws DataSetException
215     {
216         // no op
217     }
218 
219     public String toString()
220     {
221     	StringBuffer sb = new StringBuffer();
222     	sb.append(getClass().getName()).append("[");
223     	sb.append("_ready=").append(this._ready);
224     	sb.append(", _orderedTableNameMap=").append(this._orderedTableNameMap);
225     	sb.append("]");
226     	return sb.toString();
227     }
228 }