KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > xml > FlatXmlWriter


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 org.dbunit.dataset.Column;
24 import org.dbunit.dataset.DataSetException;
25 import org.dbunit.dataset.IDataSet;
26 import org.dbunit.dataset.ITableMetaData;
27 import org.dbunit.dataset.datatype.DataType;
28 import org.dbunit.dataset.datatype.TypeCastException;
29 import org.dbunit.dataset.stream.DataSetProducerAdapter;
30 import org.dbunit.dataset.stream.IDataSetConsumer;
31 import org.dbunit.util.xml.XmlWriter;
32
33 import java.io.IOException JavaDoc;
34 import java.io.OutputStream JavaDoc;
35 import java.io.OutputStreamWriter JavaDoc;
36 import java.io.Writer JavaDoc;
37
38 /**
39  * @author Manuel Laflamme
40  * @since Apr 19, 2003
41  * @version $Revision: 1.7 $
42  */

43 public class FlatXmlWriter implements IDataSetConsumer
44 {
45     private static final String JavaDoc DEFAULT_ENCODING = "UTF8";
46     private static final String JavaDoc DATASET = "dataset";
47
48     private XmlWriter _xmlWriter;
49     private ITableMetaData _activeMetaData;
50     private int _activeRowCount;
51     private boolean _includeEmptyTable = false;
52     private String JavaDoc _systemId = null;
53
54     public FlatXmlWriter(Writer writer)
55     {
56         _xmlWriter = new XmlWriter(writer);
57         _xmlWriter.enablePrettyPrint(true);
58     }
59
60     public FlatXmlWriter(OutputStream JavaDoc out) throws IOException JavaDoc
61     {
62         _xmlWriter = new XmlWriter(new OutputStreamWriter JavaDoc(out, DEFAULT_ENCODING));
63         _xmlWriter.enablePrettyPrint(true);
64     }
65
66     public FlatXmlWriter(Writer writer, String JavaDoc encoding)
67     {
68         _xmlWriter = new XmlWriter(writer, encoding);
69         _xmlWriter.enablePrettyPrint(true);
70     }
71
72     public void setIncludeEmptyTable(boolean includeEmptyTable)
73     {
74         _includeEmptyTable = includeEmptyTable;
75     }
76
77     public void setDocType(String JavaDoc systemId)
78     {
79         _systemId = systemId;
80     }
81
82     public void write(IDataSet dataSet) throws DataSetException
83     {
84         DataSetProducerAdapter provider = new DataSetProducerAdapter(dataSet);
85         provider.setConsumer(this);
86         provider.produce();
87     }
88
89     ////////////////////////////////////////////////////////////////////////////
90
// IDataSetConsumer interface
91

92     public void startDataSet() throws DataSetException
93     {
94         try
95         {
96             _xmlWriter.writeDeclaration();
97             _xmlWriter.writeDoctype(_systemId, null);
98             _xmlWriter.writeElement(DATASET);
99         }
100         catch (IOException JavaDoc e)
101         {
102             throw new DataSetException(e);
103         }
104     }
105
106     public void endDataSet() throws DataSetException
107     {
108         try
109         {
110             _xmlWriter.endElement();
111             _xmlWriter.close();
112         }
113         catch (IOException JavaDoc e)
114         {
115             throw new DataSetException(e);
116         }
117     }
118
119     public void startTable(ITableMetaData metaData) throws DataSetException
120     {
121         _activeMetaData = metaData;
122         _activeRowCount = 0;
123     }
124
125     public void endTable() throws DataSetException
126     {
127         if (_includeEmptyTable && _activeRowCount == 0)
128         {
129             try
130             {
131                 String JavaDoc tableName = _activeMetaData.getTableName();
132                 _xmlWriter.writeEmptyElement(tableName);
133             }
134             catch (IOException JavaDoc e)
135             {
136                 throw new DataSetException(e);
137             }
138         }
139
140         _activeMetaData = null;
141     }
142
143     public void row(Object JavaDoc[] values) throws DataSetException
144     {
145         try
146         {
147             String JavaDoc tableName = _activeMetaData.getTableName();
148             _xmlWriter.writeElement(tableName);
149
150             Column[] columns = _activeMetaData.getColumns();
151             for (int i = 0; i < columns.length; i++)
152             {
153                 String JavaDoc columnName = columns[i].getColumnName();
154                 Object JavaDoc value = values[i];
155
156                 // Skip null value
157
if (value == null)
158                 {
159                     continue;
160                 }
161
162                 try
163                 {
164                     String JavaDoc stringValue = DataType.asString(value);
165                     _xmlWriter.writeAttribute(columnName, stringValue);
166                 }
167                 catch (TypeCastException e)
168                 {
169                     throw new DataSetException("table=" +
170                             _activeMetaData.getTableName() + ", row=" + i +
171                             ", column=" + columnName +
172                             ", value=" + value, e);
173                 }
174             }
175
176             _activeRowCount++;
177             _xmlWriter.endElement();
178         }
179         catch (IOException JavaDoc e)
180         {
181             throw new DataSetException(e);
182         }
183     }
184 }
185
Popular Tags