001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021: package org.dbunit.dataset.filter;
022:
023: import org.slf4j.Logger;
024: import org.slf4j.LoggerFactory;
025:
026: import org.dbunit.dataset.Column;
027: import org.dbunit.dataset.ITable;
028: import org.dbunit.dataset.CompositeTable;
029: import org.dbunit.dataset.FilteredTableMetaData;
030: import org.dbunit.dataset.DataSetException;
031:
032: /**
033: * Implementation of the IColumnFilter interface that exposes columns matching
034: * include patterns and not matching exclude patterns.
035: *
036: * @author Manuel Laflamme
037: * @since Apr 17, 2004
038: * @version $Revision: 554 $
039: */
040: public class DefaultColumnFilter implements IColumnFilter {
041:
042: /**
043: * Logger for this class
044: */
045: private static final Logger logger = LoggerFactory
046: .getLogger(DefaultColumnFilter.class);
047:
048: private final PatternMatcher _includeMatcher = new PatternMatcher();
049: private final PatternMatcher _excludeMatcher = new PatternMatcher();
050:
051: /**
052: * Add a new accepted column name pattern for all tables.
053: * The following wildcard characters are supported:
054: * '*' matches zero or more characters,
055: * '?' matches one character.
056: */
057: public void includeColumn(String columnPattern) {
058: logger.debug("includeColumn(columnPattern=" + columnPattern
059: + ") - start");
060:
061: _includeMatcher.addPattern(columnPattern);
062: }
063:
064: /**
065: * Add specified columns to accepted column name list.
066: */
067: public void includeColumns(Column[] columns) {
068: logger.debug("includeColumns(columns=" + columns + ") - start");
069:
070: for (int i = 0; i < columns.length; i++) {
071: _includeMatcher.addPattern(columns[i].getColumnName());
072: }
073: }
074:
075: /**
076: * Add a new refused column name pattern for all tables.
077: * The following wildcard characters are supported:
078: * '*' matches zero or more characters,
079: * '?' matches one character.
080: */
081: public void excludeColumn(String columnPattern) {
082: logger.debug("excludeColumn(columnPattern=" + columnPattern
083: + ") - start");
084:
085: _excludeMatcher.addPattern(columnPattern);
086: }
087:
088: /**
089: * Add specified columns to excluded column name list.
090: */
091: public void excludeColumns(Column[] columns) {
092: logger.debug("excludeColumns(columns=" + columns + ") - start");
093:
094: for (int i = 0; i < columns.length; i++) {
095: _excludeMatcher.addPattern(columns[i].getColumnName());
096: }
097: }
098:
099: /**
100: * Returns a table backed by the specified table that only exposes specified
101: * columns.
102: */
103: public static ITable includedColumnsTable(ITable table,
104: String[] columnNames) throws DataSetException {
105: logger.debug("includedColumnsTable(table=" + table
106: + ", columnNames=" + columnNames + ") - start");
107:
108: DefaultColumnFilter columnFilter = new DefaultColumnFilter();
109: for (int i = 0; i < columnNames.length; i++) {
110: String columnName = columnNames[i];
111: columnFilter.includeColumn(columnName);
112: }
113:
114: FilteredTableMetaData metaData = new FilteredTableMetaData(
115: table.getTableMetaData(), columnFilter);
116: return new CompositeTable(metaData, table);
117: }
118:
119: /**
120: * Returns a table backed by the specified table that only exposes specified
121: * columns.
122: */
123: public static ITable includedColumnsTable(ITable table,
124: Column[] columns) throws DataSetException {
125: logger.debug("includedColumnsTable(table=" + table
126: + ", columns=" + columns + ") - start");
127:
128: DefaultColumnFilter columnFilter = new DefaultColumnFilter();
129: columnFilter.includeColumns(columns);
130:
131: FilteredTableMetaData metaData = new FilteredTableMetaData(
132: table.getTableMetaData(), columnFilter);
133: return new CompositeTable(metaData, table);
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,
141: String[] columnNames) throws DataSetException {
142: logger.debug("excludedColumnsTable(table=" + table
143: + ", columnNames=" + columnNames + ") - start");
144:
145: DefaultColumnFilter columnFilter = new DefaultColumnFilter();
146: for (int i = 0; i < columnNames.length; i++) {
147: String columnName = columnNames[i];
148: columnFilter.excludeColumn(columnName);
149: }
150:
151: FilteredTableMetaData metaData = new FilteredTableMetaData(
152: table.getTableMetaData(), columnFilter);
153: return new CompositeTable(metaData, table);
154: }
155:
156: /**
157: * Returns a table backed by the specified table but with specified
158: * columns excluded.
159: */
160: public static ITable excludedColumnsTable(ITable table,
161: Column[] columns) throws DataSetException {
162: logger.debug("excludedColumnsTable(table=" + table
163: + ", columns=" + columns + ") - start");
164:
165: DefaultColumnFilter columnFilter = new DefaultColumnFilter();
166: columnFilter.excludeColumns(columns);
167:
168: FilteredTableMetaData metaData = new FilteredTableMetaData(
169: table.getTableMetaData(), columnFilter);
170: return new CompositeTable(metaData, table);
171: }
172:
173: ////////////////////////////////////////////////////////////////////////////
174: // IColumnFilter interface
175:
176: public boolean accept(String tableName, Column column) {
177: logger.debug("accept(tableName=" + tableName + ", column="
178: + column + ") - start");
179:
180: if (_includeMatcher.isEmpty()
181: || _includeMatcher.accept(column.getColumnName())) {
182: return !_excludeMatcher.accept(column.getColumnName());
183: }
184: return false;
185: }
186: }
|