use of org.apache.velocity.runtime.parser.node.SimpleNode in project gerrit by GerritCodeReview.
the class OutgoingEmail method velocify.
protected String velocify(String template) throws EmailException {
try {
RuntimeInstance runtime = args.velocityRuntime;
String templateName = "OutgoingEmail";
SimpleNode tree = runtime.parse(new StringReader(template), templateName);
InternalContextAdapterImpl ica = new InternalContextAdapterImpl(velocityContext);
ica.pushCurrentTemplateName(templateName);
try {
tree.init(ica, runtime);
StringWriter w = new StringWriter();
tree.render(ica, w);
return w.toString();
} finally {
ica.popCurrentTemplateName();
}
} catch (Exception e) {
throw new EmailException("Cannot format velocity template: " + template, e);
}
}
use of org.apache.velocity.runtime.parser.node.SimpleNode in project auto by google.
the class TemplateTest method velocityRender.
private String velocityRender(String template, Map<String, ?> vars) {
VelocityContext velocityContext = new VelocityContext(new TreeMap<String, Object>(vars));
StringWriter writer = new StringWriter();
SimpleNode parsedTemplate;
try {
parsedTemplate = velocityRuntimeInstance.parse(new StringReader(template), testName.getMethodName());
} catch (org.apache.velocity.runtime.parser.ParseException e) {
throw new AssertionError(e);
}
boolean rendered = velocityRuntimeInstance.render(velocityContext, writer, parsedTemplate.getTemplateName(), parsedTemplate);
assertThat(rendered).isTrue();
return writer.toString();
}
use of org.apache.velocity.runtime.parser.node.SimpleNode in project Gargoyle by callakrsos.
the class MailUtil method getTemplateFromFile.
/**
* 파일로부터 템플릿 정보를 얻어온다.
*
* @Date 2015. 9. 13.
* @param templateFileName
* @return
* @throws Exception
* @User KYJ
*/
public static Template getTemplateFromFile(final String templateFileName) throws Exception {
String readFileToString = "";
if (templateFileName.startsWith("classpath:")) {
String res = templateFileName.replace("classpath:", "");
InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream(res);
readFileToString = ValueUtil.toString(resourceAsStream);
} else
readFileToString = FileUtils.readFileToString(new File(templateFileName));
RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
StringReader reader = new StringReader(readFileToString);
SimpleNode node = runtimeServices.parse(reader, templateFileName);
Template template = new Template();
template.setRuntimeServices(runtimeServices);
template.setData(node);
template.initDocument();
return template;
}
Aggregations