View Javadoc
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  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.dbunit.dataset.DataSetException;
27  import org.dbunit.dataset.IDataSet;
28  import org.dbunit.dataset.ITable;
29  import org.dbunit.dataset.ITableIterator;
30  import org.dbunit.dataset.ITableMetaData;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   * This class provides a skeletal implementation of the {@link ITableFilter}
36   * interface to minimize the effort required to implement a filter. Subclasses
37   * are only required to implement the {@link #isValidName} method.
38   *
39   * @author Manuel Laflamme
40   * @author Last changed by: $Author$
41   * @version $Revision$ $Date$
42   * @since 2.2.0
43   */
44  public abstract class AbstractTableFilter implements ITableFilter
45  {
46  
47      /**
48       * Logger for this class
49       */
50      private static final Logger logger = LoggerFactory.getLogger(AbstractTableFilter.class);
51  
52      /**
53       * Returns <code>true</code> if specified table is allowed by this filter.
54       * This legacy method, now replaced by accept, still exist for compatibily
55       * with older environment
56       */
57      public abstract boolean isValidName(String tableName) throws DataSetException;
58  
59      ////////////////////////////////////////////////////////////////////////////
60      // ITableFilter interface
61  
62      public boolean accept(String tableName) throws DataSetException
63      {
64          logger.debug("accept(tableName={}) - start", tableName);
65  
66          return isValidName(tableName);
67      }
68  
69      public String[] getTableNames(IDataSet dataSet) throws DataSetException
70      {
71          logger.debug("getTableNames(dataSet={}) - start", dataSet);
72  
73          String[] tableNames = dataSet.getTableNames();
74          List nameList = new ArrayList();
75          for (int i = 0; i < tableNames.length; i++)
76          {
77              String tableName = tableNames[i];
78              if (accept(tableName))
79              {
80                  nameList.add(tableName);
81              }
82          }
83          return (String[])nameList.toArray(new String[0]);
84      }
85  
86      public ITableIterator iterator(IDataSet dataSet, boolean reversed)
87              throws DataSetException
88      {
89          logger.debug("iterator(dataSet={}, reversed={}) - start", dataSet, String.valueOf(reversed));
90  
91          return new FilterIterator(reversed ?
92                  dataSet.reverseIterator() : dataSet.iterator());
93      }
94  
95      ////////////////////////////////////////////////////////////////////////////
96      // FilterIterator class
97  
98      private class FilterIterator implements ITableIterator
99      {
100 
101         /**
102          * Logger for this class
103          */
104         private final Logger logger = LoggerFactory.getLogger(FilterIterator.class);
105 
106         private final ITableIterator _iterator;
107 
108         public FilterIterator(ITableIterator iterator)
109         {
110             _iterator = iterator;
111         }
112 
113         ////////////////////////////////////////////////////////////////////////////
114         // ITableIterator interface
115 
116         public boolean next() throws DataSetException
117         {
118             logger.debug("next() - start");
119 
120             while(_iterator.next())
121             {
122                 if (accept(_iterator.getTableMetaData().getTableName()))
123                 {
124                     return true;
125                 }
126             }
127             return false;
128         }
129 
130         public ITableMetaData getTableMetaData() throws DataSetException
131         {
132             logger.debug("getTableMetaData() - start");
133 
134             return _iterator.getTableMetaData();
135         }
136 
137         public ITable getTable() throws DataSetException
138         {
139             logger.debug("getTable() - start");
140 
141             return _iterator.getTable();
142         }
143     }
144 }