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