Search in sources :

Example 1 with SimpleNode

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);
    }
}
Also used : InternalContextAdapterImpl(org.apache.velocity.context.InternalContextAdapterImpl) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) EmailException(com.google.gerrit.common.errors.EmailException) RuntimeInstance(org.apache.velocity.runtime.RuntimeInstance) OrmException(com.google.gwtorm.server.OrmException) ValidationException(com.google.gerrit.server.validators.ValidationException) MalformedURLException(java.net.MalformedURLException) EmailException(com.google.gerrit.common.errors.EmailException) SimpleNode(org.apache.velocity.runtime.parser.node.SimpleNode)

Example 2 with SimpleNode

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();
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) SimpleNode(org.apache.velocity.runtime.parser.node.SimpleNode) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader)

Example 3 with SimpleNode

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;
}
Also used : RuntimeServices(org.apache.velocity.runtime.RuntimeServices) InputStream(java.io.InputStream) StringReader(java.io.StringReader) File(java.io.File) SimpleNode(org.apache.velocity.runtime.parser.node.SimpleNode) Template(org.apache.velocity.Template)

Aggregations

StringReader (java.io.StringReader)3 SimpleNode (org.apache.velocity.runtime.parser.node.SimpleNode)3 StringWriter (java.io.StringWriter)2 EmailException (com.google.gerrit.common.errors.EmailException)1 ValidationException (com.google.gerrit.server.validators.ValidationException)1 OrmException (com.google.gwtorm.server.OrmException)1 File (java.io.File)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 Template (org.apache.velocity.Template)1 VelocityContext (org.apache.velocity.VelocityContext)1 InternalContextAdapterImpl (org.apache.velocity.context.InternalContextAdapterImpl)1 RuntimeInstance (org.apache.velocity.runtime.RuntimeInstance)1 RuntimeServices (org.apache.velocity.runtime.RuntimeServices)1