Java Code Examples for org.apache.velocity.runtime.resource.util.StringResourceRepository#putStringResource()

The following examples show how to use org.apache.velocity.runtime.resource.util.StringResourceRepository#putStringResource() . 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: VelocityTemplateParser.java    From eagle with Apache License 2.0 6 votes vote down vote up
public VelocityTemplateParser(String templateString) throws ParseErrorException {
    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 resourceRepository = (StringResourceRepository) engine.getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
    resourceRepository.putStringResource(TEMPLATE_NAME, templateString);
    template = engine.getTemplate(TEMPLATE_NAME);
    ASTprocess data = (ASTprocess) template.getData();
    visitor = new ParserNodeVisitor();
    data.jjtAccept(visitor, null);
}
 
Example 2
Source File: CodeGenerator.java    From sundrio with Apache License 2.0 6 votes vote down vote up
public CodeGenerator(CodeGeneratorContext context, M model, String[] parameters, Writer writer, URL templateUrl, String templateResource, String templateContent, Set<Class<? extends Directive>> directives) {
    this.context = context != null ? context : new CodeGeneratorContext();
    this.model = model;
    this.parameters = parameters;
    this.writer = writer;
    this.templateResource = templateResource;
    this.templateUrl = templateUrl;
    this.templateContent = templateContent;
    this.directives = directives;

    StringResourceRepository repo = StringResourceLoader.getRepository();
    try {
        repo.putStringResource(TEMPLATE, templateContent != null ? templateContent : (templateUrl != null ? loadResource(templateUrl) : loadResource(templateResource)));
    } catch (Exception e) {
        throw new RuntimeException(TEMPLATE_READER_FAILURE, e);
    }

    for (Class<? extends Directive> directive : directives) {
        context.getVelocityEngine().loadDirective(directive.getCanonicalName());
    }

    this.template = this.context.getVelocityEngine().getTemplate(TEMPLATE);
    this.context.getVelocityContext().put(MODEL, model);
    this.context.getVelocityContext().put(PARAMETERS, parameters);
}
 
Example 3
Source File: StringResourceLoaderRepositoryTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void setUp() throws Exception
{
    Velocity.reset();
    Velocity.setProperty(Velocity.RESOURCE_LOADERS, "string");
    Velocity.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
    Velocity.addProperty("string.resource.loader.modificationCheckInterval", "1");
    Velocity.setProperty(Velocity.RUNTIME_LOG_INSTANCE, new TestLogger());
    Velocity.init();

    StringResourceRepository repo = getRepo(null, null);
    repo.putStringResource("foo", "This is $foo");
    repo.putStringResource("bar", "This is $bar");

    context = new VelocityContext();
    context.put("foo", "wonderful!");
    context.put("bar", "horrible!");
    context.put("woogie", "a woogie");
}
 
Example 4
Source File: StringResourceLoaderRepositoryTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void testAlternateStaticRepo() throws Exception
{
    VelocityEngine engine = newStringEngine("alternate.repo", true);
    // should be null be for init
    StringResourceRepository repo = getRepo("alternate.repo", null);
    assertNull(repo);
    engine.init();
    // and not null after init
    repo = getRepo("alternate.repo", null);
    assertNotNull(repo);
    repo.putStringResource("foo", "This is NOT $foo");

    // get and merge template with the same name from both runtimes with the same context
    String engineOut = render(engine.getTemplate("foo"));
    String singletonOut = render(RuntimeSingleton.getTemplate("foo"));

    // make sure they're NOT equal
    assertFalse(engineOut.equals(singletonOut));
}
 
Example 5
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 6
Source File: StringResourceLoaderRepositoryTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void testAppRepo() throws Exception
{
    VelocityEngine engine = newStringEngine(null, false);
    engine.init();

    StringResourceRepository repo = getRepo(null, engine);
    assertNotNull(repo);
    repo.putStringResource("woogie", "What is $woogie?");

    String out = render(engine.getTemplate("woogie"));
    assertEquals(out, "What is a woogie?");
}
 
Example 7
Source File: StringResourceLoaderRepositoryTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void testAlternateAppRepo() throws Exception
{
    VelocityEngine engine = newStringEngine("alternate.app.repo", false);
    engine.init();

    StringResourceRepository repo = getRepo("alternate.app.repo", engine);
    assertNotNull(repo);
    repo.putStringResource("you/foo.vm", "You look $foo");

    String out = render(engine.getTemplate("you/foo.vm"));
    assertEquals(out, "You look wonderful!");
}
 
Example 8
Source File: TemplateEngine.java    From gp2srv with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void registerResource(StringResourceRepository repo, String registeredName, String relativeClasspathRef) throws IOException {
	String templateClasspathRef = templatesClasspathPrefix + relativeClasspathRef.replaceAll("\\.\\./", "");
	InputStream templateBodyInputStream = GPhoto2Server.class.getResourceAsStream(templateClasspathRef);
	String templateBodyString = IOUtils.toString(templateBodyInputStream, "UTF-8");
	repo.putStringResource(registeredName, templateBodyString);
}