1 21 package org.dbunit.dataset; 22 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 34 public class ReplacementDataSet extends AbstractDataSet 35 { 36 private final IDataSet _dataSet; 37 private final Map _objectMap; 38 private final Map _substringMap; 39 private String _startDelim; 40 private String _endDelim; 41 42 43 48 public ReplacementDataSet(IDataSet dataSet) 49 { 50 _dataSet = dataSet; 51 _objectMap = new HashMap (); 52 _substringMap = new HashMap (); 53 } 54 55 62 public ReplacementDataSet(IDataSet dataSet, Map objectMap, Map substringMap) 63 { 64 _dataSet = dataSet; 65 _objectMap = objectMap == null ? new HashMap () : objectMap; 66 _substringMap = substringMap == null ? new HashMap () : substringMap; 67 } 68 69 75 public void addReplacementObject(Object originalObject, Object replacementObject) 76 { 77 _objectMap.put(originalObject, replacementObject); 78 } 79 80 86 public void addReplacementSubstring(String originalSubstring, 87 String replacementSubstring) 88 { 89 if (originalSubstring == null || replacementSubstring == null) 90 { 91 throw new NullPointerException (); 92 } 93 94 _substringMap.put(originalSubstring, replacementSubstring); 95 } 96 97 100 public void setSubstringDelimiters(String startDelimiter, String endDelimiter) 101 { 102 if (startDelimiter == null || endDelimiter == null) 103 { 104 throw new NullPointerException (); 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 120 protected ITableIterator createIterator(boolean reversed) 121 throws DataSetException 122 { 123 return new ReplacementIterator(reversed ? 124 _dataSet.reverseIterator() : _dataSet.iterator()); 125 } 126 127 130 public String [] getTableNames() throws DataSetException 131 { 132 return _dataSet.getTableNames(); 133 } 134 135 public ITableMetaData getTableMetaData(String tableName) 136 throws DataSetException 137 { 138 return _dataSet.getTableMetaData(tableName); 139 } 140 141 public ITable getTable(String tableName) throws DataSetException 142 { 143 return createReplacementTable(_dataSet.getTable(tableName)); 144 } 145 146 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 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 |