CsvURLProducer.java

  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.csv;

  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;

  24. import java.io.IOException;
  25. import java.net.URL;
  26. import java.util.Iterator;
  27. import java.util.List;

  28. import org.dbunit.dataset.Column;
  29. import org.dbunit.dataset.DataSetException;
  30. import org.dbunit.dataset.DefaultTableMetaData;
  31. import org.dbunit.dataset.ITableMetaData;
  32. import org.dbunit.dataset.datatype.DataType;
  33. import org.dbunit.dataset.stream.DefaultConsumer;
  34. import org.dbunit.dataset.stream.IDataSetConsumer;
  35. import org.dbunit.dataset.stream.IDataSetProducer;

  36. /**
  37.  * A {@link IDataSetProducer Data Set Producer} that produces datasets from
  38.  * CVS files found at a base URL.
  39.  *
  40.  * Based HEAVILY on {@link org.dbunit.dataset.csv.CsvProducer}.
  41.  *  
  42.  * @author Dion Gillard
  43.  * @author Federico Spinazzi
  44.  * @author Last changed by: $Author$
  45.  * @version $Revision$ $Date$
  46.  * @since Sep 12, 2004 (pre 2.3)
  47.  */
  48. public class CsvURLProducer implements IDataSetProducer {

  49.     /**
  50.      * Logger for this class
  51.      */
  52.     private static final Logger logger = LoggerFactory.getLogger(CsvURLProducer.class);

  53.     /** the default consumer - does nothing */
  54.     private static final IDataSetConsumer EMPTY_CONSUMER = new DefaultConsumer();

  55.     /**
  56.      * the consumer of the produced datasets, by default a
  57.      * {@link DefaultConsumer}
  58.      */
  59.     private IDataSetConsumer _consumer = EMPTY_CONSUMER;
  60.    
  61.     /** the base url to retrieve data from */
  62.     private URL base;

  63.     /** the offset from the base url where the list of tables can be found */
  64.     private String tableList;
  65.    
  66.     /**
  67.      * Create a CSV Data Set Producer which uses the base URL to retrieve
  68.      * a list of tables and the data.
  69.      * @param base the URL where the tableList and data can be found.
  70.      * @param tableList the relative location of the list of tables.
  71.      */
  72.     public CsvURLProducer(URL base, String tableList)
  73.     {
  74.         this.base = base;
  75.         this.tableList = tableList;
  76.     }
  77.    
  78.     /*
  79.      * @see IDataSetProducer#setConsumer(org.dbunit.dataset.stream.IDataSetConsumer)
  80.      */
  81.     public void setConsumer(IDataSetConsumer consumer) throws DataSetException {
  82.         logger.debug("setConsumer(consumer) - start");

  83.         _consumer = consumer;
  84.     }

  85.     /*
  86.      * @see IDataSetProducer#produce()
  87.      */
  88.     public void produce() throws DataSetException {
  89.         logger.debug("produce() - start");

  90.         _consumer.startDataSet();
  91.         try {
  92.             List tableSpecs = CsvProducer.getTables(base, tableList);
  93.             for (Iterator tableIter = tableSpecs.iterator(); tableIter.hasNext();) {
  94.                 String table = (String) tableIter.next();
  95.                 try {
  96.                     produceFromURL(new URL(base, table + ".csv"));
  97.                 } catch (CsvParserException e) {
  98.                     throw new DataSetException("error producing dataset for table '" + table + "'", e);
  99.                 }

  100.             }
  101.             _consumer.endDataSet();
  102.         } catch (IOException e) {
  103.             throw new DataSetException("error getting list of tables", e);
  104.         }
  105.     }

  106.     /**
  107.      * Produce a dataset from a URL.
  108.      * The URL is assumed to contain data in CSV format.
  109.      * @param url a url containing CSV data.
  110.      */
  111.     private void produceFromURL(URL url) throws DataSetException {
  112.         logger.debug("produceFromURL(url=" + url + ") - start");

  113.         try {
  114.             CsvParser parser = new CsvParserImpl();
  115.             List readData = parser.parse(url);
  116.             List readColumns = (List) readData.get(0);
  117.             Column[] columns = new Column[readColumns.size()];

  118.             for (int i = 0; i < readColumns.size(); i++) {
  119.                 columns[i] = new Column((String) readColumns.get(i), DataType.UNKNOWN);
  120.             }

  121.             String tableName = url.getFile();
  122.             tableName = tableName.substring(tableName.lastIndexOf("/")+1, tableName.indexOf(".csv"));
  123.             ITableMetaData metaData = new DefaultTableMetaData(tableName, columns);
  124.             _consumer.startTable(metaData);
  125.             for (int i = 1 ; i < readData.size(); i++) {
  126.                 List rowList = (List)readData.get(i);
  127.                 Object[] row = rowList.toArray();
  128.                 for(int col = 0; col < row.length; col++) {
  129.                     if (CsvDataSetWriter.NULL.equals(row[col])) {
  130.                         row[col] = null;
  131.                     }
  132.                 }
  133.                 _consumer.row(row);
  134.             }
  135.             _consumer.endTable();
  136.         } catch (CsvParserException e) {
  137.             throw new DataSetException("error parsing CSV for URL: '" + url + "'", e);
  138.         } catch (IOException e) {
  139.             throw new DataSetException("I/O error parsing CSV for URL: '" + url + "'", e);
  140.         }
  141.     }
  142. }