AbstractTableFilter.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.filter;

  22. import java.util.ArrayList;
  23. import java.util.List;

  24. import org.dbunit.dataset.DataSetException;
  25. import org.dbunit.dataset.IDataSet;
  26. import org.dbunit.dataset.ITable;
  27. import org.dbunit.dataset.ITableIterator;
  28. import org.dbunit.dataset.ITableMetaData;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;

  31. /**
  32.  * This class provides a skeletal implementation of the {@link ITableFilter}
  33.  * interface to minimize the effort required to implement a filter. Subclasses
  34.  * are only required to implement the {@link #isValidName} method.
  35.  *
  36.  * @author Manuel Laflamme
  37.  * @author Last changed by: $Author$
  38.  * @version $Revision$ $Date$
  39.  * @since 2.2.0
  40.  */
  41. public abstract class AbstractTableFilter implements ITableFilter
  42. {

  43.     /**
  44.      * Logger for this class
  45.      */
  46.     private static final Logger logger = LoggerFactory.getLogger(AbstractTableFilter.class);

  47.     /**
  48.      * Returns <code>true</code> if specified table is allowed by this filter.
  49.      * This legacy method, now replaced by accept, still exist for compatibily
  50.      * with older environment
  51.      */
  52.     public abstract boolean isValidName(String tableName) throws DataSetException;

  53.     ////////////////////////////////////////////////////////////////////////////
  54.     // ITableFilter interface

  55.     public boolean accept(String tableName) throws DataSetException
  56.     {
  57.         logger.debug("accept(tableName={}) - start", tableName);

  58.         return isValidName(tableName);
  59.     }

  60.     public String[] getTableNames(IDataSet dataSet) throws DataSetException
  61.     {
  62.         logger.debug("getTableNames(dataSet={}) - start", dataSet);

  63.         String[] tableNames = dataSet.getTableNames();
  64.         List nameList = new ArrayList();
  65.         for (int i = 0; i < tableNames.length; i++)
  66.         {
  67.             String tableName = tableNames[i];
  68.             if (accept(tableName))
  69.             {
  70.                 nameList.add(tableName);
  71.             }
  72.         }
  73.         return (String[])nameList.toArray(new String[0]);
  74.     }

  75.     public ITableIterator iterator(IDataSet dataSet, boolean reversed)
  76.             throws DataSetException
  77.     {
  78.         logger.debug("iterator(dataSet={}, reversed={}) - start", dataSet, String.valueOf(reversed));

  79.         return new FilterIterator(reversed ?
  80.                 dataSet.reverseIterator() : dataSet.iterator());
  81.     }

  82.     ////////////////////////////////////////////////////////////////////////////
  83.     // FilterIterator class

  84.     private class FilterIterator implements ITableIterator
  85.     {

  86.         /**
  87.          * Logger for this class
  88.          */
  89.         private final Logger logger = LoggerFactory.getLogger(FilterIterator.class);

  90.         private final ITableIterator _iterator;

  91.         public FilterIterator(ITableIterator iterator)
  92.         {
  93.             _iterator = iterator;
  94.         }

  95.         ////////////////////////////////////////////////////////////////////////////
  96.         // ITableIterator interface

  97.         public boolean next() throws DataSetException
  98.         {
  99.             logger.debug("next() - start");

  100.             while(_iterator.next())
  101.             {
  102.                 if (accept(_iterator.getTableMetaData().getTableName()))
  103.                 {
  104.                     return true;
  105.                 }
  106.             }
  107.             return false;
  108.         }

  109.         public ITableMetaData getTableMetaData() throws DataSetException
  110.         {
  111.             logger.debug("getTableMetaData() - start");

  112.             return _iterator.getTableMetaData();
  113.         }

  114.         public ITable getTable() throws DataSetException
  115.         {
  116.             logger.debug("getTable() - start");

  117.             return _iterator.getTable();
  118.         }
  119.     }
  120. }