org.apache.velocity.runtime.resource.loader.StringResourceLoader Java Examples

The following examples show how to use org.apache.velocity.runtime.resource.loader.StringResourceLoader. 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: ResourceExistsTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void setUp() throws Exception
    {
try {
        velocity = new VelocityEngine();
        velocity.setProperty("resource.loader", "file,string");
        velocity.setProperty("file.resource.loader.path", path);
        velocity.setProperty("string.resource.loader.class", StringResourceLoader.class.getName());

        // actual instance of logger
        logger.on();
        velocity.setProperty(RuntimeConstants.RUNTIME_LOG_INSTANCE, logger);
        velocity.setProperty("runtime.log.logsystem.test.level", "debug");
} catch (Exception e) {
    System.out.println("exception via gump: "+e);
    e.printStackTrace();
    System.out.println("log: "+logger.getLog());
}
    }
 
Example #4
Source File: StringResourceLoaderTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void  testSimpleTemplate()
        throws Exception
{
    StringResourceLoader.getRepository().putStringResource("simpletemplate.vm", "This is a test for ${foo}");

    Template template = RuntimeSingleton.getTemplate(getFileName(null, "simpletemplate", TMPL_FILE_EXT));

    FileOutputStream fos =
        new FileOutputStream (
            getFileName(RESULTS_DIR, "simpletemplate", RESULT_FILE_EXT));

    Writer writer = new BufferedWriter(new OutputStreamWriter(fos));

    VelocityContext context = new VelocityContext();
    context.put("foo", "a foo object");

    template.merge(context, writer);
    writer.flush();
    writer.close();

    if (!isMatch(RESULTS_DIR, COMPARE_DIR, "simpletemplate",
                    RESULT_FILE_EXT, CMP_FILE_EXT))
    {
        fail("Output incorrect.");
    }
}
 
Example #5
Source File: StringResourceLoaderTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void setUp()
        throws Exception
{
    assureResultsDirectoryExists(RESULTS_DIR);

    Velocity.reset();

    Velocity.setProperty(Velocity.RESOURCE_LOADERS, "string");
    Velocity.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
    Velocity.addProperty("string.resource.loader.modificationCheckInterval", "1");

    // Silence the logger.
    Velocity.setProperty(Velocity.RUNTIME_LOG_INSTANCE, new TestLogger());

    Velocity.init();
}
 
Example #6
Source File: BaseTestCase.java    From velocity-tools with Apache License 2.0 6 votes vote down vote up
protected void setUp() throws Exception
{
    engine = new VelocityEngine();

    //by default, make the engine's log output go to the test-report
    log = new MockLogger(false, false);
    log.setEnabledLevel(MockLogger.LOG_LEVEL_INFO);
    engine.setProperty(RuntimeConstants.RUNTIME_LOG_INSTANCE, log);

    // use string resource loader by default, instead of file
    engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "file,string");
    engine.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
    engine.addProperty("string.resource.loader.repository.name", stringRepoName);
    engine.addProperty("string.resource.loader.repository.static", "false");

    setUpEngine(engine);

    context = new VelocityContext();
    setUpContext(context);
}
 
Example #7
Source File: MacroAutoReloadTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
protected void setUp() throws Exception
{
    // always copy macros library before modifying it, to ensure successive tests will pass
    Files.copy(FileSystems.getDefault().getPath(RELOAD_TEMPLATE_PATH + "/macros.vtl"), FileSystems.getDefault().getPath(RELOAD_TEMPLATE_PATH + "/macros2.vtl"), StandardCopyOption.REPLACE_EXISTING);

    engine = new VelocityEngine();

    //by default, make the engine's log output go to the test-report
    log = new TestLogger(false, false);
    engine.setProperty(RuntimeConstants.RUNTIME_LOG_INSTANCE, log);

    // use file resource loader
    engine.setProperty(RuntimeConstants.RESOURCE_LOADERS, "file,string");
    engine.addProperty("file.resource.loader.path", RELOAD_TEMPLATE_PATH);
    engine.addProperty("velocimacro.library", "macros2.vtl");
    engine.addProperty("velocimacro.library.autoreload", "true");
    engine.addProperty("file.resource.loader.cache", "false");
    engine.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
    engine.addProperty("string.resource.loader.repository.name", "stringRepo");
    engine.addProperty("string.resource.loader.repository.static", "false");
    context = new VelocityContext();
}
 
