This repository has been archived by the owner on Aug 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Template.java
129 lines (118 loc) · 3.63 KB
/
Template.java
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import java.util.*;
import java.io.*;
abstract class TemplateValue {
protected String type;
abstract public String toString();
public String getType() {
return type;
}
/**
* Returns the output of the value escaped for use in a templae
* @param String escType The type of the template
* @returns String
*/
public String getEscaped(String escType) {
String output = this.toString();
if (output == null) {
Manager.logErr("null toString() " + this.getClass());
}
if (escType.equals("html")) {
// Any non-html values put into an html template should be html encoded
if (escType.equals("html") && !this.getType().equals("html")) {
output = output.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """);
}
// Escape any dollar signs in the values in case they get mixed up with placeholders
output = output.replace("$", "\\$");
}
return output;
}
}
public class Template extends TemplateValue{
private static File templateDir = new File(Manager.getSetting("template_dir", ""));
private File templateFile;
private String content;
private String type;
private Map<String, TemplateValue> data;
public Template(String name) throws IOException {
this(name, "html");
}
public Template(String name, String type) throws IOException {
this.type = type;
templateFile = new File(templateDir, name + "." + type);
content = Manager.readFile(new FileInputStream(templateFile));
data = new HashMap<String, TemplateValue>();
if (type.equals("html")) setData("rootdomain", Service.getById("root").getDomain());
}
public void setData(String key, String val) {
if (val != null) setData(key, new TemplateString(val));
}
public void setData(String key, TemplateValue val) {
data.put(key, val);
}
public String getFileName() {
return templateFile.getName();
}
public String getType() {
return type;
}
public String toString() {
String output = new String(content);
Iterator<Map.Entry<String, TemplateValue>> iter = data.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, TemplateValue> entry = iter.next();
String key = entry.getKey();
TemplateValue val = entry.getValue();
if (key == null || val == null) continue;
key = "$"+key+"$";
output = output.replace(key, val.getEscaped(type));
}
// Replace any placeholders which haven't got data with empty strings
output = output.replaceAll("\\$.+?\\$", "");
// Unescape remaining dollar signs
output = output.replace("\\$", "$");
return output;
}
}
/**
* A very simple extention of TemplateValue, which returns a single string
*/
class TemplateString extends TemplateValue {
private String value;
public TemplateString(String value) {
if (value == null) throw new NullPointerException();
this.type = "text";
this.value = value;
}
public String toString() {
return value;
}
}
class TemplateGroup extends TemplateValue {
private List<TemplateValue> values;
public TemplateGroup(String type) {
this.type = type;
values = new LinkedList<TemplateValue>();
}
public void add(TemplateValue val) {
// Only add values of the same type to avoid confusion
if (val.getType().equals(type)) values.add(val);
}
public String getEscaped(String escType) {
String output = "";
Iterator<TemplateValue> iter = values.iterator();
while (iter.hasNext()) {
TemplateValue val = iter.next();
output += val.getEscaped(escType);
}
return output;
}
public String toString() {
String output = "";
Iterator<TemplateValue> iter = values.iterator();
while (iter.hasNext()) {
TemplateValue val = iter.next();
output += val.toString();
}
return output;
}
}