Search in sources :

Example 6 with Configuration

use of freemarker.template.Configuration in project jforum2 by rafaelsteil.

the class TestCaseUtils method init.

private void init() throws IOException {
    getRootDir();
    SystemGlobals.initGlobals(this.rootDir, this.rootDir + "/WEB-INF/config/SystemGlobals.properties");
    if (new File(SystemGlobals.getValue(ConfigKeys.INSTALLATION_CONFIG)).exists()) {
        SystemGlobals.loadAdditionalDefaults(SystemGlobals.getValue(ConfigKeys.INSTALLATION_CONFIG));
    }
    // Configure the template engine
    Configuration templateCfg = new Configuration();
    templateCfg.setDirectoryForTemplateLoading(new File(SystemGlobals.getApplicationPath() + "/templates"));
    templateCfg.setTemplateUpdateDelay(0);
    JForumExecutionContext.setTemplateConfig(templateCfg);
    I18n.load();
}
Also used : Configuration(freemarker.template.Configuration) File(java.io.File)

Example 7 with Configuration

use of freemarker.template.Configuration in project spring-framework by spring-projects.

the class FreeMarkerConfigurationFactory method createConfiguration.

/**
	 * Prepare the FreeMarker Configuration and return it.
	 * @return the FreeMarker Configuration object
	 * @throws IOException if the config file wasn't found
	 * @throws TemplateException on FreeMarker initialization failure
	 */
public Configuration createConfiguration() throws IOException, TemplateException {
    Configuration config = newConfiguration();
    Properties props = new Properties();
    // Load config file if specified.
    if (this.configLocation != null) {
        if (logger.isInfoEnabled()) {
            logger.info("Loading FreeMarker configuration from " + this.configLocation);
        }
        PropertiesLoaderUtils.fillProperties(props, this.configLocation);
    }
    // Merge local properties if specified.
    if (this.freemarkerSettings != null) {
        props.putAll(this.freemarkerSettings);
    }
    // setAllSharedVariables methods.
    if (!props.isEmpty()) {
        config.setSettings(props);
    }
    if (!CollectionUtils.isEmpty(this.freemarkerVariables)) {
        config.setAllSharedVariables(new SimpleHash(this.freemarkerVariables, config.getObjectWrapper()));
    }
    if (this.defaultEncoding != null) {
        config.setDefaultEncoding(this.defaultEncoding);
    }
    List<TemplateLoader> templateLoaders = new LinkedList<>(this.templateLoaders);
    // Register template loaders that are supposed to kick in early.
    if (this.preTemplateLoaders != null) {
        templateLoaders.addAll(this.preTemplateLoaders);
    }
    // Register default template loaders.
    if (this.templateLoaderPaths != null) {
        for (String path : this.templateLoaderPaths) {
            templateLoaders.add(getTemplateLoaderForPath(path));
        }
    }
    postProcessTemplateLoaders(templateLoaders);
    // Register template loaders that are supposed to kick in late.
    if (this.postTemplateLoaders != null) {
        templateLoaders.addAll(this.postTemplateLoaders);
    }
    TemplateLoader loader = getAggregateTemplateLoader(templateLoaders);
    if (loader != null) {
        config.setTemplateLoader(loader);
    }
    postProcessConfiguration(config);
    return config;
}
Also used : Configuration(freemarker.template.Configuration) TemplateLoader(freemarker.cache.TemplateLoader) FileTemplateLoader(freemarker.cache.FileTemplateLoader) MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) SimpleHash(freemarker.template.SimpleHash) Properties(java.util.Properties) LinkedList(java.util.LinkedList)

Example 8 with Configuration

use of freemarker.template.Configuration in project spring-framework by spring-projects.

the class FreeMarkerConfigurerTests method freeMarkerConfigurationFactoryBeanWithResourceLoaderPath.

@Test
public void freeMarkerConfigurationFactoryBeanWithResourceLoaderPath() throws Exception {
    FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean();
    fcfb.setTemplateLoaderPath("file:/mydir");
    fcfb.afterPropertiesSet();
    Configuration cfg = fcfb.getObject();
    assertTrue(cfg.getTemplateLoader() instanceof SpringTemplateLoader);
}
Also used : FreeMarkerConfigurationFactoryBean(org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean) Configuration(freemarker.template.Configuration) SpringTemplateLoader(org.springframework.ui.freemarker.SpringTemplateLoader) Test(org.junit.Test)

Example 9 with Configuration

use of freemarker.template.Configuration in project camel by apache.

the class SpringBootStarterMojo method getTemplate.

private Template getTemplate(String name) throws IOException {
    Configuration cfg = new Configuration(Configuration.getVersion());
    cfg.setTemplateLoader(new URLTemplateLoader() {

        @Override
        protected URL getURL(String name) {
            return SpringBootStarterMojo.class.getResource("/" + name);
        }
    });
    cfg.setDefaultEncoding("UTF-8");
    Template template = cfg.getTemplate(name);
    return template;
}
Also used : URLTemplateLoader(freemarker.cache.URLTemplateLoader) Configuration(freemarker.template.Configuration) URL(java.net.URL) Template(freemarker.template.Template)

Example 10 with Configuration

use of freemarker.template.Configuration in project greenDAO by greenrobot.

the class DaoGenerator method getConfiguration.

private Configuration getConfiguration(String probingTemplate) throws IOException {
    Configuration config = new Configuration(Configuration.VERSION_2_3_23);
    config.setClassForTemplateLoading(getClass(), "/");
    try {
        config.getTemplate(probingTemplate);
    } catch (TemplateNotFoundException e) {
        // When running from an IDE like IntelliJ, class loading resources may fail for some reason (Gradle is OK)
        // Working dir is module dir
        File dir = new File("src/main/resources/");
        if (!dir.exists()) {
            // Working dir is base module dir
            dir = new File("DaoGenerator/src/main/resources/");
        }
        if (dir.exists() && new File(dir, probingTemplate).exists()) {
            config.setDirectoryForTemplateLoading(dir);
            config.getTemplate(probingTemplate);
        } else {
            throw e;
        }
    }
    return config;
}
Also used : Configuration(freemarker.template.Configuration) TemplateNotFoundException(freemarker.template.TemplateNotFoundException) File(java.io.File)

Aggregations

Configuration (freemarker.template.Configuration)56 Template (freemarker.template.Template)30 Writer (java.io.Writer)17 IOException (java.io.IOException)14 StringWriter (java.io.StringWriter)14 File (java.io.File)13 HashMap (java.util.HashMap)11 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)8 TemplateException (freemarker.template.TemplateException)7 OutputStreamWriter (java.io.OutputStreamWriter)7 StringTemplateLoader (freemarker.cache.StringTemplateLoader)5 ThirdEyeAnomalyConfiguration (com.linkedin.thirdeye.anomaly.ThirdEyeAnomalyConfiguration)4 ClassTemplateLoader (freemarker.cache.ClassTemplateLoader)4 FileTemplateLoader (freemarker.cache.FileTemplateLoader)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 FileWriter (java.io.FileWriter)4 StringReader (java.io.StringReader)4 ArrayList (java.util.ArrayList)4 MultiTemplateLoader (freemarker.cache.MultiTemplateLoader)3 TemplateLoader (freemarker.cache.TemplateLoader)3