Example #8
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 #9
Source File: StringResourceLoaderRepositoryTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
protected VelocityEngine newStringEngine(String repoName, boolean isStatic)
{
    VelocityEngine engine = new VelocityEngine();
    TestLogger logger = new TestLogger();
    engine.setProperty(Velocity.RESOURCE_LOADERS, "string");
    engine.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
    if (repoName != null)
    {
        engine.addProperty("string.resource.loader.repository.name", repoName);
    }
    if (!isStatic)
    {
        engine.addProperty("string.resource.loader.repository.static", "false");
    }
    engine.addProperty("string.resource.loader.modificationCheckInterval", "1");
    engine.setProperty(Velocity.RUNTIME_LOG_INSTANCE, logger);
    return engine;
}
 
Example #10
Source File: StringResourceLoaderRepositoryTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
protected StringResourceRepository getRepo(String name, VelocityEngine engine)
{
    if (engine == null)
    {
        if (name == null)
        {
            return StringResourceLoader.getRepository();
        }
        else
        {
            return StringResourceLoader.getRepository(name);
        }
    }
    else
    {
        if (name == null)
        {
            return (StringResourceRepository)engine.getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
        }
        else
        {
            return (StringResourceRepository)engine.getApplicationAttribute(name);
        }
    }
}
 
Example #11
Source File: BaseTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
protected VelocityEngine createEngine()
{
    VelocityEngine ret = new VelocityEngine();
    ret.setProperty(RuntimeConstants.RUNTIME_LOG_INSTANCE, log);

    // use string resource loader by default, instead of file
    ret.setProperty(RuntimeConstants.RESOURCE_LOADERS, "file,string");
    ret.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
    ret.addProperty("string.resource.loader.repository.name", stringRepoName);
    ret.addProperty("string.resource.loader.repository.static", "false");

    setUpEngine(ret);
    return ret;
}
 
Example #12
Source File: ResourceExistsTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void testStringResourceExists() throws Exception
    {
try {
        assertFalse(velocity.resourceExists("foo.vm"));
        StringResourceLoader.getRepository().putStringResource("foo.vm", "Make it so!");
        assertTrue(velocity.resourceExists("foo.vm"));
} catch (Exception e) {
    System.out.println("exception via gump: "+e);
    e.printStackTrace();
    System.out.println("log: "+logger.getLog());
}
    }
 
Example #13
Source File: Velocity702TestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void setUpEngine(VelocityEngine engine)
{
    engine.setProperty(RuntimeConstants.RESOURCE_LOADERS, "high,low");
    engine.addProperty("high.resource.loader.class", StringResourceLoader.class.getName());
    engine.addProperty("high.resource.loader.cache", "false");
    engine.addProperty("high.resource.loader.repository.name", "high");
    engine.addProperty("high.resource.loader.repository.static", "false");
    engine.addProperty("high.resource.loader.modificationCheckInterval", "1");
    engine.addProperty("low.resource.loader.class", StringResourceLoader.class.getName());
    engine.addProperty("low.resource.loader.cache", "true");
    engine.addProperty("low.resource.loader.repository.name", "low");
    engine.addProperty("low.resource.loader.repository.static", "false");
    engine.addProperty("low.resource.loader.modificationCheckInterval", "1");
    engine.init();
}
 
Example #14
Source File: Velocity747TestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
protected void setUp() throws Exception
{
    Properties props = new Properties();
    /* The props file contains *spaces* at the end of the line:
     *   velocimacro.permissions.allow.inline.local.scope = true
     * which caused the initial problem
     */
    props.load(new FileReader(TEST_COMPARE_DIR + "/issues/velocity-747/vel.props"));
    props.setProperty("file.resource.loader.path", TEST_COMPARE_DIR + "/issues/velocity-747/");
    engine1 = new VelocityEngine(props);

    //by default, make the engine's log output go to the test-report
    log = new TestLogger(false, false);
    engine1.setProperty(RuntimeConstants.RUNTIME_LOG_INSTANCE, log);

    engine2 = new VelocityEngine();
    engine2.setProperty(RuntimeConstants.RESOURCE_LOADERS, "file,string");
    engine2.addProperty("file.resource.loader.path", TEST_COMPARE_DIR + "/issues/velocity-747/");
    engine2.addProperty("file.resource.loader.cache", "true");
    engine2.addProperty("file.resource.loader.modificationCheckInterval", "-1");
    engine2.addProperty("velocimacro.permissions.allow.inline.local.scope", "true");
    engine2.addProperty("velocimacro.max.depth", "-1");
    engine2.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
    engine2.addProperty("string.resource.loader.repository.name", "stringRepo");
    engine2.addProperty("string.resource.loader.repository.static", "false");
    log = new TestLogger(false, false);
    engine2.setProperty(RuntimeConstants.RUNTIME_LOG_INSTANCE, log);
}
 
