-
Notifications
You must be signed in to change notification settings - Fork 524
/
HandlebarsTemplateEngineImpl.java
196 lines (165 loc) · 5.5 KB
/
HandlebarsTemplateEngineImpl.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
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
/*
* Copyright 2014 Red Hat, Inc.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package io.vertx.ext.web.templ.handlebars.impl;
import com.github.jknack.handlebars.Context;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Template;
import com.github.jknack.handlebars.ValueResolver;
import com.github.jknack.handlebars.io.TemplateLoader;
import com.github.jknack.handlebars.io.TemplateSource;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.ext.web.common.template.CachingTemplateEngine;
import io.vertx.ext.web.common.template.impl.TemplateHolder;
import io.vertx.ext.web.templ.handlebars.HandlebarsTemplateEngine;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import static io.vertx.core.impl.Utils.isWindows;
/**
* @author <a href="http://pmlopes@gmail.com">Paulo Lopes</a>
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class HandlebarsTemplateEngineImpl extends CachingTemplateEngine<Template> implements HandlebarsTemplateEngine {
private static final List<ValueResolver> VALUE_RESOLVERS_LIST = ValueResolver.defaultValueResolvers();
private static final ValueResolver[] VALUE_RESOLVERS = VALUE_RESOLVERS_LIST.toArray(new ValueResolver[0]);
private final Handlebars handlebars;
private final Loader loader;
private ValueResolver[] resolvers;
public HandlebarsTemplateEngineImpl(Vertx vertx, String extension) {
super(vertx, extension);
loader = new Loader(vertx);
// custom resolvers
resolvers = new ValueResolver[VALUE_RESOLVERS.length + 2];
// custom resolvers for vertx json types
resolvers[0] = JsonArrayValueResolver.INSTANCE;
resolvers[1] = JsonObjectValueResolver.INSTANCE;
// default resolvers
System.arraycopy(VALUE_RESOLVERS, 0, resolvers, 2, VALUE_RESOLVERS.length);
// create the engine
handlebars = new Handlebars(loader)
.with(new JsonFormatter());
}
@Override
public Future<Buffer> render(Map<String, Object> context, String templateFile) {
try {
String src = adjustLocation(templateFile);
TemplateHolder<Template> template = getTemplate(src);
if (template == null) {
// either it's not cache or cache is disabled
int idx = findLastFileSeparator(src);
String prefix = "";
String basename = src;
if (idx != -1) {
prefix = src.substring(0, idx);
basename = src.substring(idx + 1);
}
synchronized (this) {
loader.setPrefix(prefix);
template = new TemplateHolder<>(handlebars.compile(basename), prefix);
}
putTemplate(src, template);
}
Context engineContext = Context.newBuilder(context).resolver(resolvers).build();
return Future.succeededFuture(Buffer.buffer(template.template().apply(engineContext)));
} catch (Exception ex) {
return Future.failedFuture(ex);
}
}
private static int findLastFileSeparator(String src) {
if (isWindows()) {
return Math.max(src.lastIndexOf('/'), src.lastIndexOf('\\'));
}
return src.lastIndexOf('/');
}
@Override
public <T> T unwrap() {
return (T) handlebars;
}
@Override
public HandlebarsTemplateEngine setResolvers(ValueResolver... resolvers) {
this.resolvers = resolvers;
return this;
}
private class Loader implements TemplateLoader {
private final Vertx vertx;
private String templateDirectory;
private Charset charset = Charset.defaultCharset();
Loader(Vertx vertx) {
this.vertx = vertx;
}
@Override
public TemplateSource sourceAt(String location) throws IOException {
final String loc = resolve(location);
final Buffer templ;
if (vertx.fileSystem().existsBlocking(loc)) {
templ = vertx.fileSystem()
.readFileBlocking(loc);
} else {
throw new FileNotFoundException(loc);
}
if (templ == null) {
throw new IOException("Cannot read resource " + loc);
}
long lastMod = System.currentTimeMillis();
return new TemplateSource() {
@Override
public String content(Charset charset) {
return templ.toString(charset);
}
@Override
public String filename() {
return loc;
}
@Override
public long lastModified() {
return lastMod;
}
};
}
@Override
public String resolve(String location) {
return templateDirectory + "/" + adjustLocation(location);
}
@Override
public String getPrefix() {
return templateDirectory;
}
@Override
public String getSuffix() {
return extension;
}
@Override
public void setPrefix(String prefix) {
templateDirectory = prefix;
}
@Override
public void setSuffix(String suffix) {
extension = suffix;
}
@Override
public void setCharset(Charset charset) {
this.charset = charset;
}
@Override
public Charset getCharset() {
return charset;
}
}
}