1 21 package org.dbunit.dataset; 22 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 27 35 public class ReplacementTable implements ITable 36 { 37 private final ITable _table; 38 private final Map _objectMap; 39 private final Map _substringMap; 40 private String _startDelim; 41 private String _endDelim; 42 43 48 public ReplacementTable(ITable table) 49 { 50 this(table, new HashMap (), new HashMap (), null, null); 51 } 52 53 public ReplacementTable(ITable table, Map objectMap, Map substringMap, 54 String startDelimiter, String endDelimiter) 55 { 56 _table = table; 57 _objectMap = objectMap; 58 _substringMap = substringMap; 59 _startDelim = startDelimiter; 60 _endDelim = endDelimiter; 61 } 62 63 69 public void addReplacementObject(Object originalObject, Object replacementObject) 70 { 71 _objectMap.put(originalObject, replacementObject); 72 } 73 74 80 public void addReplacementSubstring(String originalSubstring, 81 String replacementSubstring) 82 { 83 if (originalSubstring == null || replacementSubstring == null) 84 { 85 throw new NullPointerException (); 86 } 87 88 _substringMap.put(originalSubstring, replacementSubstring); 89 } 90 91 94 public void setSubstringDelimiters(String startDelimiter, String endDelimiter) 95 { 96 if (startDelimiter == null || endDelimiter == null) 97 { 98 throw new NullPointerException (); 99 } 100 101 _startDelim = startDelimiter; 102 _endDelim = endDelimiter; 103 } 104 105 private String replaceSubstrings(String value) 106 { 107 StringBuffer buffer = null; 108 109 for (Iterator it = _substringMap.entrySet().iterator(); it.hasNext();) 110 { 111 Map.Entry entry = (Map.Entry )it.next(); 112 String original = (String )entry.getKey(); 113 String replacement = (String )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 (); 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 replaceDelimitedSubstrings(String value) 143 { 144 StringBuffer 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 (); 160 } 161 162 String 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 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 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 getValue(int row, String column) throws DataSetException 207 { 208 Object value = _table.getValue(row, column); 209 210 if (_objectMap.containsKey(value)) 212 { 213 return _objectMap.get(value); 214 } 215 216 if (_substringMap.size() == 0 || !(value instanceof String )) 218 { 219 return value; 220 } 221 222 if (_startDelim != null && _endDelim != null) 224 { 225 return replaceDelimitedSubstrings((String )value); 226 } 227 return replaceSubstrings((String )value); 228 } 229 } 230 231 | Popular Tags |