blob: 553f2ffba3637d7bfa9b879aad8dae199325ea19 (
plain) (
blame)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.task.util.file;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
/**
* A very simple template engine when there's little complexity and lots of Velocity special characters $ and #,
* i.e. typically shell script.
*
* @author hakonhall
*/
public class Templar {
private final String template;
private static final String prefix = "<%=";
private static final String suffix = "%>";
private final Map<String, String> settings = new HashMap<>();
public static Templar fromUtf8File(Path path) {
return new Templar(new UnixPath(path).readUtf8File());
}
public Templar(String template) {
this.template = template;
}
public Templar set(String name, String value) {
settings.put(name, value);
return this;
}
public String resolve() {
StringBuilder text = new StringBuilder(template.length() * 2);
int start= 0;
int end;
for (; start < template.length(); start = end) {
int prefixStart = template.indexOf(prefix, start);
if (prefixStart == -1) {
text.append(template, start, template.length());
break;
} else {
text.append(template, start, prefixStart);
}
int suffixStart = template.indexOf(suffix, prefixStart + prefix.length());
if (suffixStart == -1) {
throw new IllegalArgumentException("Prefix at offset " + prefixStart + " is not terminated");
}
int prefixEnd = prefixStart + prefix.length();
String name = template.substring(prefixEnd, suffixStart).trim();
String value = settings.get(name);
if (value == null) {
throw new IllegalArgumentException("No value is set for name '" + name + "' at offset " + prefixEnd);
}
text.append(value);
end = suffixStart + suffix.length();
}
return text.toString();
}
public FileWriter getFileWriterTo(Path path) {
return new FileWriter(path, this::resolve);
}
}
|