KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > csv > CsvDataSetWriter


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002, Manuel Laflamme
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.csv;
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
29 import java.io.File JavaDoc;
30 import java.io.FileWriter JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.Writer JavaDoc;
33
34 /**
35  * @author fede
36  * @since 24-set-2003 15.27.05
37  * @version $Revision: 1.3 $
38  */

39 public class CsvDataSetWriter implements IDataSetConsumer {
40
41     /**
42      * todo: customizable separators (field, lines), manage the writers opened for each table
43      */

44
45     private static final String JavaDoc NULL = "null";
46     private static final String JavaDoc NONE = "none";
47     private static final String JavaDoc FIELD_SEPARATOR = ", ";
48     private static final String JavaDoc QUOTE = "\"";
49     private static final String JavaDoc ESCAPE = "\\";
50
51     private Writer JavaDoc writer;
52     private ITableMetaData _activeMetaData;
53     private String JavaDoc theDirectory;
54     private static char testExport;
55
56     public CsvDataSetWriter(String JavaDoc theDirectory) {
57         setTheDirectory(theDirectory);
58     }
59
60     public CsvDataSetWriter(File JavaDoc theDirectory) {
61         setTheDirectory(theDirectory.getAbsolutePath());
62     }
63
64     public void write(IDataSet dataSet) throws DataSetException {
65         DataSetProducerAdapter provider = new DataSetProducerAdapter(dataSet);
66         provider.setConsumer(this);
67         provider.produce();
68     }
69
70     public void startDataSet() throws DataSetException {
71         try {
72             new File JavaDoc(getTheDirectory()).mkdirs();
73         } catch (Exception JavaDoc e) {
74             throw new DataSetException("Error while creating the destination directory '" + getTheDirectory() + "'", e);
75         }
76     }
77
78     public void endDataSet() throws DataSetException {}
79
80     public void startTable(ITableMetaData metaData) throws DataSetException {
81         try {
82             _activeMetaData = metaData;
83             String JavaDoc tableName = _activeMetaData.getTableName();
84             setWriter(new FileWriter JavaDoc(getTheDirectory() + File.separator + tableName + ".csv"));
85             writeColumnNames();
86             getWriter().write(System.getProperty("line.separator"));
87         } catch (IOException JavaDoc e) {
88             throw new DataSetException(e);
89         }
90
91     }
92
93     private void writeColumnNames() throws DataSetException, IOException JavaDoc {
94         Column[] columns = _activeMetaData.getColumns();
95         for (int i = 0; i < columns.length; i++) {
96             String JavaDoc columnName = columns[i].getColumnName();
97             getWriter().write(columnName);
98             if (i < columns.length - 1) getWriter().write(FIELD_SEPARATOR);
99         }
100     }
101
102     public void endTable() throws DataSetException {
103         try {
104             getWriter().close();
105             _activeMetaData = null;
106         } catch (IOException JavaDoc e) {
107             throw new DataSetException(e);
108         }
109     }
110
111     public void row(Object JavaDoc[] values) throws DataSetException {
112         try {
113
114             Column[] columns = _activeMetaData.getColumns();
115             for (int i = 0; i < columns.length; i++) {
116                 String JavaDoc columnName = columns[i].getColumnName();
117                 Object JavaDoc value = values[i];
118
119                 // null
120
if (value == null) {
121                     getWriter().write(NULL);
122                 }
123                 // none
124
else if (value == ITable.NO_VALUE) {
125                     getWriter().write(NONE);
126                 }
127                 // values
128
else {
129                     try {
130                         String JavaDoc stringValue = DataType.asString(value);
131                         final String JavaDoc quoted = quote(stringValue);
132                         getWriter().write(quoted);
133                     } catch (TypeCastException e) {
134                         throw new DataSetException("table=" +
135                                 _activeMetaData.getTableName() + ", row=" + i +
136                                 ", column=" + columnName +
137                                 ", value=" + value, e);
138                     }
139                 }
140                 if (i < columns.length - 1) getWriter().write(",");
141             }
142             getWriter().write(System.getProperty("line.separator"));
143         } catch (IOException JavaDoc e) {
144             throw new DataSetException(e);
145         }
146     }
147
148     private String JavaDoc quote(String JavaDoc stringValue) {
149         return new StringBuffer JavaDoc(QUOTE).append(escape(stringValue)).append(QUOTE).toString();
150     }
151
152     protected static String JavaDoc escape(String JavaDoc stringValue) {
153         char [] array = stringValue.toCharArray();
154         testExport = QUOTE.toCharArray()[0];
155         final char escape = ESCAPE.toCharArray()[0];
156         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
157         for (int i = 0; i < array.length; i++) {
158             char c = array[i];
159             if (c == testExport || c == escape) {
160                 buffer.append('\\');
161             }
162             buffer.append(c);
163         }
164         return buffer.toString();
165     }
166
167     public Writer JavaDoc getWriter() {
168         return writer;
169     }
170
171     public void setWriter(Writer JavaDoc writer) {
172         this.writer = writer;
173     }
174
175     public String JavaDoc getTheDirectory() {
176         return theDirectory;
177     }
178
179     public void setTheDirectory(String JavaDoc theDirectory) {
180         this.theDirectory = theDirectory;
181     }
182
183     public static void write(IDataSet dataset, File JavaDoc dest) throws DataSetException {
184         CsvDataSetWriter writer = new CsvDataSetWriter(dest);
185         writer.write(dataset);
186     }
187
188     protected void finalize() throws Throwable JavaDoc {
189         if (getWriter() != null) {
190             getWriter().close();
191         }
192     }
193 }
194
Popular Tags