KickJava   Java API By Example, From Geeks To Geeks.

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


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.CachedDataSet;
25 import org.dbunit.dataset.DataSetException;
26 import org.dbunit.dataset.IDataSet;
27
28 import org.xml.sax.InputSource JavaDoc;
29
30 import java.io.File JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.OutputStream JavaDoc;
34 import java.io.Reader JavaDoc;
35 import java.io.Writer JavaDoc;
36 import java.net.URL JavaDoc;
37
38 /**
39  * Reads and writes flat XML dataset document. Each XML element corresponds to a table row.
40  * Each XML element name corresponds to a table name. The XML attributes
41  * correspond to table columns.
42  * <p>
43  * Flat XML dataset document sample:
44  * <p>
45  * <pre>
46  * &lt;!DOCTYPE dataset SYSTEM "my-dataset.dtd"&gt;
47  * &lt;dataset&gt;
48  * &lt;TEST_TABLE COL0="row 0 col 0"
49  * COL1="row 0 col 1"
50  * COL2="row 0 col 2"/&gt;
51  * &lt;TEST_TABLE COL1="row 1 col 1"/&gt;
52  * &lt;SECOND_TABLE COL0="row 0 col 0"
53  * COL1="row 0 col 1" /&gt;
54  * &lt;EMPTY_TABLE/&gt;
55  * &lt;/dataset&gt;</pre>
56  * <p>
57  * To specify null values, omit corresponding attribute.
58  * In the above example, missing COL0 and COL2 attributes of TEST_TABLE second row represents null values.
59  * <p>
60  * Table metadata is deduced from the first row of each table. <b>Beware that DbUnit may think
61  * a table miss some columns if the first row of that table has one or more null values.</b>
62  * Because of that, this is highly recommended to use DTD. DbUnit will use the
63  * columns declared in the DTD as table metadata. DbUnit only support external system URI.
64  * The URI can be absolute or relative.
65  *
66  * @author Manuel Laflamme
67  * @version $Revision: 1.29 $
68  * @since Mar 12, 2002
69  */

