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 org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  
27  /**
28   * This filter exposes only allowed tables from the filtered dataset. This
29   * implementation do not modify the original table sequence from the filtered
30   * dataset and support duplicate table names.
31   *
32   * @author Manuel Laflamme
33   * @since Mar 7, 2003
34   * @version $Revision$
35   */
36  public class IncludeTableFilter extends AbstractTableFilter implements ITableFilter
37  {
38  
39      /**
40       * Logger for this class
41       */
42      private static final Logger logger = LoggerFactory.getLogger(IncludeTableFilter.class);
43  
44      private final PatternMatcher _patternMatcher = new PatternMatcher();
45  
46      /**
47       * Create a new empty IncludeTableFilter. Use {@link #includeTable} to allow
48       * access to some tables.
49       */
50      public IncludeTableFilter()
51      {
52      }
53  
54      /**
55       * Create a new IncludeTableFilter which allow access to specified tables.
56       */
57      public IncludeTableFilter(String[] tableNames)
58      {
59          for (int i = 0; i < tableNames.length; i++)
60          {
61              String tableName = tableNames[i];
62              includeTable(tableName);
63          }
64      }
65  
66      /**
67       * Add a new accepted table name pattern.
68       * The following wildcard characters are supported:
69       * '*' matches zero or more characters,
70       * '?' matches one character.
71       */
72      public void includeTable(String patternName)
73      {
74          logger.debug("includeTable(patternName={} - start", patternName);
75  
76          _patternMatcher.addPattern(patternName);
77      }
78  
79      public boolean isEmpty()
80      {
81          logger.debug("isEmpty() - start");
82  
83          return _patternMatcher.isEmpty();
84      }
85  
86      ////////////////////////////////////////////////////////////////////////////
87      // ITableFilter interface
88  
89      public boolean isValidName(String tableName)
90      {
91          logger.debug("isValidName(tableName={}) - start", tableName);
92  
93          return _patternMatcher.accept(tableName);
94      }
95  }