KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > ReplacementDataSet


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;
22
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 /**
27  * Decorator that replace configured values from the decorated dataset
28  * with replacement values.
29  *
30  * @author Manuel Laflamme
31  * @since Mar 17, 2003
32  * @version $Revision: 1.6 $
33  */

34 public class ReplacementDataSet extends AbstractDataSet
35 {
36     private final IDataSet _dataSet;
37     private final Map JavaDoc _objectMap;
38     private final Map JavaDoc _substringMap;
39     private String JavaDoc _startDelim;
40     private String JavaDoc _endDelim;
41
42
43     /**
44      * Create a new ReplacementDataSet object that decorates the specified dataset.
45      *
46      * @param dataSet the decorated table
47      */

48     public ReplacementDataSet(IDataSet dataSet)
49     {
50         _dataSet = dataSet;
51         _objectMap = new HashMap JavaDoc();
52         _substringMap = new HashMap JavaDoc();
53     }
54
55     /**
56      * Create a new ReplacementDataSet object that decorates the specified dataset.
57      *
58      * @param dataSet the decorated dataset
59      * @param objectMap the replacement objects mapping
60      * @param substringMap the replacement substrings mapping
61      */

62     public ReplacementDataSet(IDataSet dataSet, Map JavaDoc objectMap, Map JavaDoc substringMap)
63     {
64         _dataSet = dataSet;
65         _objectMap = objectMap == null ? new HashMap JavaDoc() : objectMap;
66         _substringMap = substringMap == null ? new HashMap JavaDoc() : substringMap;
67     }
68
69     /**
70      * Add a new Object replacement mapping.
71      *
72      * @param originalObject the object to replace
73      * @param replacementObject the replacement object
74      */

75     public void addReplacementObject(Object JavaDoc originalObject, Object JavaDoc replacementObject)
76     {
77         _objectMap.put(originalObject, replacementObject);
78     }
79
80     /**
81      * Add a new substring replacement mapping.
82      *
83      * @param originalSubstring the substring to replace
84      * @param replacementSubstring the replacement substring
85      */

86     public void addReplacementSubstring(String JavaDoc originalSubstring,
87             String JavaDoc replacementSubstring)
88     {
89         if (originalSubstring == null || replacementSubstring == null)
90         {
91             throw new NullPointerException JavaDoc();
92         }
93
94         _substringMap.put(originalSubstring, replacementSubstring);
95     }
96
97     /**
98      * Sets substring delimiters.
99      */

100     public void setSubstringDelimiters(String JavaDoc startDelimiter, String JavaDoc endDelimiter)
101     {
102         if (startDelimiter == null || endDelimiter == null)
103         {
104             throw new NullPointerException JavaDoc();
105         }
106
107         _startDelim = startDelimiter;
108         _endDelim = endDelimiter;
109     }
110
111     private ReplacementTable createReplacementTable(ITable table)
112     {
113         return new ReplacementTable(table, _objectMap, _substringMap,
114                 _startDelim, _endDelim);
115     }
116
117     ////////////////////////////////////////////////////////////////////////////
118
// AbstractDataSet class
119

120     protected ITableIterator createIterator(boolean reversed)
121             throws DataSetException
122     {
123         return new ReplacementIterator(reversed ?
124                 _dataSet.reverseIterator() : _dataSet.iterator());
125     }
126
127     ////////////////////////////////////////////////////////////////////////////
128
// IDataSet interface
129

130     public String JavaDoc[] getTableNames() throws DataSetException
131     {
132         return _dataSet.getTableNames();
133     }
134
135     public ITableMetaData getTableMetaData(String JavaDoc tableName)
136             throws DataSetException
137     {
138         return _dataSet.getTableMetaData(tableName);
139     }
140
141     public ITable getTable(String JavaDoc tableName) throws DataSetException
142     {
143         return createReplacementTable(_dataSet.getTable(tableName));
144     }
145
146     ////////////////////////////////////////////////////////////////////////////
147
// ReplacementIterator class
148

149     private class ReplacementIterator implements ITableIterator
150     {
151         private final ITableIterator _iterator;
152
153         public ReplacementIterator(ITableIterator iterator)
154         {
155             _iterator = iterator;
156         }
157
158         ////////////////////////////////////////////////////////////////////////
159
// ITableIterator interface
160

161         public boolean next() throws DataSetException
162         {
163             return _iterator.next();
164         }
165
166         public ITableMetaData getTableMetaData() throws DataSetException
167         {
168             return _iterator.getTableMetaData();
169         }
170
171         public ITable getTable() throws DataSetException
172         {
173             return createReplacementTable(_iterator.getTable());
174         }
175     }
176 }
177
Popular Tags