-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathDefaultMustacheFactory.java
More file actions
277 lines (234 loc) · 7.62 KB
/
DefaultMustacheFactory.java
File metadata and controls
277 lines (234 loc) · 7.62 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package com.github.mustachejava;
import com.github.mustachejava.codes.DefaultMustache;
import com.github.mustachejava.reflect.ReflectionObjectHandler;
import com.github.mustachejava.resolver.DefaultResolver;
import java.io.File;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.function.Function;
import static com.github.mustachejava.util.HtmlEscaper.escape;
/**
* Simplest possible code factory
*/
public class DefaultMustacheFactory implements MustacheFactory {
/**
* Create the default cache for mustache compilations. This is basically
* required by the specification to handle recursive templates.
*/
protected final ConcurrentHashMap<String, Mustache> mustacheCache = createMustacheCache();
/**
* This is the default object handler.
*/
protected ObjectHandler oh = new ReflectionObjectHandler();
/**
* This parser should work with any MustacheFactory
*/
protected final MustacheParser mc = createParser();
/**
* New templates that are generated at runtime are cached here. The template key
* includes the text of the template and the context so we get proper error
* messages and debugging information.
*/
protected final ConcurrentHashMap<FragmentKey, Mustache> templateCache = createLambdaCache();
protected int recursionLimit = 100;
private final MustacheResolver mustacheResolver;
protected ExecutorService es;
public DefaultMustacheFactory() {
this.mustacheResolver = new DefaultResolver();
}
public DefaultMustacheFactory(MustacheResolver mustacheResolver) {
this.mustacheResolver = mustacheResolver;
}
/**
* Use the classpath to resolve mustache templates.
*
* @param classpathResourceRoot the location in the resources where templates are stored
*/
public DefaultMustacheFactory(String classpathResourceRoot) {
this.mustacheResolver = new DefaultResolver(classpathResourceRoot);
}
/**
* Use the file system to resolve mustache templates.
*
* @param fileRoot the root of the file system where templates are stored
*/
public DefaultMustacheFactory(File fileRoot) {
this.mustacheResolver = new DefaultResolver(fileRoot);
}
/**
* Using the directory, namd and extension, resolve a partial to a name.
*
* @param dir
* @param name
* @param extension
* @return
*/
public String resolvePartialPath(String dir, String name, String extension) {
String filePath = name;
// Do not prepend directory if it is already defined
if (!name.startsWith("/")) {
filePath = dir + filePath;
}
int sepIndex = name.lastIndexOf("/");
name = sepIndex == -1 ? name : name.substring(sepIndex);
// Do not append extension if it has the same extension or original one
if (!(name.endsWith(extension) || name.contains("."))) {
filePath = filePath + extension;
}
String path = new File(filePath).getPath();
return ensureForwardSlash(path);
}
private static String ensureForwardSlash(String path) {
return path.replace('\\', '/');
}
@Override
public MustacheVisitor createMustacheVisitor() {
return new DefaultMustacheVisitor(this);
}
@Override
public Reader getReader(String resourceName) {
Reader reader = mustacheResolver.getReader(resourceName);
if (reader == null) {
throw new MustacheNotFoundException(resourceName);
}
return reader;
}
@Override
public void encode(String value, Writer writer) {
escape(value, writer);
}
@Override
public ObjectHandler getObjectHandler() {
return oh;
}
/**
* You can override the default object handler post construction.
*
* @param oh The object handler to use.
*/
public void setObjectHandler(ObjectHandler oh) {
this.oh = oh;
}
/**
* There is an ExecutorService that is used when executing parallel
* operations when a Callable is returned from a mustache value or iterable.
*
* @return the executor service
*/
public ExecutorService getExecutorService() {
return es;
}
/**
* If you need to specify your own executor service you can.
*
* @param es The executor service to use for Future evaluation
*/
public void setExecutorService(ExecutorService es) {
this.es = es;
}
public Mustache getFragment(FragmentKey templateKey) {
Mustache mustache = templateCache.computeIfAbsent(templateKey, getFragmentCacheFunction());
mustache.init();
return mustache;
}
protected Function<FragmentKey, Mustache> getFragmentCacheFunction() {
return (fragmentKey) -> {
StringReader reader = new StringReader(fragmentKey.templateText);
TemplateContext tc = fragmentKey.tc;
return mc.compile(reader, tc.file(), tc.startChars(), tc.endChars(), tc.startOfLine());
};
}
@Override
public Mustache compile(String name) {
Mustache mustache = mustacheCache.computeIfAbsent(name, getMustacheCacheFunction());
mustache.init();
return mustache;
}
@Override
public Mustache compile(Reader reader, String name) {
return compile(reader, name, "{{", "}}");
}
// Template functions need this to comply with the specification
public Mustache compile(Reader reader, String file, String sm, String em) {
Mustache compile = mc.compile(reader, file, sm, em);
compile.init();
partialCache.remove();
return compile;
}
@Override
public String translate(String from) {
return from;
}
/**
* Override this method to apply any filtering to text that will appear
* verbatim in the output template.
*
* @param appended The text to be appended to the output
* @param startOfLine Are we at the start of the line?
* @return the filtered string
*/
public String filterText(String appended, boolean startOfLine) {
return appended;
}
/**
* Maximum recursion limit for partials.
*
* @param recursionLimit the number of recursions we will attempt before failing
*/
public void setRecursionLimit(int recursionLimit) {
this.recursionLimit = recursionLimit;
}
public int getRecursionLimit() {
return recursionLimit;
}
private final ThreadLocal<Map<String, Mustache>> partialCache = new ThreadLocal<Map<String, Mustache>>() {
@Override
protected Map<String, Mustache> initialValue() {
return new HashMap<>();
}
};
/**
* In order to handle recursion, we need a temporary thread local cache during compilation
* that is ultimately thrown away after the top level partial is complete.
*
* @param s the name of the partial to compile
* @return the compiled partial
*/
public Mustache compilePartial(String s) {
final Map<String, Mustache> cache = partialCache.get();
final Mustache cached = cache.get(s);
if (cached != null) {
// Our implementation supports this but I
// don't think it makes sense in the interface
if (cached instanceof DefaultMustache) {
((DefaultMustache)cached).setRecursive();
}
return cached;
}
try {
final Mustache mustache = mc.compile(s);
cache.put(s, mustache);
mustache.init();
return mustache;
} finally {
cache.remove(s);
}
}
protected MustacheParser createParser() {
return new MustacheParser(this);
}
protected Function<String, Mustache> getMustacheCacheFunction() {
return mc::compile;
}
protected ConcurrentHashMap<String, Mustache> createMustacheCache() {
return new ConcurrentHashMap<>();
}
protected ConcurrentHashMap<FragmentKey, Mustache> createLambdaCache() {
return new ConcurrentHashMap<>();
}
}