blob: be238e9deaa7064760b8150be3dd3e702cb06db1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
/*
*******************************************************************************
* Copyright (C) 2002-2012, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
package org.unicode.cldr.util;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
public class VariableReplacer {
// simple implementation for now
private Map m = new TreeMap(Collections.reverseOrder());
// TODO - fix to do streams also, clean up implementation
public VariableReplacer add(String variable, String value) {
m.put(variable, value);
return this;
}
public String replace(String source) {
String oldSource;
do {
oldSource = source;
for (Iterator it = m.keySet().iterator(); it.hasNext();) {
String variable = (String) it.next();
String value = (String) m.get(variable);
source = replaceAll(source, variable, value);
}
} while (!source.equals(oldSource));
return source;
}
public String replaceAll(String source, String key, String value) {
while (true) {
int pos = source.indexOf(key);
if (pos < 0) return source;
source = source.substring(0, pos) + value + source.substring(pos + key.length());
}
}
}
|