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: package org.dbunit.dataset.csv;
022:
023: import org.slf4j.Logger;
024: import org.slf4j.LoggerFactory;
025:
026: import java.io.IOException;
027: import java.net.URL;
028: import java.util.Iterator;
029: import java.util.List;
030:
031: import org.dbunit.dataset.Column;
032: import org.dbunit.dataset.DataSetException;
033: import org.dbunit.dataset.DefaultTableMetaData;
034: import org.dbunit.dataset.ITableMetaData;
035: import org.dbunit.dataset.datatype.DataType;
036: import org.dbunit.dataset.stream.DefaultConsumer;
037: import org.dbunit.dataset.stream.IDataSetConsumer;
038: import org.dbunit.dataset.stream.IDataSetProducer;
039:
040: /**
041: * A {@link IDataSetProducer Data Set Producer} that produces datasets from
042: * CVS files found at a base URL.
043: *
044: * Based HEAVILY on {@link org.dbunit.dataset.csv.CsvProducer}.
045: *
046: * @author Dion Gillard
047: * @author Federico Spinazzi
048: */
049: public class CsvURLProducer implements IDataSetProducer {
050:
051: /**
052: * Logger for this class
053: */
054: private static final Logger logger = LoggerFactory
055: .getLogger(CsvURLProducer.class);
056:
057: /** the default consumer - does nothing */
058: private static final IDataSetConsumer EMPTY_CONSUMER = new DefaultConsumer();
059:
060: /**
061: * the consumer of the produced datasets, by default a
062: * {@link DefaultConsumer}
063: */
064: private IDataSetConsumer _consumer = EMPTY_CONSUMER;
065:
066: /** the base url to retrieve data from */
067: private URL base;
068:
069: /** the offset from the base url where the list of tables can be found */
070: private String tableList;
071:
072: /**
073: * Create a Csv Data Set Producer which uses the base URL to retrieve
074: * a list of tables and the data.
075: * @param base the URL where the tableList and data can be found.
076: * @param tableList the relative location of the list of tables.
077: */
078: public CsvURLProducer(URL base, String tableList) {
079: this .base = base;
080: this .tableList = tableList;
081: }
082:
083: /*
084: * @see IDataSetProducer#setConsumer(org.dbunit.dataset.stream.IDataSetConsumer)
085: */
086: public void setConsumer(IDataSetConsumer consumer)
087: throws DataSetException {
088: logger.debug("setConsumer(consumer) - start");
089:
090: _consumer = consumer;
091: }
092:
093: /*
094: * @see IDataSetProducer#produce()
095: */
096: public void produce() throws DataSetException {
097: logger.debug("produce() - start");
098:
099: _consumer.startDataSet();
100: try {
101: List tableSpecs = CsvProducer.getTables(base, tableList);
102: for (Iterator tableIter = tableSpecs.iterator(); tableIter
103: .hasNext();) {
104: String table = (String) tableIter.next();
105: try {
106: produceFromURL(new URL(base, table + ".csv"));
107: } catch (CsvParserException e) {
108: logger.error("produce()", e);
109:
110: throw new DataSetException(
111: "error producing dataset for table '"
112: + table + "'", e);
113: }
114:
115: }
116: _consumer.endDataSet();
117: } catch (IOException e) {
118: logger.error("produce()", e);
119:
120: throw new DataSetException("error getting list of tables",
121: e);
122: }
123: }
124:
125: /**
126: * Produce a dataset from a URL.
127: * The URL is assumed to contain data in CSV format.
128: * @param url a url containing CSV data.
129: */
130: private void produceFromURL(URL url) throws DataSetException {
131: logger.debug("produceFromURL(url=" + url + ") - start");
132:
133: try {
134: CsvParser parser = new CsvParserImpl();
135: List readData = parser.parse(url);
136: List readColumns = (List) readData.get(0);
137: Column[] columns = new Column[readColumns.size()];
138:
139: for (int i = 0; i < readColumns.size(); i++) {
140: columns[i] = new Column((String) readColumns.get(i),
141: DataType.UNKNOWN);
142: }
143:
144: String tableName = url.getFile();
145: tableName = tableName.substring(
146: tableName.lastIndexOf("/") + 1, tableName
147: .indexOf(".csv"));
148: ITableMetaData metaData = new DefaultTableMetaData(
149: tableName, columns);
150: _consumer.startTable(metaData);
151: for (int i = 1; i < readData.size(); i++) {
152: List rowList = (List) readData.get(i);
153: Object[] row = rowList.toArray();
154: for (int col = 0; col < row.length; col++) {
155: if (CsvDataSetWriter.NULL.equals(row[col])) {
156: row[col] = null;
157: }
158: }
159: _consumer.row(row);
160: }
161: _consumer.endTable();
162: } catch (CsvParserException e) {
163: logger.error("produceFromURL()", e);
164:
165: throw new DataSetException("error parsing CSV for URL: '"
166: + url + "'");
167: } catch (IOException e) {
168: logger.error("produceFromURL()", e);
169:
170: throw new DataSetException(
171: "I/O error parsing CSV for URL: '" + url + "'");
172: }
173: }
174: }
|