01: /*
02: *******************************************************************************
03: * Copyright (C) 2002-2005, International Business Machines Corporation and *
04: * others. All Rights Reserved. *
05: *******************************************************************************
06: */
07: package com.ibm.icu.dev.test.util;
08:
09: import java.util.Collections;
10: import java.util.Comparator;
11: import java.util.Iterator;
12: import java.util.Map;
13: import java.util.TreeMap;
14:
15: public class VariableReplacer {
16: // simple implementation for now
17: private Comparator c;
18: private Map m = new TreeMap(Collections.reverseOrder());
19:
20: // TODO - fix to do streams also, clean up implementation
21:
22: public VariableReplacer add(String variable, String value) {
23: m.put(variable, value);
24: return this ;
25: }
26:
27: public String replace(String source) {
28: String oldSource;
29: do {
30: oldSource = source;
31: for (Iterator it = m.keySet().iterator(); it.hasNext();) {
32: String variable = (String) it.next();
33: String value = (String) m.get(variable);
34: source = replaceAll(source, variable, value);
35: }
36: } while (!source.equals(oldSource));
37: return source;
38: }
39:
40: public String replaceAll(String source, String key, String value) {
41: while (true) {
42: int pos = source.indexOf(key);
43: if (pos < 0)
44: return source;
45: source = source.substring(0, pos) + value
46: + source.substring(pos + key.length());
47: }
48: }
49: }
|