KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.xml.sax.InputSource JavaDoc;
28
29 import java.io.*;
30
31 /**
32  * Reads and writes original XML dataset document. This format
33  * is very verbiose and must conform to the following DTD:
34  * <pre>
35  *
36  * &lt;?xml version="1.0" encoding="UTF-8"?&gt;
37  * &lt;!ELEMENT dataset (table+)&gt;
38  * &lt;!ELEMENT table (column*, row*)&gt;
39  * &lt;!ATTLIST table
40  * name CDATA #REQUIRED
41  * &gt;
42  * &lt;!ELEMENT column (#PCDATA)&gt;
43  * &lt;!ELEMENT row (value | null | none)*&gt;
44  * &lt;!ELEMENT value (#PCDATA)&gt;
45  * &lt;!ELEMENT null EMPTY&gt;
46  *</pre>
47  * @author Manuel Laflamme
48  * @version $Revision: 1.20 $
49  * @since Feb 17, 2002
50  */

51 public class XmlDataSet extends CachedDataSet
52 {
53     private static final String JavaDoc DEFAULT_ENCODING = "UTF8";
54
55     /**
56      * Creates an XmlDataSet with the specified xml reader.
57      */

58     public XmlDataSet(Reader reader) throws DataSetException
59     {
60         super(new XmlProducer(new InputSource JavaDoc(reader)));
61     }
62
63     /**
64      * Creates an XmlDataSet with the specified xml input stream.
65      */

66     public XmlDataSet(InputStream in) throws DataSetException
67     {
68         super(new XmlProducer(new InputSource JavaDoc(in)));
69     }
70
71     /**
72      * Write the specified dataset to the specified output stream as xml.
73      */

74     public static void write(IDataSet dataSet, OutputStream out)
75             throws IOException, DataSetException
76     {
77         OutputStreamWriter writer = new OutputStreamWriter(out, DEFAULT_ENCODING);
78         write(dataSet, writer);
79     }
80
81     /**
82      * Write the specified dataset to the specified writer as xml.
83      */

84     public static void write(IDataSet dataSet, Writer writer)
85             throws IOException, DataSetException
86     {
87         write(dataSet, writer, null);
88     }
89
90     /**
91      * Write the specified dataset to the specified writer as xml.
92      */

93     public static void write(IDataSet dataSet, Writer writer, String JavaDoc encoding)
94             throws IOException, DataSetException
95     {
96         XmlDataSetWriter datasetWriter = new XmlDataSetWriter(writer, encoding);
97         datasetWriter.write(dataSet);
98     }
99 }
Popular Tags