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.Reader;
28 import java.io.Writer;
29 import java.nio.charset.Charset;
30
31 import org.dbunit.dataset.CachedDataSet;
32 import org.dbunit.dataset.DataSetException;
33 import org.dbunit.dataset.IDataSet;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.xml.sax.InputSource;
37
38 /**
39 * Reads and writes original XML dataset document. This format
40 * is very verbose and must conform to the following DTD:
41 *
42 <pre>
43 <?xml version="1.0" encoding="UTF-8"?>
44 <!ELEMENT dataset (table+)>
45 <!ELEMENT table (column*, row*)>
46 <!ATTLIST table name CDATA #REQUIRED>
47 <!ELEMENT column (#PCDATA)>
48 <!ELEMENT row (value | null | none)*>
49 <!ELEMENT value (#PCDATA)>
50 <!ELEMENT null EMPTY>
51 <!ELEMENT none EMPTY>
52 </pre>
53
54 *
55 * @author Manuel Laflamme
56 * @author Last changed by: $Author$
57 * @version $Revision$ $Date$
58 * @since 1.0 (Feb 17, 2002)
59 */
60 public class XmlDataSet extends CachedDataSet
61 {
62
63 /**
64 * Logger for this class
65 */
66 private static final Logger logger = LoggerFactory.getLogger(XmlDataSet.class);
67
68
69 /**
70 * Creates an XmlDataSet with the specified xml reader.
71 */
72 public XmlDataSet(Reader reader) throws DataSetException
73 {
74 super(new XmlProducer(new InputSource(reader)));
75 }
76
77 /**
78 * Creates an XmlDataSet with the specified xml input stream.
79 */
80 public XmlDataSet(InputStream in) throws DataSetException
81 {
82 super(new XmlProducer(new InputSource(in)));
83 }
84
85 /**
86 * Write the specified dataset to the specified output stream as xml.
87 */
88 public static void write(IDataSet dataSet, OutputStream out)
89 throws IOException, DataSetException
90 {
91 logger.debug("write(dataSet={}, out={}) - start", dataSet, out);
92 XmlDataSet.write(dataSet, out, null);
93 }
94
95 /**
96 * Write the specified dataset to the specified output stream as xml (using specified encoding).
97 */
98 public static void write(IDataSet dataSet, OutputStream out, Charset charset)
99 throws IOException, DataSetException
100 {
101 logger.debug("write(dataSet={}, out={}, charset={}) - start",
102 dataSet, out, charset);
103
104 XmlDataSetWriter datasetWriter = new XmlDataSetWriter(out, charset);
105 datasetWriter.write(dataSet);
106 }
107
108 /**
109 * Write the specified dataset to the specified writer as xml.
110 */
111 public static void write(IDataSet dataSet, Writer writer)
112 throws IOException, DataSetException
113 {
114 logger.debug("write(dataSet={}, writer={}) - start", dataSet, writer);
115 write(dataSet, writer, Charset.defaultCharset());
116 }
117
118 /**
119 * Write the specified dataset to the specified writer as xml.
120 */
121 public static void write(IDataSet dataSet, Writer writer, Charset charset)
122 throws IOException, DataSetException
123 {
124 logger.debug("write(dataSet={}, writer={}, charset={}) - start",
125 dataSet, writer, charset);
126
127 XmlDataSetWriter datasetWriter = new XmlDataSetWriter(writer, charset);
128 datasetWriter.write(dataSet);
129 }
130 }