1 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 37 public class DefaultColumnFilter implements IColumnFilter 38 { 39 private final PatternMatcher _includeMatcher = new PatternMatcher(); 40 private final PatternMatcher _excludeMatcher = new PatternMatcher(); 41 42 48 public void includeColumn(String columnPattern) 49 { 50 _includeMatcher.addPattern(columnPattern); 51 } 52 53 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 70 public void excludeColumn(String columnPattern) 71 { 72 _excludeMatcher.addPattern(columnPattern); 73 } 74 75 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 90 public static ITable includedColumnsTable(ITable table, String [] columnNames) 91 throws DataSetException 92 { 93 DefaultColumnFilter columnFilter = new DefaultColumnFilter(); 94 for (int i = 0; i < columnNames.length; i++) 95 { 96 String 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 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 124 public static ITable excludedColumnsTable(ITable table, String [] columnNames) 125 throws DataSetException 126 { 127 DefaultColumnFilter columnFilter = new DefaultColumnFilter(); 128 for (int i = 0; i < columnNames.length; i++) 129 { 130 String 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 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 157 public boolean accept(String 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 |