KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 package org.dbunit.dataset.csv;
23
24 import org.dbunit.dataset.Column;
25 import org.dbunit.dataset.DataSetException;
26 import org.dbunit.dataset.DefaultTableMetaData;
27 import org.dbunit.dataset.ITableMetaData;
28 import org.dbunit.dataset.csv.handlers.PipelineException;
29 import org.dbunit.dataset.datatype.DataType;
30 import org.dbunit.dataset.stream.DefaultConsumer;
31 import org.dbunit.dataset.stream.IDataSetConsumer;
32 import org.dbunit.dataset.stream.IDataSetProducer;
33
34 import java.io.File JavaDoc;
35 import java.io.FilenameFilter JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.util.List JavaDoc;
38
39 /**
40  * @author Federico Spinazzi
41  * @since Sep 17, 2003
42  * @version $Revision: 1.3 $
43  */

44
45 public class CsvProducer implements IDataSetProducer {
46
47     private static final IDataSetConsumer EMPTY_CONSUMER = new DefaultConsumer();
48     private IDataSetConsumer _consumer = EMPTY_CONSUMER;
49     private String JavaDoc _theDirectory;
50
51     public CsvProducer(String JavaDoc theDirectory) {
52         _theDirectory = theDirectory;
53     }
54
55     public CsvProducer(File JavaDoc theDirectory) {
56         _theDirectory = theDirectory.getAbsolutePath();
57     }
58
59     public void setConsumer(IDataSetConsumer consumer) throws DataSetException {
60         _consumer = consumer;
61     }
62
63     public void produce() throws DataSetException {
64
65         File JavaDoc dir = new File JavaDoc(_theDirectory);
66
67         if (!dir.isDirectory()) {
68             throw new DataSetException("'" + _theDirectory + "' should be a directory");
69         }
70
71         // @todo: move in a class by itself, somewhere
72
FilenameFilter JavaDoc filter = new FilenameFilter JavaDoc() {
73             public boolean accept(File JavaDoc dir, String JavaDoc name) {
74                 return name.endsWith(".csv") && !dir.isFile();
75             }
76         };
77
78         _consumer.startDataSet();
79
80         File JavaDoc[] children = dir.listFiles(filter);
81         for (int i = 0; i < children.length; i++) {
82             try {
83                 produceFromFile(children[i]);
84             } catch (CsvParserException e) {
85                 throw new DataSetException(e);
86             }
87         }
88
89         _consumer.endDataSet();
90
91     }
92
93     private void produceFromFile(File JavaDoc theDataFile) throws DataSetException, CsvParserException {
94         try {
95             CsvParser parser = new CsvParserImpl();
96             List JavaDoc readData = parser.parse(theDataFile);
97             List JavaDoc readColumns = ((List JavaDoc) readData.get(0));
98             Column[] columns = new Column[readColumns.size()];
99
100             for (int i = 0; i < readColumns.size(); i++) {
101                 columns[i] = new Column((String JavaDoc) readColumns.get(i), DataType.UNKNOWN);
102             }
103
104             String JavaDoc tableName = theDataFile.getName().substring(0, theDataFile.getName().indexOf(".csv"));
105             ITableMetaData metaData = new DefaultTableMetaData(tableName, columns);
106             _consumer.startTable(metaData);
107             for (int i = 1 ; i < readData.size(); i++) {
108                 List JavaDoc rowList = (List JavaDoc)readData.get(i);
109                 _consumer.row(rowList.toArray());
110             }
111             _consumer.endTable();
112         } catch (PipelineException e) {
113             throw new DataSetException(e);
114         } catch (IllegalInputCharacterException e) {
115             throw new DataSetException(e);
116         } catch (IOException JavaDoc e) {
117             throw new DataSetException(e);
118         }
119     }
120
121 }
122
Popular Tags