blob: 14fea240baa7f0189b8d49a48e03145dac129009 (
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
|
// Copyright 2018 Yahoo Holdings. 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 org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
/**
* Uses the Velocity engine to render a template, to and from both String and Path objects.
*
* @author hakonhall
* @author jonmv
*/
public class Template {
private final VelocityEngine velocityEngine = new VelocityEngine();
private final VelocityContext velocityContext = new VelocityContext();
private final String template;
private Template(String template) {
this.template = template;
velocityEngine.addProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,
"org.apache.velocity.runtime.log.NullLogSystem");
velocityEngine.init();
}
public static Template at(Path templatePath) {
return of(IOExceptionUtil.uncheck(() -> new String(Files.readAllBytes(templatePath))));
}
public static Template of(String template) {
return new Template(template);
}
public Template set(String name, Object value) {
velocityContext.put(name, value);
return this;
}
public FileWriter getFileWriterTo(Path destinationPath) {
return new FileWriter(destinationPath, this::render);
}
public String render() {
StringWriter writer = new StringWriter();
velocityEngine.evaluate(velocityContext, writer, "Template", template);
return writer.toString();
}
}
|