XmlDataSet.java

  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. package org.dbunit.dataset.xml;

  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;
  25. import java.io.Reader;
  26. import java.io.Writer;
  27. import java.nio.charset.Charset;

  28. import org.dbunit.dataset.CachedDataSet;
  29. import org.dbunit.dataset.DataSetException;
  30. import org.dbunit.dataset.IDataSet;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;
  33. import org.xml.sax.InputSource;

  34. /**
  35.  * Reads and writes original XML dataset document. This format
  36.  * is very verbose and must conform to the following DTD:
  37.  *
  38. <pre>
  39. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  40. &lt;!ELEMENT dataset (table+)&gt;
  41. &lt;!ELEMENT table (column*, row*)&gt;
  42. &lt;!ATTLIST table name CDATA #REQUIRED&gt;
  43. &lt;!ELEMENT column (#PCDATA)&gt;
  44. &lt;!ELEMENT row (value | null | none)*&gt;
  45. &lt;!ELEMENT value (#PCDATA)&gt;
  46. &lt;!ELEMENT null EMPTY&gt;
  47. &lt;!ELEMENT none EMPTY&gt;
  48. </pre>

  49.  *
  50.  * @author Manuel Laflamme
  51.  * @author Last changed by: $Author$
  52.  * @version $Revision$ $Date$
  53.  * @since 1.0 (Feb 17, 2002)
  54.  */
  55. public class XmlDataSet extends CachedDataSet
  56. {

  57.     /**
  58.      * Logger for this class
  59.      */
  60.     private static final Logger logger = LoggerFactory.getLogger(XmlDataSet.class);


  61.     /**
  62.      * Creates an XmlDataSet with the specified xml reader.
  63.      */
  64.     public XmlDataSet(Reader reader) throws DataSetException
  65.     {
  66.         super(new XmlProducer(new InputSource(reader)));
  67.     }

  68.     /**
  69.      * Creates an XmlDataSet with the specified xml input stream.
  70.      */
  71.     public XmlDataSet(InputStream in) throws DataSetException
  72.     {
  73.         super(new XmlProducer(new InputSource(in)));
  74.     }

  75.     /**
  76.      * Write the specified dataset to the specified output stream as xml.
  77.      */
  78.     public static void write(IDataSet dataSet, OutputStream out)
  79.             throws IOException, DataSetException
  80.     {
  81.         logger.debug("write(dataSet={}, out={}) - start", dataSet, out);
  82.         XmlDataSet.write(dataSet, out, null);
  83.     }

  84.     /**
  85.      * Write the specified dataset to the specified output stream as xml (using specified encoding).
  86.      */
  87.     public static void write(IDataSet dataSet, OutputStream out, Charset charset)
  88.             throws IOException, DataSetException
  89.     {
  90.         logger.debug("write(dataSet={}, out={}, charset={}) - start",
  91.                 dataSet, out, charset);

  92.         XmlDataSetWriter datasetWriter = new XmlDataSetWriter(out, charset);
  93.         datasetWriter.write(dataSet);
  94.     }

  95.     /**
  96.      * Write the specified dataset to the specified writer as xml.
  97.      */
  98.     public static void write(IDataSet dataSet, Writer writer)
  99.             throws IOException, DataSetException
  100.     {
  101.         logger.debug("write(dataSet={}, writer={}) - start", dataSet, writer);
  102.         write(dataSet, writer, Charset.defaultCharset());
  103.     }

  104.     /**
  105.      * Write the specified dataset to the specified writer as xml.
  106.      */
  107.     public static void write(IDataSet dataSet, Writer writer, Charset charset)
  108.             throws IOException, DataSetException
  109.     {
  110.         logger.debug("write(dataSet={}, writer={}, charset={}) - start",
  111.             dataSet, writer, charset);

  112.         XmlDataSetWriter datasetWriter = new XmlDataSetWriter(writer, charset);
  113.         datasetWriter.write(dataSet);
  114.     }
  115. }