KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
24 import org.dbunit.dataset.datatype.DataType;
25 import org.dbunit.dataset.datatype.TypeCastException;
26 import org.dbunit.dataset.stream.DataSetProducerAdapter;
27 import org.dbunit.dataset.stream.IDataSetConsumer;
28 import org.dbunit.util.xml.XmlWriter;
29
30 import java.io.IOException JavaDoc;
31 import java.io.Writer JavaDoc;
32
33 /**
34  * @author Manuel Laflamme
35  * @since Jun 13, 2003
36  * @version $Revision: 1.5 $
37  */

38 public class XmlDataSetWriter implements IDataSetConsumer
39 {
40     private static final String JavaDoc DATASET = "dataset";
41     private static final String JavaDoc TABLE = "table";
42     private static final String JavaDoc NAME = "name";
43     private static final String JavaDoc COLUMN = "column";
44     private static final String JavaDoc ROW = "row";
45     private static final String JavaDoc VALUE = "value";
46     private static final String JavaDoc NULL = "null";
47     private static final String JavaDoc NONE = "none";
48
49     static char[] CDATA_DETECTION_CHARS = new char[] {
50         0x20, '\n', '\r', '\t', // whitespace
51
'&', '<', // forbiden char
52
};
53
54     private XmlWriter _xmlWriter;
55     private ITableMetaData _activeMetaData;
56
57     public XmlDataSetWriter(Writer writer)
58     {
59         _xmlWriter = new XmlWriter(writer);
60         _xmlWriter.enablePrettyPrint(true);
61     }
62
63     public XmlDataSetWriter(Writer writer, String JavaDoc encoding)
64     {
65         _xmlWriter = new XmlWriter(writer, encoding);
66         _xmlWriter.enablePrettyPrint(true);
67     }
68
69     public void write(IDataSet dataSet) throws DataSetException
70     {
71         DataSetProducerAdapter provider = new DataSetProducerAdapter(dataSet);
72         provider.setConsumer(this);
73         provider.produce();
74     }
75
76     boolean needsCData(String JavaDoc text)
77     {
78         if (text == null)
79         {
80             return false;
81         }
82
83         for (int i = 0; i < text.length(); i++)
84         {
85             char c = text.charAt(i);
86             for (int j = 0; j < CDATA_DETECTION_CHARS.length; j++)
87             {
88                 if (CDATA_DETECTION_CHARS[j] == c)
89                 {
90                     return true;
91                 }
92             }
93         }
94         return false;
95     }
96
97     ////////////////////////////////////////////////////////////////////////////
98
// IDataSetConsumer interface
99

100     public void startDataSet() throws DataSetException
101     {
102         try
103         {
104             _xmlWriter.writeDeclaration();
105             _xmlWriter.writeElement(DATASET);
106         }
107         catch (IOException JavaDoc e)
108         {
109             throw new DataSetException(e);
110         }
111     }
112
113     public void endDataSet() throws DataSetException
114     {
115         try
116         {
117             _xmlWriter.endElement();
118             _xmlWriter.close();
119         }
120         catch (IOException JavaDoc e)
121         {
122             throw new DataSetException(e);
123         }
124     }
125
126     public void startTable(ITableMetaData metaData) throws DataSetException
127     {
128         try
129         {
130             _activeMetaData = metaData;
131
132             String JavaDoc tableName = _activeMetaData.getTableName();
133             _xmlWriter.writeElement(TABLE);
134             _xmlWriter.writeAttribute(NAME, tableName);
135
136             Column[] columns = _activeMetaData.getColumns();
137             for (int i = 0; i < columns.length; i++)
138             {
139                 String JavaDoc columnName = columns[i].getColumnName();
140                 _xmlWriter.writeElementWithText(COLUMN, columnName);
141             }
142         }
143         catch (IOException JavaDoc e)
144         {
145             throw new DataSetException(e);
146         }
147
148     }
149
150     public void endTable() throws DataSetException
151     {
152         try
153         {
154             _xmlWriter.endElement();
155             _activeMetaData = null;
156         }
157         catch (IOException JavaDoc e)
158         {
159             throw new DataSetException(e);
160         }
161     }
162
163     public void row(Object JavaDoc[] values) throws DataSetException
164     {
165         try
166         {
167             _xmlWriter.writeElement(ROW);
168
169             Column[] columns = _activeMetaData.getColumns();
170             for (int i = 0; i < columns.length; i++)
171             {
172                 String JavaDoc columnName = columns[i].getColumnName();
173                 Object JavaDoc value = values[i];
174
175                 // null
176
if (value == null)
177                 {
178                     _xmlWriter.writeEmptyElement(NULL);
179                 }
180                 // none
181
else if (value == ITable.NO_VALUE)
182                 {
183                     _xmlWriter.writeEmptyElement(NONE);
184                 }
185                 // values
186
else
187                 {
188                     try
189                     {
190                         String JavaDoc stringValue = DataType.asString(value);
191
192                         _xmlWriter.writeElement(VALUE);
193                         if (needsCData(stringValue))
194                         {
195                             _xmlWriter.writeCData(stringValue);
196                         }
197                         else if (stringValue.length() > 0)
198                         {
199                             _xmlWriter.writeText(stringValue);
200                         }
201                         _xmlWriter.endElement();
202                     }
203                     catch (TypeCastException e)
204                     {
205                         throw new DataSetException("table=" +
206                                 _activeMetaData.getTableName() + ", row=" + i +
207                                 ", column=" + columnName +
208                                 ", value=" + value, e);
209                     }
210                 }
211             }
212
213             _xmlWriter.endElement();
214         }
215         catch (IOException JavaDoc e)
216         {
217             throw new DataSetException(e);
218         }
219     }
220 }
221
Popular Tags