Java Code Examples for org.apache.velocity.Template#process()

The following examples show how to use org.apache.velocity.Template#process() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VelocityScriptEngine.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
/**
 * Compile a template
 * @param script template source
 * @return compiled template
 * @throws ScriptException
 */
public CompiledScript compile(Reader script) throws ScriptException
{
    initVelocityEngine(null);
    ResourceLoader resourceLoader = new SingleResourceReader(script);
    Template template = new Template();
    template.setRuntimeServices(velocityEngine);
    template.setResourceLoader(resourceLoader);
    try
    {
        template.process();
    }
    catch(Exception e)
    {
        // CB TODO - exception may have line/col informations, that ScriptException can exploit
        throw new ScriptException(e);
    }
    return new VelocityCompiledScript(this, template);
}
 
Example 2
Source File: VelocityTemplateTest.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Test
public void testVelocityTemplate() {
    String templateString = "This alert ($category) was generated because $reason and $REASON from $source at $created_time";
    String resultString = "This alert ($category) was generated because timeout and IO error from localhost at 2016-11-30 05:52:47,053";
    VelocityEngine engine = new VelocityEngine();
    engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute");
    engine.setProperty("runtime.log.logsystem.log4j.logger", LOG.getName());
    engine.setProperty(Velocity.RESOURCE_LOADER, "string");
    engine.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
    engine.addProperty("string.resource.loader.repository.static", "false");
    // engine.addProperty("runtime.references.strict", "true");
    engine.init();

    StringResourceRepository repo = (StringResourceRepository) engine.getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
    repo.putStringResource("alert_template", "");
    repo.putStringResource("alert_template", templateString);

    Assert.assertEquals(templateString, repo.getStringResource("alert_template").getBody());

    VelocityContext context = new VelocityContext();
    context.put("reason", "timeout");
    context.put("REASON", "IO error");
    context.put("source","localhost");
    context.put("created_time", "2016-11-30 05:52:47,053");

    Template velocityTemplate = engine.getTemplate("alert_template");
    ASTprocess data = (ASTprocess) velocityTemplate.getData();
    ReferenceContext referenceContext = new ReferenceContext();
    data.jjtAccept(referenceContext,null);
    Assert.assertEquals(5, referenceContext.getReferences().size());
    StringWriter writer = new StringWriter();
    velocityTemplate.merge(context, writer);
    velocityTemplate.process();
    Assert.assertEquals(resultString, writer.toString());
}
 
Example 3
Source File: TestVelocityView.java    From gocd with Apache License 2.0 5 votes vote down vote up
@Override
protected Template getTemplate() throws Exception {
    Template realTemplateForTest = setupTemplate(loader, runtimeServices, "template1", new File("src/main/webapp/" + templatePath).toURL().openStream());

    List<Template> templates = new ArrayList<>();
    templates.add(realTemplateForTest);
    templates.addAll(additionalTemplates);

    for (Template template : templates) {
        template.process();
    }

    return templates.get(0);
}