KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > filter > DefaultColumnFilter


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.ITable;
25 import org.dbunit.dataset.CompositeTable;
26 import org.dbunit.dataset.FilteredTableMetaData;
27 import org.dbunit.dataset.DataSetException;
28
29 /**
30  * Implementation of the IColumnFilter interface that exposes columns matching
31  * include patterns and not matching exclude patterns.
32  *
33  * @author Manuel Laflamme
34  * @since Apr 17, 2004
35  * @version $Revision: 1.4 $
36  */

37 public class DefaultColumnFilter implements IColumnFilter
38 {
39     private final PatternMatcher _includeMatcher = new PatternMatcher();
40     private final PatternMatcher _excludeMatcher = new PatternMatcher();
41
42     /**
43      * Add a new accepted column name pattern for all tables.
44      * The following wildcard characters are supported:
45      * '*' matches zero or more characters,
46      * '?' matches one character.
47      */

48     public void includeColumn(String JavaDoc columnPattern)
49     {
50         _includeMatcher.addPattern(columnPattern);
51     }
52
53     /**
54      * Add specified columns to accepted column name list.
55      */

56     public void includeColumns(Column[] columns)
57     {
58         for (int i = 0; i < columns.length; i++)
59         {
60             _includeMatcher.addPattern(columns[i].getColumnName());
61         }
62     }
63
64     /**
65      * Add a new refused column name pattern for all tables.
66      * The following wildcard characters are supported:
67      * '*' matches zero or more characters,
68      * '?' matches one character.
69      */

70     public void excludeColumn(String JavaDoc columnPattern)
71     {
72         _excludeMatcher.addPattern(columnPattern);
73     }
74
75     /**
76      * Add specified columns to excluded column name list.
77      */

78     public void excludeColumns(Column[] columns)
79     {
80         for (int i = 0; i < columns.length; i++)
81         {
82             _excludeMatcher.addPattern(columns[i].getColumnName());
83         }
84     }
85
86     /**
87      * Returns a table backed by the specified table that only exposes specified
88      * columns.
89      */

90     public static ITable includedColumnsTable(ITable table, String JavaDoc[] columnNames)
91             throws DataSetException
92     {
93         DefaultColumnFilter columnFilter = new DefaultColumnFilter();
94         for (int i = 0; i < columnNames.length; i++)
95         {
96             String JavaDoc columnName = columnNames[i];
97             columnFilter.includeColumn(columnName);
98         }
99
100         FilteredTableMetaData metaData = new FilteredTableMetaData(
101                 table.getTableMetaData(), columnFilter);
102         return new CompositeTable(metaData, table);
103     }
104
105     /**
106      * Returns a table backed by the specified table that only exposes specified
107      * columns.
108      */

109     public static ITable includedColumnsTable(ITable table, Column[] columns)
110             throws DataSetException
111     {
112         DefaultColumnFilter columnFilter = new DefaultColumnFilter();
113         columnFilter.includeColumns(columns);
114
115         FilteredTableMetaData metaData = new FilteredTableMetaData(
116                 table.getTableMetaData(), columnFilter);
117         return new CompositeTable(metaData, table);
118     }
119
120     /**
121      * Returns a table backed by the specified table but with specified
122      * columns excluded.
123      */

124     public static ITable excludedColumnsTable(ITable table, String JavaDoc[] columnNames)
125             throws DataSetException
126     {
127         DefaultColumnFilter columnFilter = new DefaultColumnFilter();
128         for (int i = 0; i < columnNames.length; i++)
129         {
130             String JavaDoc columnName = columnNames[i];
131             columnFilter.excludeColumn(columnName);
132         }
133
134         FilteredTableMetaData metaData = new FilteredTableMetaData(
135                 table.getTableMetaData(), columnFilter);
136         return new CompositeTable(metaData, table);
137     }
138
139     /**
140      * Returns a table backed by the specified table but with specified
141      * columns excluded.
142      */

143     public static ITable excludedColumnsTable(ITable table, Column[] columns)
144             throws DataSetException
145     {
146         DefaultColumnFilter columnFilter = new DefaultColumnFilter();
147         columnFilter.excludeColumns(columns);
148
149         FilteredTableMetaData metaData = new FilteredTableMetaData(
150                 table.getTableMetaData(), columnFilter);
151         return new CompositeTable(metaData, table);
152     }
153
154     ////////////////////////////////////////////////////////////////////////////
155
// IColumnFilter interface
156

157     public boolean accept(String JavaDoc tableName, Column column)
158     {
159         if (_includeMatcher.isEmpty() ||
160                 _includeMatcher.accept(column.getColumnName()))
161         {
162             return !_excludeMatcher.accept(column.getColumnName());
163         }
164         return false;
165     }
166 }
167
Popular Tags