Example #15
Source File: StringResourceLoaderRepositoryTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void testPreCreatedStaticRepo() throws Exception
{
    VelocityEngine engine = newStringEngine("my.repo", true);
    MyRepo repo = new MyRepo();
    repo.put("bar", "This is NOT $bar");
    StringResourceLoader.setRepository("my.repo", repo);

    String out = render(engine.getTemplate("bar"));
    assertEquals(out, "This is NOT horrible!");
}
 
Example #16
Source File: VelocityViewTag.java    From velocity-tools with Apache License 2.0 5 votes vote down vote up
public StringResourceRepository getRepository()
{
    if (this.repository == null)
    {
        setRepository(StringResourceLoader.getRepository());
    }
    return this.repository;
}
 
Example #17
Source File: TemplateEngine.java    From gp2srv with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Properties getConfigurationAsProperties() {
	Properties result = new Properties();

	result.setProperty(RuntimeConstants.RESOURCE_LOADER, "string");
	result.setProperty(RuntimeConstants.VM_LIBRARY_AUTORELOAD, "false");
	result.setProperty(RuntimeConstants.VM_LIBRARY, RuntimeConstants.VM_LIBRARY_DEFAULT);
	result.setProperty("string.resource.loader.class", StringResourceLoader.class.getName());
	result.setProperty("string.resource.loader.modificationCheckInterval", "0");
	result.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, SystemLogChute.class.getName());

	return result;
}
 
Example #18
Source File: TemplateEngine.java    From gp2srv with GNU Lesser General Public License v3.0 5 votes vote down vote up
private StringResourceRepository initStringResourceRepository(Map<String, String> pathToTempalteMapping) throws IOException {
	StringResourceRepository result = new StringResourceRepositoryImpl();
	StringResourceLoader.setRepository(StringResourceLoader.REPOSITORY_NAME_DEFAULT, result);
	registerResource(result, RuntimeConstants.VM_LIBRARY_DEFAULT, RuntimeConstants.VM_LIBRARY_DEFAULT);
	for (Map.Entry<String, String> pathToTempalteMappingItem : pathToTempalteMapping.entrySet()) {
		registerResource(result, pathToTempalteMappingItem.getKey(), pathToTempalteMappingItem.getValue());
	}
	return result;
}
 
Example #19
Source File: CodeGeneratorContext.java    From sundrio with Apache License 2.0 5 votes vote down vote up
public CodeGeneratorContext(VelocityEngine velocityEngine, VelocityContext velocityContext) {
    this.velocityEngine = velocityEngine;
    this.velocityContext = velocityContext;

    this.velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "string");
    this.velocityEngine.setProperty("string.resource.loader.class", StringResourceLoader.class.getName());
    //We are going to use shading so we need to make sure that the following configuration will be shade friendly...
    this.velocityEngine.setProperty(RuntimeConstants.RESOURCE_MANAGER_CLASS, ResourceManagerImpl.class.getName());
    this.velocityEngine.setProperty(RuntimeConstants.RESOURCE_MANAGER_CACHE_CLASS, ResourceCacheImpl.class.getName());
    this.velocityEngine.setProperty(RuntimeConstants.PARSER_POOL_CLASS, ParserPoolImpl.class.getName());
    this.velocityEngine.setProperty(RuntimeConstants.UBERSPECT_CLASSNAME, UberspectImpl.class.getName());
    this.velocityEngine.setProperty("runtime.log.logsystem.class", SystemLogChute.class.getName());

    ClassLoader current = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(VelocityEngine.class.getClassLoader());
        this.velocityEngine.init();

        //Load standard directives
        this.velocityEngine.loadDirective(ClassDirective.class.getCanonicalName());
        this.velocityEngine.loadDirective(MethodDirective.class.getCanonicalName());
        this.velocityEngine.loadDirective(FieldDirective.class.getCanonicalName());
        //Load utility directives
        this.velocityEngine.loadDirective(PluralizeDirective.class.getCanonicalName());
        this.velocityEngine.loadDirective(SingularizeDirective.class.getCanonicalName());
    } finally {
        Thread.currentThread().setContextClassLoader(current);
    }
}
 
Example #20
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 #21
Source File: VelocityAlertTemplateEngine.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Override
public void init(Config config) {
    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.init();

    stringResourceRepository = (StringResourceRepository) engine.getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
    policyDefinitionRepository = new HashMap<>();
}