DefaultColumnFilter.java

  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. import org.dbunit.dataset.Column;
  23. import org.dbunit.dataset.ColumnFilterTable;
  24. import org.dbunit.dataset.DataSetException;
  25. import org.dbunit.dataset.ITable;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;

  28. /**
  29.  * Implementation of the IColumnFilter interface that exposes columns matching
  30.  * include patterns and not matching exclude patterns.
  31.  *
  32.  * @author Manuel Laflamme
  33.  * @since Apr 17, 2004
  34.  * @version $Revision$
  35.  */
  36. public class DefaultColumnFilter implements IColumnFilter
  37. {

  38.     /**
  39.      * Logger for this class
  40.      */
  41.     private static final Logger logger = LoggerFactory.getLogger(DefaultColumnFilter.class);

  42.     private final PatternMatcher _includeMatcher = new PatternMatcher();
  43.     private final PatternMatcher _excludeMatcher = new PatternMatcher();

  44.     /**
  45.      * Add a new accepted column name pattern for all tables.
  46.      * The following wildcard characters are supported:
  47.      * '*' matches zero or more characters,
  48.      * '?' matches one character.
  49.      * @param columnPattern The column pattern to be supported
  50.      */
  51.     public void includeColumn(String columnPattern)
  52.     {
  53.         logger.debug("includeColumn(columnPattern={}) - start", columnPattern);

  54.         _includeMatcher.addPattern(columnPattern);
  55.     }

  56.     /**
  57.      * Add specified columns to accepted column name list.
  58.      */
  59.     public void includeColumns(Column[] columns)
  60.     {
  61.         logger.debug("includeColumns(columns={}) - start", columns);

  62.         for (int i = 0; i < columns.length; i++)
  63.         {
  64.             _includeMatcher.addPattern(columns[i].getColumnName());
  65.         }
  66.     }

  67.     /**
  68.      * Add a new refused column name pattern for all tables.
  69.      * The following wildcard characters are supported:
  70.      * '*' matches zero or more characters,
  71.      * '?' matches one character.
  72.      */
  73.     public void excludeColumn(String columnPattern)
  74.     {
  75.         logger.debug("excludeColumn(columnPattern={}) - start", columnPattern);

  76.         _excludeMatcher.addPattern(columnPattern);
  77.     }

  78.     /**
  79.      * Add specified columns to excluded column name list.
  80.      */
  81.     public void excludeColumns(Column[] columns)
  82.     {
  83.         logger.debug("excludeColumns(columns={} - start", columns);

  84.         for (int i = 0; i < columns.length; i++)
  85.         {
  86.             _excludeMatcher.addPattern(columns[i].getColumnName());
  87.         }
  88.     }

  89.     /**
  90.      * Returns a table backed by the specified table that only exposes specified
  91.      * columns.
  92.      */
  93.     public static ITable includedColumnsTable(ITable table, String[] columnNames)
  94.             throws DataSetException
  95.     {
  96.         logger.debug("includedColumnsTable(table={}, columnNames={}) - start", table, columnNames);

  97.         DefaultColumnFilter columnFilter = new DefaultColumnFilter();
  98.         for (int i = 0; i < columnNames.length; i++)
  99.         {
  100.             String columnName = columnNames[i];
  101.             columnFilter.includeColumn(columnName);
  102.         }

  103.         return new ColumnFilterTable(table, columnFilter);
  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.         logger.debug("includedColumnsTable(table={}, columns={}) - start", table, columns);

  113.         DefaultColumnFilter columnFilter = new DefaultColumnFilter();
  114.         columnFilter.includeColumns(columns);

  115.         return new ColumnFilterTable(table, columnFilter);
  116.     }

  117.     /**
  118.      * Returns a table backed by the specified table but with specified
  119.      * columns excluded.
  120.      */
  121.     public static ITable excludedColumnsTable(ITable table, String[] columnNames)
  122.             throws DataSetException
  123.     {
  124.         logger.debug("excludedColumnsTable(table={}, columnNames={}) - start", table, columnNames);

  125.         DefaultColumnFilter columnFilter = new DefaultColumnFilter();
  126.         for (int i = 0; i < columnNames.length; i++)
  127.         {
  128.             String columnName = columnNames[i];
  129.             columnFilter.excludeColumn(columnName);
  130.         }

  131.         return new ColumnFilterTable(table, columnFilter);
  132.     }

  133.     /**
  134.      * Returns a table backed by the specified table but with specified
  135.      * columns excluded.
  136.      */
  137.     public static ITable excludedColumnsTable(ITable table, Column[] columns)
  138.             throws DataSetException
  139.     {
  140.         logger.debug("excludedColumnsTable(table={}, columns={}) - start", table, columns);

  141.         DefaultColumnFilter columnFilter = new DefaultColumnFilter();
  142.         columnFilter.excludeColumns(columns);

  143.         return new ColumnFilterTable(table, columnFilter);
  144.     }

  145.     ////////////////////////////////////////////////////////////////////////////
  146.     // IColumnFilter interface

  147.     public boolean accept(String tableName, Column column)
  148.     {
  149.         logger.debug("accept(tableName={}, column={}) - start", tableName, column);

  150.         if (_includeMatcher.isEmpty() ||
  151.                 _includeMatcher.accept(column.getColumnName()))
  152.         {
  153.             return !_excludeMatcher.accept(column.getColumnName());
  154.         }
  155.         return false;
  156.     }
  157.    
  158.    
  159.     public String toString()
  160.     {
  161.         final StringBuilder sb = new StringBuilder();
  162.         sb.append(getClass().getName()).append("[");
  163.         sb.append("_includeMatcher=").append(_includeMatcher);
  164.         sb.append(", _excludeMatcher=").append(_excludeMatcher);
  165.         sb.append("]");
  166.         return sb.toString();
  167.     }
  168. }