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.dbunit.dataset.Column;
24  import org.dbunit.dataset.ColumnFilterTable;
25  import org.dbunit.dataset.DataSetException;
26  import org.dbunit.dataset.ITable;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * Implementation of the IColumnFilter interface that exposes columns matching
32   * include patterns and not matching exclude patterns.
33   *
34   * @author Manuel Laflamme
35   * @since Apr 17, 2004
36   * @version $Revision$
37   */
38  public class DefaultColumnFilter implements IColumnFilter
39  {
40  
41      /**
42       * Logger for this class
43       */
44      private static final Logger logger = LoggerFactory.getLogger(DefaultColumnFilter.class);
45  
46      private final PatternMatcher _includeMatcher = new PatternMatcher();
47      private final PatternMatcher _excludeMatcher = new PatternMatcher();
48  
49      /**
50       * Add a new accepted column name pattern for all tables.
51       * The following wildcard characters are supported:
52       * '*' matches zero or more characters,
53       * '?' matches one character.
54       * @param columnPattern The column pattern to be supported
55       */
56      public void includeColumn(String columnPattern)
57      {
58          logger.debug("includeColumn(columnPattern={}) - start", columnPattern);
59  
60          _includeMatcher.addPattern(columnPattern);
61      }
62  
63      /**
64       * Add specified columns to accepted column name list.
65       */
66      public void includeColumns(Column[] columns)
67      {
68          logger.debug("includeColumns(columns={}) - start", columns);
69  
70          for (int i = 0; i < columns.length; i++)
71          {
72              _includeMatcher.addPattern(columns[i].getColumnName());
73          }
74      }
75  
76      /**
77       * Add a new refused column name pattern for all tables.
78       * The following wildcard characters are supported:
79       * '*' matches zero or more characters,
80       * '?' matches one character.
81       */
82      public void excludeColumn(String columnPattern)
83      {
84          logger.debug("excludeColumn(columnPattern={}) - start", columnPattern);
85  
86          _excludeMatcher.addPattern(columnPattern);
87      }
88  
89      /**
90       * Add specified columns to excluded column name list.
91       */
92      public void excludeColumns(Column[] columns)
93      {
94          logger.debug("excludeColumns(columns={} - start", columns);
95  
96          for (int i = 0; i < columns.length; i++)
97          {
98              _excludeMatcher.addPattern(columns[i].getColumnName());
99          }
100     }
101 
102     /**
103      * Returns a table backed by the specified table that only exposes specified
104      * columns.
105      */
106     public static ITable includedColumnsTable(ITable table, String[] columnNames)
107             throws DataSetException
108     {
109         logger.debug("includedColumnsTable(table={}, columnNames={}) - start", table, columnNames);
110 
111         DefaultColumnFilter columnFilter = new DefaultColumnFilter();
112         for (int i = 0; i < columnNames.length; i++)
113         {
114             String columnName = columnNames[i];
115             columnFilter.includeColumn(columnName);
116         }
117 
118         return new ColumnFilterTable(table, columnFilter);
119     }
120 
121     /**
122      * Returns a table backed by the specified table that only exposes specified
123      * columns.
124      */
125     public static ITable includedColumnsTable(ITable table, Column[] columns)
126             throws DataSetException
127     {
128         logger.debug("includedColumnsTable(table={}, columns={}) - start", table, columns);
129 
130         DefaultColumnFilter columnFilter = new DefaultColumnFilter();
131         columnFilter.includeColumns(columns);
132 
133         return new ColumnFilterTable(table, columnFilter);
134     }
135 
136     /**
137      * Returns a table backed by the specified table but with specified
138      * columns excluded.
139      */
140     public static ITable excludedColumnsTable(ITable table, String[] columnNames)
141             throws DataSetException
142     {
143         logger.debug("excludedColumnsTable(table={}, columnNames={}) - start", table, columnNames);
144 
145         DefaultColumnFilter columnFilter = new DefaultColumnFilter();
146         for (int i = 0; i < columnNames.length; i++)
147         {
148             String columnName = columnNames[i];
149             columnFilter.excludeColumn(columnName);
150         }
151 
152         return new ColumnFilterTable(table, columnFilter);
153     }
154 
155     /**
156      * Returns a table backed by the specified table but with specified
157      * columns excluded.
158      */
159     public static ITable excludedColumnsTable(ITable table, Column[] columns)
160             throws DataSetException
161     {
162         logger.debug("excludedColumnsTable(table={}, columns={}) - start", table, columns);
163 
164         DefaultColumnFilter columnFilter = new DefaultColumnFilter();
165         columnFilter.excludeColumns(columns);
166 
167         return new ColumnFilterTable(table, columnFilter);
168     }
169 
170     ////////////////////////////////////////////////////////////////////////////
171     // IColumnFilter interface
172 
173     public boolean accept(String tableName, Column column)
174     {
175         logger.debug("accept(tableName={}, column={}) - start", tableName, column);
176 
177         if (_includeMatcher.isEmpty() ||
178                 _includeMatcher.accept(column.getColumnName()))
179         {
180             return !_excludeMatcher.accept(column.getColumnName());
181         }
182         return false;
183     }
184     
185     
186     public String toString()
187     {
188         StringBuffer sb = new StringBuffer();
189         sb.append(getClass().getName()).append("[");
190         sb.append("_includeMatcher=").append(_includeMatcher);
191         sb.append(", _excludeMatcher=").append(_excludeMatcher);
192         sb.append("]");
193         return sb.toString();
194     }
195 }