KickJava   Java API By Example, From Geeks To Geeks.

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


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

35 public class ReplacementTable implements ITable
36 {
37     private final ITable _table;
38     private final Map JavaDoc _objectMap;
39     private final Map JavaDoc _substringMap;
40     private String JavaDoc _startDelim;
41     private String JavaDoc _endDelim;
42
43     /**
44      * Create a new ReplacementTable object that decorates the specified table.
45      *
46      * @param table the decorated table
47      */

48     public ReplacementTable(ITable table)
49     {
50         this(table, new HashMap JavaDoc(), new HashMap JavaDoc(), null, null);
51     }
52
53     public ReplacementTable(ITable table, Map JavaDoc objectMap, Map JavaDoc substringMap,
54             String JavaDoc startDelimiter, String JavaDoc endDelimiter)
55     {
56         _table = table;
57         _objectMap = objectMap;
58         _substringMap = substringMap;
59         _startDelim = startDelimiter;
60         _endDelim = endDelimiter;
61     }
62
63     /**
64      * Add a new Object replacement mapping.
65      *
66      * @param originalObject the object to replace
67      * @param replacementObject the replacement object
68      */

69     public void addReplacementObject(Object JavaDoc originalObject, Object JavaDoc replacementObject)
70     {
71         _objectMap.put(originalObject, replacementObject);
72     }
73
74     /**
75      * Add a new substring replacement mapping.
76      *
77      * @param originalSubstring the substring to replace
78      * @param replacementSubstring the replacement substring
79      */

80     public void addReplacementSubstring(String JavaDoc originalSubstring,
81             String JavaDoc replacementSubstring)
82     {
83         if (originalSubstring == null || replacementSubstring == null)
84         {
85             throw new NullPointerException JavaDoc();
86         }
87
88         _substringMap.put(originalSubstring, replacementSubstring);
89     }
90
91     /**
92      * Sets substring delimiters.
93      */

94     public void setSubstringDelimiters(String JavaDoc startDelimiter, String JavaDoc endDelimiter)
95     {
96         if (startDelimiter == null || endDelimiter == null)
97         {
98             throw new NullPointerException JavaDoc();
99         }
100
101         _startDelim = startDelimiter;
102         _endDelim = endDelimiter;
103     }
104
105     private String JavaDoc replaceSubstrings(String JavaDoc value)
106     {
107         StringBuffer JavaDoc buffer = null;
108
109         for (Iterator JavaDoc it = _substringMap.entrySet().iterator(); it.hasNext();)
110         {
111             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
112             String JavaDoc original = (String JavaDoc)entry.getKey();
113             String JavaDoc replacement = (String JavaDoc)entry.getValue();
114
115             int startIndex = 0;
116             int lastEndIndex = 0;
117             for(;;)
118             {
119                 startIndex = value.indexOf(original, lastEndIndex);
120                 if (startIndex == -1)
121                 {
122                     if (buffer != null)
123                     {
124                         buffer.append(value.substring(lastEndIndex));
125                     }
126                     break;
127                 }
128
129                 if (buffer == null)
130                 {
131                     buffer = new StringBuffer JavaDoc();
132                 }
133                 buffer.append(value.substring(lastEndIndex, startIndex));
134                 buffer.append(replacement);
135                 lastEndIndex = startIndex + original.length();
136             }
137         }
138
139         return buffer == null ? value : buffer.toString();
140     }
141
142     private String JavaDoc replaceDelimitedSubstrings(String JavaDoc value)
143     {
144         StringBuffer JavaDoc buffer = null;
145
146         int startIndex = 0;
147         int endIndex = 0;
148         int lastEndIndex = 0;
149         for(;;)
150         {
151             startIndex = value.indexOf(_startDelim, lastEndIndex);
152             if (startIndex != -1)
153             {
154                 endIndex = value.indexOf(_endDelim, startIndex + _startDelim.length());
155                 if (endIndex != -1)
156                 {
157                     if (buffer == null)
158                     {
159                         buffer = new StringBuffer JavaDoc();
160                     }
161
162                     String JavaDoc substring = value.substring(
163                             startIndex + _startDelim.length(), endIndex);
164                     if (_substringMap.containsKey(substring))
165                     {
166                         buffer.append(value.substring(lastEndIndex, startIndex));
167                         buffer.append(_substringMap.get(substring));
168                     }
169                     else
170                     {
171                         buffer.append(value.substring(
172                                 lastEndIndex, endIndex + _endDelim.length()));
173                     }
174
175                     lastEndIndex = endIndex + _endDelim.length();
176                 }
177             }
178
179             // No more delimited substring
180
if (startIndex == -1 || endIndex == -1)
181             {
182                 if (buffer != null)
183                 {
184                     buffer.append(value.substring(lastEndIndex));
185                 }
186                 break;
187             }
188         }
189
190         return buffer == null ? value : buffer.toString();
191     }
192
193     ////////////////////////////////////////////////////////////////////////
194
// ITable interface
195

196     public ITableMetaData getTableMetaData()
197     {
198         return _table.getTableMetaData();
199     }
200
201     public int getRowCount()
202     {
203         return _table.getRowCount();
204     }
205
206     public Object JavaDoc getValue(int row, String JavaDoc column) throws DataSetException
207     {
208         Object JavaDoc value = _table.getValue(row, column);
209
210         // Object replacement
211
if (_objectMap.containsKey(value))
212         {
213             return _objectMap.get(value);
214         }
215
216         // Stop here if substring replacement not applicable
217
if (_substringMap.size() == 0 || !(value instanceof String JavaDoc))
218         {
219             return value;
220         }
221
222         // Substring replacement
223
if (_startDelim != null && _endDelim != null)
224         {
225             return replaceDelimitedSubstrings((String JavaDoc)value);
226         }
227         return replaceSubstrings((String JavaDoc)value);
228     }
229 }
230
231
Popular Tags