View Javadoc
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  
23  import java.io.IOException;
24  import java.io.OutputStream;
25  import java.io.UnsupportedEncodingException;
26  import java.io.Writer;
27  
28  import org.dbunit.dataset.Column;
29  import org.dbunit.dataset.DataSetException;
30  import org.dbunit.dataset.IDataSet;
31  import org.dbunit.dataset.ITableMetaData;
32  import org.dbunit.dataset.datatype.DataType;
33  import org.dbunit.dataset.datatype.TypeCastException;
34  import org.dbunit.dataset.stream.DataSetProducerAdapter;
35  import org.dbunit.dataset.stream.IDataSetConsumer;
36  import org.dbunit.util.xml.XmlWriter;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  /**
41   * @author Manuel Laflamme
42   * @author Last changed by: $Author$
43   * @version $Revision$ $Date$
44   * @since 1.5.5 (Apr 19, 2003)
45   */
46  public class FlatXmlWriter implements IDataSetConsumer
47  {
48  
49      /**
50       * Logger for this class
51       */
52      private static final Logger logger = LoggerFactory.getLogger(FlatXmlWriter.class);
53  
54      private static final String DATASET = "dataset";
55  
56      private XmlWriter _xmlWriter;
57      private ITableMetaData _activeMetaData;
58      private int _activeRowCount;
59      private boolean _includeEmptyTable = false;
60      private String _systemId = null;
61  
62      public FlatXmlWriter(OutputStream out) throws IOException
63      {
64          this(out, null);
65      }
66  
67      /**
68       * @param outputStream The stream to which the XML will be written.
69       * @param encoding The encoding to be used for the {@link XmlWriter}.
70       * Can be null. See {@link XmlWriter#XmlWriter(OutputStream, String)}.
71       * @throws UnsupportedEncodingException
72       */
73      public FlatXmlWriter(OutputStream outputStream, String encoding) 
74      throws UnsupportedEncodingException
75      {
76          _xmlWriter = new XmlWriter(outputStream, encoding);
77          _xmlWriter.enablePrettyPrint(true);
78      }
79  
80      public FlatXmlWriter(Writer writer)
81      {
82          _xmlWriter = new XmlWriter(writer);
83          _xmlWriter.enablePrettyPrint(true);
84      }
85  
86      public FlatXmlWriter(Writer writer, String encoding)
87      {
88          _xmlWriter = new XmlWriter(writer, encoding);
89          _xmlWriter.enablePrettyPrint(true);
90      }
91  
92      public void setIncludeEmptyTable(boolean includeEmptyTable)
93      {
94          _includeEmptyTable = includeEmptyTable;
95      }
96  
97      public void setDocType(String systemId)
98      {
99          _systemId = systemId;
100     }
101 
102     /**
103      * Enable or disable pretty print of the XML.
104      * @param enabled <code>true</code> to enable pretty print (which is the default). 
105      * <code>false</code> otherwise.
106      * @since 2.4
107      */
108     public void setPrettyPrint(boolean enabled)
109     {
110         _xmlWriter.enablePrettyPrint(enabled);
111     }
112     
113     /**
114      * Writes the given {@link IDataSet} using this writer.
115      * @param dataSet The {@link IDataSet} to be written
116      * @throws DataSetException
117      */
118     public void write(IDataSet dataSet) throws DataSetException
119     {
120         logger.debug("write(dataSet={}) - start", dataSet);
121 
122         DataSetProducerAdapter provider = new DataSetProducerAdapter(dataSet);
123         provider.setConsumer(this);
124         provider.produce();
125     }
126 
127     ////////////////////////////////////////////////////////////////////////////
128     // IDataSetConsumer interface
129 
130     public void startDataSet() throws DataSetException
131     {
132         logger.debug("startDataSet() - start");
133 
134         try
135         {
136             _xmlWriter.writeDeclaration();
137             _xmlWriter.writeDoctype(_systemId, null);
138             _xmlWriter.writeElement(DATASET);
139         }
140         catch (IOException e)
141         {
142             throw new DataSetException(e);
143         }
144     }
145 
146     public void endDataSet() throws DataSetException
147     {
148         logger.debug("endDataSet() - start");
149 
150         try
151         {
152             _xmlWriter.endElement();
153             _xmlWriter.close();
154         }
155         catch (IOException e)
156         {
157             throw new DataSetException(e);
158         }
159     }
160 
161     public void startTable(ITableMetaData metaData) throws DataSetException
162     {
163         logger.debug("startTable(metaData={}) - start", metaData);
164 
165         _activeMetaData = metaData;
166         _activeRowCount = 0;
167     }
168 
169     public void endTable() throws DataSetException
170     {
171         logger.debug("endTable() - start");
172 
173         if (_includeEmptyTable && _activeRowCount == 0)
174         {
175             try
176             {
177                 String tableName = _activeMetaData.getTableName();
178                 _xmlWriter.writeEmptyElement(tableName);
179             }
180             catch (IOException e)
181             {
182                 throw new DataSetException(e);
183             }
184         }
185 
186         _activeMetaData = null;
187     }
188 
189     public void row(Object[] values) throws DataSetException
190     {
191         logger.debug("row(values={}) - start", values);
192 
193         try
194         {
195             String tableName = _activeMetaData.getTableName();
196             _xmlWriter.writeElement(tableName);
197 
198             Column[] columns = _activeMetaData.getColumns();
199             for (int i = 0; i < columns.length; i++)
200             {
201                 String columnName = columns[i].getColumnName();
202                 Object value = values[i];
203 
204                 // Skip null value
205                 if (value == null)
206                 {
207                     continue;
208                 }
209 
210                 try
211                 {
212                     String stringValue = DataType.asString(value);
213                     _xmlWriter.writeAttribute(columnName, stringValue, true);
214                 }
215                 catch (TypeCastException e)
216                 {
217                     throw new DataSetException("table=" +
218                             _activeMetaData.getTableName() + ", row=" + i +
219                             ", column=" + columnName +
220                             ", value=" + value, e);
221                 }
222             }
223 
224             _activeRowCount++;
225             _xmlWriter.endElement();
226         }
227         catch (IOException e)
228         {
229             throw new DataSetException(e);
230         }
231     }
232 }