70 public class FlatXmlDataSet extends CachedDataSet
71 {
72     /**
73      * Creates an FlatXmlDataSet object with the specifed InputSource.
74      */

75     public FlatXmlDataSet(InputSource JavaDoc source) throws IOException JavaDoc, DataSetException
76     {
77         super(new FlatXmlProducer(source));
78     }
79
80     /**
81      * Creates an FlatXmlDataSet object with the specifed xml file.
82      * Relative DOCTYPE uri are resolved from the xml file path.
83      *
84      * @param xmlFile the xml file
85      */

86     public FlatXmlDataSet(File JavaDoc xmlFile) throws IOException JavaDoc, DataSetException
87     {
88         this(xmlFile, true);
89     }
90
91     /**
92      * Creates an FlatXmlDataSet object with the specifed xml file.
93      * Relative DOCTYPE uri are resolved from the xml file path.
94      *
95      * @param xmlFile the xml file
96      * @param dtdMetadata if <code>false</code> do not use DTD as metadata
97      */

98     public FlatXmlDataSet(File JavaDoc xmlFile, boolean dtdMetadata)
99             throws IOException JavaDoc, DataSetException
100     {
101         this(xmlFile.toURL(), dtdMetadata);
102     }
103
104     /**
105      * Creates an FlatXmlDataSet object with the specifed xml URL.
106      * Relative DOCTYPE uri are resolved from the xml file path.
107      *
108      * @param xmlUrl the xml URL
109      */

110     public FlatXmlDataSet(URL JavaDoc xmlUrl) throws IOException JavaDoc, DataSetException
111     {
112         this(xmlUrl, true);
113     }
114
115     /**
116      * Creates an FlatXmlDataSet object with the specifed xml URL.
117      * Relative DOCTYPE uri are resolved from the xml file path.
118      *
119      * @param xmlUrl the xml URL
120      * @param dtdMetadata if <code>false</code> do not use DTD as metadata
121      */

122     public FlatXmlDataSet(URL JavaDoc xmlUrl, boolean dtdMetadata)
123             throws IOException JavaDoc, DataSetException
124     {
125         super(new FlatXmlProducer(
126                 new InputSource JavaDoc(xmlUrl.toString()), dtdMetadata));
127     }
128     
129     /**
130      * Creates an FlatXmlDataSet object with the specifed xml reader.
131      * Relative DOCTYPE uri are resolved from the current working dicrectory.
132      *
133      * @param xmlReader the xml reader
134      */

135     public FlatXmlDataSet(Reader JavaDoc xmlReader) throws IOException JavaDoc, DataSetException
136     {
137         this(xmlReader, true);
138     }
139
140     /**
141      * Creates an FlatXmlDataSet object with the specifed xml reader.
142      * Relative DOCTYPE uri are resolved from the current working dicrectory.
143      *
144      * @param xmlReader the xml reader
145      * @param dtdMetadata if <code>false</code> do not use DTD as metadata
146      */

147     public FlatXmlDataSet(Reader JavaDoc xmlReader, boolean dtdMetadata)
148             throws IOException JavaDoc, DataSetException
149     {
150         super(new FlatXmlProducer(
151                 new InputSource JavaDoc(xmlReader), dtdMetadata));
152     }
153
154     /**
155      * Creates an FlatXmlDataSet object with the specifed xml and dtd readers.
156      *
157      * @param xmlReader the xml reader
158      * @param dtdReader the dtd reader
159      */

160     public FlatXmlDataSet(Reader JavaDoc xmlReader, Reader JavaDoc dtdReader)
161             throws IOException JavaDoc, DataSetException
162     {
163         this(xmlReader, new FlatDtdDataSet(dtdReader));
164     }
165
166     /**
167      * Creates an FlatXmlDataSet object with the specifed xml reader.
168      *
169      * @param xmlReader the xml reader
170      * @param metaDataSet the dataset used as metadata source.
171      */

172     public FlatXmlDataSet(Reader JavaDoc xmlReader, IDataSet metaDataSet)
173             throws IOException JavaDoc, DataSetException
174     {
175         super(new FlatXmlProducer(
176                 new InputSource JavaDoc(xmlReader), metaDataSet));
177     }
178
179     /**
180      * Creates an FlatXmlDataSet object with the specifed xml input stream.
181      * Relative DOCTYPE uri are resolved from the current working dicrectory.
182      *
183      * @param xmlStream the xml input stream
184      */

185     public FlatXmlDataSet(InputStream JavaDoc xmlStream) throws IOException JavaDoc, DataSetException
186     {
187         this(xmlStream, true);
188     }
189
190     /**
191      * Creates an FlatXmlDataSet object with the specifed xml input stream.
192      * Relative DOCTYPE uri are resolved from the current working dicrectory.
193      *
194      * @param xmlStream the xml input stream
195      * @param dtdMetadata if <code>false</code> do not use DTD as metadata
196      */

197     public FlatXmlDataSet(InputStream JavaDoc xmlStream, boolean dtdMetadata)
198             throws IOException JavaDoc, DataSetException
199     {
200         super(new FlatXmlProducer(
201                 new InputSource JavaDoc(xmlStream), dtdMetadata));
202     }
203
204     /**
205      * Creates an FlatXmlDataSet object with the specifed xml and dtd input
206      * stream.
207      *
208      * @param xmlStream the xml input stream
209      * @param dtdStream the dtd input stream
210      */

211     public FlatXmlDataSet(InputStream JavaDoc xmlStream, InputStream JavaDoc dtdStream)
212             throws IOException JavaDoc, DataSetException
213     {
214         this(xmlStream, new FlatDtdDataSet(dtdStream));
215     }
216
217     /**
218      * Creates an FlatXmlDataSet object with the specifed xml input stream.
219      *
220      * @param xmlStream the xml input stream
221      * @param metaDataSet the dataset used as metadata source.
222      */

223     public FlatXmlDataSet(InputStream JavaDoc xmlStream, IDataSet metaDataSet)
224             throws IOException JavaDoc, DataSetException
225     {
226         super(new FlatXmlProducer(
227                 new InputSource JavaDoc(xmlStream), metaDataSet));
228     }
229
230     /**
231      * Write the specified dataset to the specified output stream as xml.
232      */

233     public static void write(IDataSet dataSet, OutputStream JavaDoc out)
234             throws IOException JavaDoc, DataSetException
235     {
236         FlatXmlWriter datasetWriter = new FlatXmlWriter(out);
237         datasetWriter.setIncludeEmptyTable(true);
238         datasetWriter.write(dataSet);
239     }
240
241     /**
242      * Write the specified dataset to the specified writer as xml.
243      */

244     public static void write(IDataSet dataSet, Writer JavaDoc writer)
245             throws IOException JavaDoc, DataSetException
246     {
247         write(dataSet, writer, null);
248     }
249
250     /**
251      * Write the specified dataset to the specified writer as xml.
252      */

253     public static void write(IDataSet dataSet, Writer JavaDoc writer, String JavaDoc encoding)
254             throws IOException JavaDoc, DataSetException
255     {
256         FlatXmlWriter datasetWriter = new FlatXmlWriter(writer, encoding);
257         datasetWriter.setIncludeEmptyTable(true);
258         datasetWriter.write(dataSet);
259     }
260
261     /**
262      * Write a DTD for the specified dataset to the specified output.
263      * @deprecated use {@link FlatDtdDataSet#write}
264      */

265     public static void writeDtd(IDataSet dataSet, OutputStream JavaDoc out)
266             throws IOException JavaDoc, DataSetException
267     {
268         FlatDtdDataSet.write(dataSet, out);
269     }
270 }
271
272
273
274
275
276
277
278
279
280
281
282
Popular Tags