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