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: import org.xml.sax.InputSource;
031:
032: import java.io.*;
033:
034: /**
035: * Reads and writes original XML dataset document. This format
036: * is very verbiose and must conform to the following DTD:
037: * <pre>
038: *
039: * <?xml version="1.0" encoding="UTF-8"?>
040: * <!ELEMENT dataset (table+)>
041: * <!ELEMENT table (column*, row*)>
042: * <!ATTLIST table
043: * name CDATA #REQUIRED
044: * >
045: * <!ELEMENT column (#PCDATA)>
046: * <!ELEMENT row (value | null | none)*>
047: * <!ELEMENT value (#PCDATA)>
048: * <!ELEMENT null EMPTY>
049: *</pre>
050: * @author Manuel Laflamme
051: * @version $Revision: 554 $
052: * @since Feb 17, 2002
053: */
054: public class XmlDataSet extends CachedDataSet {
055:
056: /**
057: * Logger for this class
058: */
059: private static final Logger logger = LoggerFactory
060: .getLogger(XmlDataSet.class);
061:
062: private static final String DEFAULT_ENCODING = "UTF8";
063:
064: /**
065: * Creates an XmlDataSet with the specified xml reader.
066: */
067: public XmlDataSet(Reader reader) throws DataSetException {
068: super (new XmlProducer(new InputSource(reader)));
069: }
070:
071: /**
072: * Creates an XmlDataSet with the specified xml input stream.
073: */
074: public XmlDataSet(InputStream in) throws DataSetException {
075: super (new XmlProducer(new InputSource(in)));
076: }
077:
078: /**
079: * Write the specified dataset to the specified output stream as xml.
080: */
081: public static void write(IDataSet dataSet, OutputStream out)
082: throws IOException, DataSetException {
083: logger.debug("write(dataSet=" + dataSet + ", out=" + out
084: + ") - start");
085:
086: OutputStreamWriter writer = new OutputStreamWriter(out,
087: DEFAULT_ENCODING);
088: write(dataSet, writer);
089: }
090:
091: /**
092: * Write the specified dataset to the specified writer as xml.
093: */
094: public static void write(IDataSet dataSet, Writer writer)
095: throws IOException, DataSetException {
096: logger.debug("write(dataSet=" + dataSet + ", writer=" + writer
097: + ") - start");
098:
099: write(dataSet, writer, null);
100: }
101:
102: /**
103: * Write the specified dataset to the specified writer as xml.
104: */
105: public static void write(IDataSet dataSet, Writer writer,
106: String encoding) throws IOException, DataSetException {
107: logger.debug("write(dataSet=" + dataSet + ", writer=" + writer
108: + ", encoding=" + encoding + ") - start");
109:
110: XmlDataSetWriter datasetWriter = new XmlDataSetWriter(writer,
111: encoding);
112: datasetWriter.write(dataSet);
113: }
114: }
|