001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021:
022: package org.dbunit.dataset.xml;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import org.dbunit.dataset.CachedDataSet;
028: import org.dbunit.dataset.DataSetException;
029: import org.dbunit.dataset.IDataSet;
030:
031: import org.xml.sax.InputSource;
032:
033: import java.io.File;
034: import java.io.IOException;
035: import java.io.InputStream;
036: import java.io.OutputStream;
037: import java.io.Reader;
038: import java.io.Writer;
039: import java.net.URL;
040:
041: /**
042: * Reads and writes flat XML dataset document. Each XML element corresponds to a table row.
043: * Each XML element name corresponds to a table name. The XML attributes
044: * correspond to table columns.
045: * <p>
046: * Flat XML dataset document sample:
047: * <p>
048: * <pre>
049: * <!DOCTYPE dataset SYSTEM "my-dataset.dtd">
050: * <dataset>
051: * <TEST_TABLE COL0="row 0 col 0"
052: * COL1="row 0 col 1"
053: * COL2="row 0 col 2"/>
054: * <TEST_TABLE COL1="row 1 col 1"/>
055: * <SECOND_TABLE COL0="row 0 col 0"
056: * COL1="row 0 col 1" />
057: * <EMPTY_TABLE/>
058: * </dataset></pre>
059: * <p>
060: * To specify null values, omit corresponding attribute.
061: * In the above example, missing COL0 and COL2 attributes of TEST_TABLE second row represents null values.
062: * <p>
063: * Table metadata is deduced from the first row of each table. <b>Beware that DbUnit may think
064: * a table miss some columns if the first row of that table has one or more null values.</b>
065: * Because of that, this is highly recommended to use DTD. DbUnit will use the
066: * columns declared in the DTD as table metadata. DbUnit only support external system URI.
067: * The URI can be absolute or relative.
068: *
069: * @author Manuel Laflamme
070: * @version $Revision: 554 $
071: * @since Mar 12, 2002
072: */
073: public class FlatXmlDataSet extends CachedDataSet {
074:
075: /**
076: * Logger for this class
077: */
078: private static final Logger logger = LoggerFactory
079: .getLogger(FlatXmlDataSet.class);
080:
081: /**
082: * Creates an FlatXmlDataSet object with the specifed InputSource.
083: */
084: public FlatXmlDataSet(InputSource source) throws IOException,
085: DataSetException {
086: super (new FlatXmlProducer(source));
087: }
088:
089: /**
090: * Creates an FlatXmlDataSet object with the specifed xml file.
091: * Relative DOCTYPE uri are resolved from the xml file path.
092: *
093: * @param xmlFile the xml file
094: */
095: public FlatXmlDataSet(File xmlFile) throws IOException,
096: DataSetException {
097: this (xmlFile, true);
098: }
099:
100: /**
101: * Creates an FlatXmlDataSet object with the specifed xml file.
102: * Relative DOCTYPE uri are resolved from the xml file path.
103: *
104: * @param xmlFile the xml file
105: * @param dtdMetadata if <code>false</code> do not use DTD as metadata
106: */
107: public FlatXmlDataSet(File xmlFile, boolean dtdMetadata)
108: throws IOException, DataSetException {
109: this (xmlFile.toURL(), dtdMetadata);
110: }
111:
112: /**
113: * Creates an FlatXmlDataSet object with the specifed xml URL.
114: * Relative DOCTYPE uri are resolved from the xml file path.
115: *
116: * @param xmlUrl the xml URL
117: */
118: public FlatXmlDataSet(URL xmlUrl) throws IOException,
119: DataSetException {
120: this (xmlUrl, true);
121: }
122:
123: /**
124: * Creates an FlatXmlDataSet object with the specifed xml URL.
125: * Relative DOCTYPE uri are resolved from the xml file path.
126: *
127: * @param xmlUrl the xml URL
128: * @param dtdMetadata if <code>false</code> do not use DTD as metadata
129: */
130: public FlatXmlDataSet(URL xmlUrl, boolean dtdMetadata)
131: throws IOException, DataSetException {
132: super (new FlatXmlProducer(new InputSource(xmlUrl.toString()),
133: dtdMetadata));
134: }
135:
136: /**
137: * Creates an FlatXmlDataSet object with the specifed xml reader.
138: * Relative DOCTYPE uri are resolved from the current working dicrectory.
139: *
140: * @param xmlReader the xml reader
141: */
142: public FlatXmlDataSet(Reader xmlReader) throws IOException,
143: DataSetException {
144: this (xmlReader, true);
145: }
146:
147: /**
148: * Creates an FlatXmlDataSet object with the specifed xml reader.
149: * Relative DOCTYPE uri are resolved from the current working dicrectory.
150: *
151: * @param xmlReader the xml reader
152: * @param dtdMetadata if <code>false</code> do not use DTD as metadata
153: */
154: public FlatXmlDataSet(Reader xmlReader, boolean dtdMetadata)
155: throws IOException, DataSetException {
156: super (new FlatXmlProducer(new InputSource(xmlReader),
157: dtdMetadata));
158: }
159:
160: /**
161: * Creates an FlatXmlDataSet object with the specifed xml and dtd readers.
162: *
163: * @param xmlReader the xml reader
164: * @param dtdReader the dtd reader
165: */
166: public FlatXmlDataSet(Reader xmlReader, Reader dtdReader)
167: throws IOException, DataSetException {
168: this (xmlReader, new FlatDtdDataSet(dtdReader));
169: }
170:
171: /**
172: * Creates an FlatXmlDataSet object with the specifed xml reader.
173: *
174: * @param xmlReader the xml reader
175: * @param metaDataSet the dataset used as metadata source.
176: */
177: public FlatXmlDataSet(Reader xmlReader, IDataSet metaDataSet)
178: throws IOException, DataSetException {
179: super (new FlatXmlProducer(new InputSource(xmlReader),
180: metaDataSet));
181: }
182:
183: /**
184: * Creates an FlatXmlDataSet object with the specifed xml input stream.
185: * Relative DOCTYPE uri are resolved from the current working dicrectory.
186: *
187: * @param xmlStream the xml input stream
188: */
189: public FlatXmlDataSet(InputStream xmlStream) throws IOException,
190: DataSetException {
191: this (xmlStream, true);
192: }
193:
194: /**
195: * Creates an FlatXmlDataSet object with the specifed xml input stream.
196: * Relative DOCTYPE uri are resolved from the current working dicrectory.
197: *
198: * @param xmlStream the xml input stream
199: * @param dtdMetadata if <code>false</code> do not use DTD as metadata
200: */
201: public FlatXmlDataSet(InputStream xmlStream, boolean dtdMetadata)
202: throws IOException, DataSetException {
203: super (new FlatXmlProducer(new InputSource(xmlStream),
204: dtdMetadata));
205: }
206:
207: /**
208: * Creates an FlatXmlDataSet object with the specifed xml and dtd input
209: * stream.
210: *
211: * @param xmlStream the xml input stream
212: * @param dtdStream the dtd input stream
213: */
214: public FlatXmlDataSet(InputStream xmlStream, InputStream dtdStream)
215: throws IOException, DataSetException {
216: this (xmlStream, new FlatDtdDataSet(dtdStream));
217: }
218:
219: /**
220: * Creates an FlatXmlDataSet object with the specifed xml input stream.
221: *
222: * @param xmlStream the xml input stream
223: * @param metaDataSet the dataset used as metadata source.
224: */
225: public FlatXmlDataSet(InputStream xmlStream, IDataSet metaDataSet)
226: throws IOException, DataSetException {
227: super (new FlatXmlProducer(new InputSource(xmlStream),
228: metaDataSet));
229: }
230:
231: /**
232: * Write the specified dataset to the specified output stream as xml.
233: */
234: public static void write(IDataSet dataSet, OutputStream out)
235: throws IOException, DataSetException {
236: logger.debug("write(dataSet=" + dataSet + ", out=" + out
237: + ") - start");
238:
239: FlatXmlWriter datasetWriter = new FlatXmlWriter(out);
240: datasetWriter.setIncludeEmptyTable(true);
241: datasetWriter.write(dataSet);
242: }
243:
244: /**
245: * Write the specified dataset to the specified writer as xml.
246: */
247: public static void write(IDataSet dataSet, Writer writer)
248: throws IOException, DataSetException {
249: logger.debug("write(dataSet=" + dataSet + ", writer=" + writer
250: + ") - start");
251:
252: write(dataSet, writer, null);
253: }
254:
255: /**
256: * Write the specified dataset to the specified writer as xml.
257: */
258: public static void write(IDataSet dataSet, Writer writer,
259: String encoding) throws IOException, DataSetException {
260: logger.debug("write(dataSet=" + dataSet + ", writer=" + writer
261: + ", encoding=" + encoding + ") - start");
262:
263: FlatXmlWriter datasetWriter = new FlatXmlWriter(writer,
264: encoding);
265: datasetWriter.setIncludeEmptyTable(true);
266: datasetWriter.write(dataSet);
267: }
268:
269: /**
270: * Write a DTD for the specified dataset to the specified output.
271: * @deprecated use {@link FlatDtdDataSet#write}
272: */
273: public static void writeDtd(IDataSet dataSet, OutputStream out)
274: throws IOException, DataSetException {
275: logger.debug("writeDtd(dataSet=" + dataSet + ", out=" + out
276: + ") - start");
277:
278: FlatDtdDataSet.write(dataSet, out);
279: }
280: }
|