001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021:
022: package org.dbunit.dataset.csv;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import java.io.BufferedReader;
028: import java.io.File;
029: import java.io.IOException;
030: import java.io.InputStream;
031: import java.io.InputStreamReader;
032: import java.net.URL;
033: import java.util.ArrayList;
034: import java.util.Iterator;
035: import java.util.List;
036:
037: import org.dbunit.dataset.Column;
038: import org.dbunit.dataset.DataSetException;
039: import org.dbunit.dataset.DefaultTableMetaData;
040: import org.dbunit.dataset.ITableMetaData;
041: import org.dbunit.dataset.csv.handlers.PipelineException;
042: import org.dbunit.dataset.datatype.DataType;
043: import org.dbunit.dataset.stream.DefaultConsumer;
044: import org.dbunit.dataset.stream.IDataSetConsumer;
045: import org.dbunit.dataset.stream.IDataSetProducer;
046:
047: /**
048: * @author Federico Spinazzi
049: * @since Sep 17, 2003
050: * @version $Revision: 558 $
051: */
052:
053: public class CsvProducer implements IDataSetProducer {
054:
055: /**
056: * Logger for this class
057: */
058: private static final Logger logger = LoggerFactory
059: .getLogger(CsvProducer.class);
060:
061: private static final IDataSetConsumer EMPTY_CONSUMER = new DefaultConsumer();
062: private IDataSetConsumer _consumer = EMPTY_CONSUMER;
063: private String _theDirectory;
064:
065: public CsvProducer(String theDirectory) {
066: _theDirectory = theDirectory;
067: }
068:
069: public CsvProducer(File theDirectory) {
070: _theDirectory = theDirectory.getAbsolutePath();
071: }
072:
073: public void setConsumer(IDataSetConsumer consumer)
074: throws DataSetException {
075: logger.debug("setConsumer(consumer) - start");
076:
077: _consumer = consumer;
078: }
079:
080: public void produce() throws DataSetException {
081: logger.debug("produce() - start");
082:
083: File dir = new File(_theDirectory);
084:
085: if (!dir.isDirectory()) {
086: throw new DataSetException("'" + _theDirectory
087: + "' should be a directory");
088: }
089:
090: _consumer.startDataSet();
091: try {
092: List tableSpecs = CsvProducer.getTables(dir.toURL(),
093: "table-ordering.txt");
094: for (Iterator tableIter = tableSpecs.iterator(); tableIter
095: .hasNext();) {
096: String table = (String) tableIter.next();
097: try {
098: produceFromFile(new File(dir, table + ".csv"));
099: } catch (CsvParserException e) {
100: logger.error("produce()", e);
101:
102: throw new DataSetException(
103: "error producing dataset for table '"
104: + table + "'", e);
105: } catch (DataSetException e) {
106: logger.error("produce()", e);
107:
108: throw new DataSetException(
109: "error producing dataset for table '"
110: + table + "'", e);
111: }
112:
113: }
114: _consumer.endDataSet();
115: } catch (IOException e) {
116: logger.error("produce()", e);
117:
118: throw new DataSetException("error getting list of tables",
119: e);
120: }
121: }
122:
123: private void produceFromFile(File theDataFile)
124: throws DataSetException, CsvParserException {
125: logger.debug("produceFromFile(theDataFile=" + theDataFile
126: + ") - start");
127:
128: try {
129: CsvParser parser = new CsvParserImpl();
130: List readData = parser.parse(theDataFile);
131: List readColumns = ((List) readData.get(0));
132: Column[] columns = new Column[readColumns.size()];
133:
134: for (int i = 0; i < readColumns.size(); i++) {
135: columns[i] = new Column((String) readColumns.get(i),
136: DataType.UNKNOWN);
137: }
138:
139: String tableName = theDataFile.getName().substring(0,
140: theDataFile.getName().indexOf(".csv"));
141: ITableMetaData metaData = new DefaultTableMetaData(
142: tableName, columns);
143: _consumer.startTable(metaData);
144: for (int i = 1; i < readData.size(); i++) {
145: List rowList = (List) readData.get(i);
146: Object[] row = rowList.toArray();
147: for (int col = 0; col < row.length; col++) {
148: row[col] = row[col].equals(CsvDataSetWriter.NULL) ? null
149: : row[col];
150: }
151: _consumer.row(row);
152: }
153: _consumer.endTable();
154: } catch (PipelineException e) {
155: logger.error("produceFromFile()", e);
156:
157: throw new DataSetException(e);
158: } catch (IllegalInputCharacterException e) {
159: logger.error("produceFromFile()", e);
160:
161: throw new DataSetException(e);
162: } catch (IOException e) {
163: logger.error("produceFromFile()", e);
164:
165: throw new DataSetException(e);
166: }
167: }
168:
169: /**
170: * Get a list of tables that this producer will create
171: * @return a list of Strings, where each item is a CSV file relative to the base URL
172: * @throws IOException when IO on the base URL has issues.
173: */
174: public static List getTables(URL base, String tableList)
175: throws IOException {
176: logger.debug("getTables(base=" + base + ", tableList="
177: + tableList + ") - start");
178:
179: List orderedNames = new ArrayList();
180: InputStream tableListStream = new URL(base, tableList)
181: .openStream();
182: BufferedReader reader = new BufferedReader(
183: new InputStreamReader(tableListStream));
184: String line = null;
185: while ((line = reader.readLine()) != null) {
186: String table = line.trim();
187: if (table.length() > 0) {
188: orderedNames.add(table);
189: }
190: }
191: reader.close();
192: return orderedNames;
193: }
194:
195: }
|