KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > xml > FlatDtdDataSet


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 org.dbunit.dataset.*;
25 import org.dbunit.dataset.stream.IDataSetConsumer;
26 import org.dbunit.dataset.stream.IDataSetProducer;
27 import org.xml.sax.InputSource JavaDoc;
28
29 import java.io.*;
30 import java.util.*;
31
32 /**
33  * @author Manuel Laflamme
34  * @version $Revision: 1.13 $
35  * @since Apr 4, 2002
36  */

37 public class FlatDtdDataSet extends AbstractDataSet implements IDataSetConsumer
38 {
39     private final List _tableNames = new ArrayList();
40     private final Map _tableMap = new HashMap();
41     private boolean _ready = false;
42
43     public FlatDtdDataSet()
44     {
45     }
46
47     public FlatDtdDataSet(InputStream in) throws DataSetException, IOException
48     {
49         this(new FlatDtdProducer(new InputSource JavaDoc(in)));
50     }
51
52     public FlatDtdDataSet(Reader reader) throws DataSetException, IOException
53     {
54         this(new FlatDtdProducer(new InputSource JavaDoc(reader)));
55     }
56
57     public FlatDtdDataSet(IDataSetProducer producer) throws DataSetException
58     {
59         producer.setConsumer(this);
60         producer.produce();
61     }
62
63     /**
64      * Write the specified dataset to the specified output stream as DTD.
65      */

66     public static void write(IDataSet dataSet, OutputStream out)
67             throws IOException, DataSetException
68     {
69         write(dataSet, new OutputStreamWriter(out));
70     }
71
72     /**
73      * Write the specified dataset to the specified writer as DTD.
74      */

75     public static void write(IDataSet dataSet, Writer out)
76             throws IOException, DataSetException
77     {
78         FlatDtdWriter datasetWriter = new FlatDtdWriter(out);
79         datasetWriter.write(dataSet);
80     }
81
82     ////////////////////////////////////////////////////////////////////////////
83
// AbstractDataSet class
84

85     protected ITableIterator createIterator(boolean reversed)
86             throws DataSetException
87     {
88         // Verify producer notifications completed
89
if (!_ready)
90         {
91             throw new IllegalStateException JavaDoc("Not ready!");
92         }
93
94         String JavaDoc[] names = (String JavaDoc[])_tableNames.toArray(new String JavaDoc[0]);
95         ITable[] tables = new ITable[names.length];
96         for (int i = 0; i < names.length; i++)
97         {
98             String JavaDoc tableName = names[i];
99             ITable table = (ITable)_tableMap.get(tableName);
100             if (table == null)
101             {
102                 throw new NoSuchTableException(tableName);
103             }
104
105             tables[i] = table;
106         }
107
108         return new DefaultTableIterator(tables, reversed);
109     }
110
111     ////////////////////////////////////////////////////////////////////////////
112
// IDataSet interface
113

114     public String JavaDoc[] getTableNames() throws DataSetException
115     {
116         // Verify producer notifications completed
117
if (!_ready)
118         {
119             throw new IllegalStateException JavaDoc("Not ready!");
120         }
121
122         return (String JavaDoc[])_tableNames.toArray(new String JavaDoc[0]);
123     }
124
125     public ITableMetaData getTableMetaData(String JavaDoc tableName) throws DataSetException
126     {
127         // Verify producer notifications completed
128
if (!_ready)
129         {
130             throw new IllegalStateException JavaDoc("Not ready!");
131         }
132
133         String JavaDoc upperTableName = tableName.toUpperCase();
134         if (_tableMap.containsKey(upperTableName))
135         {
136             ITable table = (ITable)_tableMap.get(upperTableName);
137             return table.getTableMetaData();
138         }
139
140         throw new NoSuchTableException(tableName);
141     }
142
143     public ITable getTable(String JavaDoc tableName) throws DataSetException
144     {
145         // Verify producer notifications completed
146
if (!_ready)
147         {
148             throw new IllegalStateException JavaDoc("Not ready!");
149         }
150
151         String JavaDoc upperTableName = tableName.toUpperCase();
152         if (_tableMap.containsKey(upperTableName))
153         {
154             return (ITable)_tableMap.get(upperTableName);
155         }
156
157         throw new NoSuchTableException(tableName);
158     }
159
160     ////////////////////////////////////////////////////////////////////////
161
// IDataSetConsumer interface
162

163     public void startDataSet() throws DataSetException
164     {
165         _ready = false;
166     }
167
168     public void endDataSet() throws DataSetException
169     {
170         _ready = true;
171     }
172
173     public void startTable(ITableMetaData metaData) throws DataSetException
174     {
175         String JavaDoc tableName = metaData.getTableName();
176         _tableNames.add(tableName);
177         _tableMap.put(tableName.toUpperCase(), new DefaultTable(metaData));
178     }
179
180     public void endTable() throws DataSetException
181     {
182         // no op
183
}
184
185     public void row(Object JavaDoc[] values) throws DataSetException
186     {
187         // no op
188
}
189 }
Popular Tags