2 *******************************************************************************
\r
3 * Copyright (C) 2002-2008, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.util;import java.util.Collections;
\r
8 import java.util.Iterator;
\r
9 import java.util.Map;
\r
10 import java.util.TreeMap;
\r
12 public class VariableReplacer {
\r
13 // simple implementation for now
\r
14 private Map m = new TreeMap(Collections.reverseOrder());
\r
16 // TODO - fix to do streams also, clean up implementation
\r
18 public VariableReplacer add(String variable, String value) {
\r
19 m.put(variable, value);
\r
22 public String replace(String source) {
\r
26 for (Iterator it = m.keySet().iterator(); it.hasNext();) {
\r
27 String variable = (String) it.next();
\r
28 String value = (String) m.get(variable);
\r
29 source = replaceAll(source, variable, value);
\r
31 } while (!source.equals(oldSource));
\r
34 public String replaceAll(String source, String key, String value) {
\r
36 int pos = source.indexOf(key);
\r
37 if (pos < 0) return source;
\r
38 source = source.substring(0,pos) + value + source.substring(pos+key.length());
\r