001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021: package org.dbunit.dataset;
022:
023: import org.slf4j.Logger;
024: import org.slf4j.LoggerFactory;
025:
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.Map;
029:
030: /**
031: * Decorator that replace configured values from the decorated table
032: * with replacement values.
033: *
034: * @author Manuel Laflamme
035: * @since Mar 17, 2003
036: * @version $Revision: 581 $
037: */
038: public class ReplacementTable implements ITable {
039:
040: /**
041: * Logger for this class
042: */
043: private static final Logger logger = LoggerFactory
044: .getLogger(ReplacementTable.class);
045:
046: private final ITable _table;
047: private final Map _objectMap;
048: private final Map _substringMap;
049: private String _startDelim;
050: private String _endDelim;
051:
052: /**
053: * Create a new ReplacementTable object that decorates the specified table.
054: *
055: * @param table the decorated table
056: */
057: public ReplacementTable(ITable table) {
058: this (table, new HashMap(), new HashMap(), null, null);
059: }
060:
061: public ReplacementTable(ITable table, Map objectMap,
062: Map substringMap, String startDelimiter, String endDelimiter) {
063: _table = table;
064: _objectMap = objectMap;
065: _substringMap = substringMap;
066: _startDelim = startDelimiter;
067: _endDelim = endDelimiter;
068: }
069:
070: /**
071: * Add a new Object replacement mapping.
072: *
073: * @param originalObject the object to replace
074: * @param replacementObject the replacement object
075: */
076: public void addReplacementObject(Object originalObject,
077: Object replacementObject) {
078: logger.debug("addReplacementObject(originalObject="
079: + originalObject + ", replacementObject="
080: + replacementObject + ") - start");
081:
082: _objectMap.put(originalObject, replacementObject);
083: }
084:
085: /**
086: * Add a new substring replacement mapping.
087: *
088: * @param originalSubstring the substring to replace
089: * @param replacementSubstring the replacement substring
090: */
091: public void addReplacementSubstring(String originalSubstring,
092: String replacementSubstring) {
093: logger.debug("addReplacementSubstring(originalSubstring="
094: + originalSubstring + ", replacementSubstring="
095: + replacementSubstring + ") - start");
096:
097: if (originalSubstring == null || replacementSubstring == null) {
098: throw new NullPointerException();
099: }
100:
101: _substringMap.put(originalSubstring, replacementSubstring);
102: }
103:
104: /**
105: * Sets substring delimiters.
106: */
107: public void setSubstringDelimiters(String startDelimiter,
108: String endDelimiter) {
109: logger.debug("setSubstringDelimiters(startDelimiter="
110: + startDelimiter + ", endDelimiter=" + endDelimiter
111: + ") - start");
112:
113: if (startDelimiter == null || endDelimiter == null) {
114: throw new NullPointerException();
115: }
116:
117: _startDelim = startDelimiter;
118: _endDelim = endDelimiter;
119: }
120:
121: /**
122: * Replace occurrences of source in text with target. Operates directly on text.
123: */
124: private void replaceAll(StringBuffer text, String source,
125: String target) {
126: int index = 0;
127: while ((index = text.toString().indexOf(source, index)) != -1) {
128: text.replace(index, index + source.length(), target);
129: index += target.length();
130: }
131: }
132:
133: private String replaceStrings(String value, String lDelim,
134: String rDelim) {
135: StringBuffer buffer = new StringBuffer(value);
136:
137: for (Iterator it = _substringMap.entrySet().iterator(); it
138: .hasNext();) {
139: Map.Entry entry = (Map.Entry) it.next();
140: String original = (String) entry.getKey();
141: String replacement = (String) entry.getValue();
142: replaceAll(buffer, lDelim + original + rDelim, replacement);
143: }
144:
145: return buffer == null ? value : buffer.toString();
146: }
147:
148: private String replaceSubstrings(String value) {
149: return replaceStrings(value, "", "");
150: }
151:
152: private String replaceDelimitedSubstrings(String value) {
153: return replaceStrings(value, _startDelim, _endDelim);
154: }
155:
156: ////////////////////////////////////////////////////////////////////////
157: // ITable interface
158:
159: public ITableMetaData getTableMetaData() {
160: logger.debug("getTableMetaData() - start");
161:
162: return _table.getTableMetaData();
163: }
164:
165: public int getRowCount() {
166: logger.debug("getRowCount() - start");
167:
168: return _table.getRowCount();
169: }
170:
171: public Object getValue(int row, String column)
172: throws DataSetException {
173: logger.debug("getValue(row=" + row + ", column=" + column
174: + ") - start");
175:
176: Object value = _table.getValue(row, column);
177:
178: // Object replacement
179: if (_objectMap.containsKey(value)) {
180: return _objectMap.get(value);
181: }
182:
183: // Stop here if substring replacement not applicable
184: if (_substringMap.size() == 0 || !(value instanceof String)) {
185: return value;
186: }
187:
188: // Substring replacement
189: if (_startDelim != null && _endDelim != null) {
190: return replaceDelimitedSubstrings((String) value);
191: }
192: return replaceSubstrings((String) value);
193: }
194: }
|