]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/core/src/com/ibm/icu/dev/test/util/VariableReplacer.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / core / src / com / ibm / icu / dev / test / util / VariableReplacer.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2002-2008, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\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
11 \r
12 public class VariableReplacer {\r
13     // simple implementation for now\r
14     private Map m = new TreeMap(Collections.reverseOrder());\r
15 \r
16     // TODO - fix to do streams also, clean up implementation\r
17 \r
18     public VariableReplacer add(String variable, String value) {\r
19         m.put(variable, value);\r
20         return this;\r
21     }\r
22     public String replace(String source) {\r
23         String oldSource;\r
24         do {\r
25             oldSource = 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
30             }\r
31         } while (!source.equals(oldSource));\r
32         return source;\r
33     }\r
34     public String replaceAll(String source, String key, String value) {\r
35         while (true) {\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
39         }\r
40     }\r
41 }\r
42 \r