Search in sources :

Example 1 with RuntimeInstance

use of org.apache.velocity.runtime.RuntimeInstance in project auto by google.

the class TemplateTest method setUp.

@Before
public void setUp() {
    velocityRuntimeInstance = new RuntimeInstance();
    // Ensure that $undefinedvar will produce an exception rather than outputting $undefinedvar.
    velocityRuntimeInstance.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, "true");
    velocityRuntimeInstance.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, new NullLogChute());
    // Disable any logging that Velocity might otherwise see fit to do.
    velocityRuntimeInstance.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, new NullLogChute());
    velocityRuntimeInstance.init();
}
Also used : NullLogChute(org.apache.velocity.runtime.log.NullLogChute) RuntimeInstance(org.apache.velocity.runtime.RuntimeInstance) Before(org.junit.Before)

Example 2 with RuntimeInstance

use of org.apache.velocity.runtime.RuntimeInstance in project gerrit by GerritCodeReview.

the class OutgoingEmail method velocifyFile.

protected String velocifyFile(String name) throws EmailException {
    try {
        RuntimeInstance runtime = args.velocityRuntime;
        if (runtime.getLoaderNameForResource(name) == null) {
            name = "com/google/gerrit/server/mail/" + name;
        }
        Template template = runtime.getTemplate(name, UTF_8.name());
        StringWriter w = new StringWriter();
        template.merge(velocityContext, w);
        return w.toString();
    } catch (Exception e) {
        throw new EmailException("Cannot format velocity template " + name, e);
    }
}
Also used : StringWriter(java.io.StringWriter) 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) Template(org.apache.velocity.Template)

Example 3 with RuntimeInstance

use of org.apache.velocity.runtime.RuntimeInstance 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 4 with RuntimeInstance

use of org.apache.velocity.runtime.RuntimeInstance in project gerrit by GerritCodeReview.

the class VelocityRuntimeProvider method get.

@Override
public RuntimeInstance get() {
    String rl = "resource.loader";
    String pkg = "org.apache.velocity.runtime.resource.loader";
    Properties p = new Properties();
    p.setProperty(RuntimeConstants.VM_PERM_INLINE_LOCAL, "true");
    p.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, Slf4jLogChute.class.getName());
    p.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, "true");
    p.setProperty("runtime.log.logsystem.log4j.category", "velocity");
    if (Files.isDirectory(site.mail_dir)) {
        p.setProperty(rl, "file, class");
        p.setProperty("file." + rl + ".class", pkg + ".FileResourceLoader");
        p.setProperty("file." + rl + ".path", site.mail_dir.toAbsolutePath().toString());
        p.setProperty("class." + rl + ".class", pkg + ".ClasspathResourceLoader");
    } else {
        p.setProperty(rl, "class");
        p.setProperty("class." + rl + ".class", pkg + ".ClasspathResourceLoader");
    }
    RuntimeInstance ri = new RuntimeInstance();
    try {
        ri.init(p);
    } catch (Exception err) {
        throw new ProvisionException("Cannot configure Velocity templates", err);
    }
    return ri;
}
Also used : ProvisionException(com.google.inject.ProvisionException) RuntimeInstance(org.apache.velocity.runtime.RuntimeInstance) Properties(java.util.Properties) ProvisionException(com.google.inject.ProvisionException)

Aggregations

RuntimeInstance (org.apache.velocity.runtime.RuntimeInstance)4 EmailException (com.google.gerrit.common.errors.EmailException)2 ValidationException (com.google.gerrit.server.validators.ValidationException)2 OrmException (com.google.gwtorm.server.OrmException)2 StringWriter (java.io.StringWriter)2 MalformedURLException (java.net.MalformedURLException)2 ProvisionException (com.google.inject.ProvisionException)1 StringReader (java.io.StringReader)1 Properties (java.util.Properties)1 Template (org.apache.velocity.Template)1 InternalContextAdapterImpl (org.apache.velocity.context.InternalContextAdapterImpl)1 NullLogChute (org.apache.velocity.runtime.log.NullLogChute)1 SimpleNode (org.apache.velocity.runtime.parser.node.SimpleNode)1 Before (org.junit.Before)1