FlatXmlWriter.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.OutputStream;
  24. import java.io.UnsupportedEncodingException;
  25. import java.io.Writer;
  26. import java.nio.charset.Charset;

  27. import org.dbunit.dataset.Column;
  28. import org.dbunit.dataset.DataSetException;
  29. import org.dbunit.dataset.IDataSet;
  30. import org.dbunit.dataset.ITableMetaData;
  31. import org.dbunit.dataset.datatype.DataType;
  32. import org.dbunit.dataset.datatype.TypeCastException;
  33. import org.dbunit.dataset.stream.DataSetProducerAdapter;
  34. import org.dbunit.dataset.stream.IDataSetConsumer;
  35. import org.dbunit.util.xml.XmlWriter;
  36. import org.slf4j.Logger;
  37. import org.slf4j.LoggerFactory;

  38. /**
  39.  * @author Manuel Laflamme
  40.  * @author Last changed by: $Author$
  41.  * @version $Revision$ $Date$
  42.  * @since 1.5.5 (Apr 19, 2003)
  43.  */
  44. public class FlatXmlWriter implements IDataSetConsumer
  45. {

  46.     /**
  47.      * Logger for this class
  48.      */
  49.     private static final Logger logger = LoggerFactory.getLogger(FlatXmlWriter.class);

  50.     private static final String DATASET = "dataset";

  51.     private XmlWriter _xmlWriter;
  52.     private ITableMetaData _activeMetaData;
  53.     private int _activeRowCount;
  54.     private boolean _includeEmptyTable = false;
  55.     private String _systemId = null;

  56.     public FlatXmlWriter(OutputStream out) throws IOException
  57.     {
  58.         this(out, null);
  59.     }

  60.     /**
  61.      * @param outputStream The stream to which the XML will be written.
  62.      * @param encoding The encoding to be used for the {@link XmlWriter}.
  63.      * Can be null. See {@link XmlWriter#XmlWriter(OutputStream, String)}.
  64.      * @throws UnsupportedEncodingException
  65.      */
  66.     public FlatXmlWriter(OutputStream outputStream, Charset charset)
  67.     {
  68.         _xmlWriter = new XmlWriter(outputStream, charset);
  69.         _xmlWriter.enablePrettyPrint(true);
  70.     }

  71.     public FlatXmlWriter(Writer writer)
  72.     {
  73.         _xmlWriter = new XmlWriter(writer);
  74.         _xmlWriter.enablePrettyPrint(true);
  75.     }

  76.     public FlatXmlWriter(Writer writer, Charset charset)
  77.     {
  78.         _xmlWriter = new XmlWriter(writer, charset);
  79.         _xmlWriter.enablePrettyPrint(true);
  80.     }

  81.     public void setIncludeEmptyTable(boolean includeEmptyTable)
  82.     {
  83.         _includeEmptyTable = includeEmptyTable;
  84.     }

  85.     public void setDocType(String systemId)
  86.     {
  87.         _systemId = systemId;
  88.     }

  89.     /**
  90.      * Enable or disable pretty print of the XML.
  91.      * @param enabled <code>true</code> to enable pretty print (which is the default).
  92.      * <code>false</code> otherwise.
  93.      * @since 2.4
  94.      */
  95.     public void setPrettyPrint(boolean enabled)
  96.     {
  97.         _xmlWriter.enablePrettyPrint(enabled);
  98.     }
  99.    
  100.     /**
  101.      * Writes the given {@link IDataSet} using this writer.
  102.      * @param dataSet The {@link IDataSet} to be written
  103.      * @throws DataSetException
  104.      */
  105.     public void write(IDataSet dataSet) throws DataSetException
  106.     {
  107.         logger.debug("write(dataSet={}) - start", dataSet);

  108.         DataSetProducerAdapter provider = new DataSetProducerAdapter(dataSet);
  109.         provider.setConsumer(this);
  110.         provider.produce();
  111.     }

  112.     ////////////////////////////////////////////////////////////////////////////
  113.     // IDataSetConsumer interface

  114.     public void startDataSet() throws DataSetException
  115.     {
  116.         logger.debug("startDataSet() - start");

  117.         try
  118.         {
  119.             _xmlWriter.writeDeclaration();
  120.             _xmlWriter.writeDoctype(_systemId, null);
  121.             _xmlWriter.writeElement(DATASET);
  122.         }
  123.         catch (IOException e)
  124.         {
  125.             throw new DataSetException(e);
  126.         }
  127.     }

  128.     public void endDataSet() throws DataSetException
  129.     {
  130.         logger.debug("endDataSet() - start");

  131.         try
  132.         {
  133.             _xmlWriter.endElement();
  134.             _xmlWriter.close();
  135.         }
  136.         catch (IOException e)
  137.         {
  138.             throw new DataSetException(e);
  139.         }
  140.     }

  141.     public void startTable(ITableMetaData metaData) throws DataSetException
  142.     {
  143.         logger.debug("startTable(metaData={}) - start", metaData);

  144.         _activeMetaData = metaData;
  145.         _activeRowCount = 0;
  146.     }

  147.     public void endTable() throws DataSetException
  148.     {
  149.         logger.debug("endTable() - start");

  150.         if (_includeEmptyTable && _activeRowCount == 0)
  151.         {
  152.             try
  153.             {
  154.                 String tableName = _activeMetaData.getTableName();
  155.                 _xmlWriter.writeEmptyElement(tableName);
  156.             }
  157.             catch (IOException e)
  158.             {
  159.                 throw new DataSetException(e);
  160.             }
  161.         }

  162.         _activeMetaData = null;
  163.     }

  164.     public void row(Object[] values) throws DataSetException
  165.     {
  166.         logger.debug("row(values={}) - start", values);

  167.         try
  168.         {
  169.             String tableName = _activeMetaData.getTableName();
  170.             _xmlWriter.writeElement(tableName);

  171.             Column[] columns = _activeMetaData.getColumns();
  172.             for (int i = 0; i < columns.length; i++)
  173.             {
  174.                 String columnName = columns[i].getColumnName();
  175.                 Object value = values[i];

  176.                 // Skip null value
  177.                 if (value == null)
  178.                 {
  179.                     continue;
  180.                 }

  181.                 try
  182.                 {
  183.                     String stringValue = DataType.asString(value);
  184.                     _xmlWriter.writeAttribute(columnName, stringValue, true);
  185.                 }
  186.                 catch (TypeCastException e)
  187.                 {
  188.                     throw new DataSetException("table=" +
  189.                             _activeMetaData.getTableName() + ", row=" + i +
  190.                             ", column=" + columnName +
  191.                             ", value=" + value, e);
  192.                 }
  193.             }

  194.             _activeRowCount++;
  195.             _xmlWriter.endElement();
  196.         }
  197.         catch (IOException e)
  198.         {
  199.             throw new DataSetException(e);
  200.         }
  201.     }
  202. }