Java Code Examples for org.apache.velocity.runtime.resource.loader.FileResourceLoader

The following examples show how to use org.apache.velocity.runtime.resource.loader.FileResourceLoader. These examples are extracted from open source projects. 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
public void setUp()
        throws Exception
{

    ResourceLoader rl = new FileResourceLoader();

    // pass in an instance to Velocity
    Velocity.reset();
    Velocity.setProperty( "resource.loader", "testrl" );
    Velocity.setProperty( "testrl.resource.loader.instance", rl );
    Velocity.setProperty( "testrl.resource.loader.path", FILE_RESOURCE_LOADER_PATH );

    // actual instance of logger
    logger.on();
    Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_INSTANCE, logger);
    Velocity.setProperty("runtime.log.logsystem.test.level", "debug");

    Velocity.init();
}
 
Example 2
Source Project: LicenseScout   Source File: AbstractVelocityExporter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public final void export(final OutputResult outputResult, final ReportConfiguration reportConfiguration)
        throws Exception {
    final File outputFile = reportConfiguration.getOutputFile();
    final Charset charset = ExporterUtil.getOutputCharset(reportConfiguration);
    try (final FileWriter fileWriter = new FileWriter(outputFile, charset);
            final BufferedWriter bw = new BufferedWriter(fileWriter)) {

        Velocity.setProperty(RuntimeConstants.RESOURCE_LOADERS, "file,classpath");
        Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER + ".file.class", FileResourceLoader.class.getName());
        /*
         * NOTE: setting the path to empty is necessary because the default is "." (current
         * directory) and then using absolute path names will not work.
         */
        Velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, "");
        Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER + ".classpath.class",
                ClasspathResourceLoader.class.getName());

        Velocity.init();

        final VelocityContext context = createVelocityContext(outputResult, reportConfiguration);
        final Template template = getTemplate(reportConfiguration);

        try (final StringWriter sw = new StringWriter()) {
            if (template != null) {
                template.merge(context, sw);
            }
            bw.write(sw.getBuffer().toString());
        }
    }
}