use of freemarker.cache.TemplateLoader 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;
}
use of freemarker.cache.TemplateLoader in project moco by dreamhead.
the class TemplateResourceReader method createTemplate.
private Template createTemplate(final MessageContent messageContent) throws IOException {
TemplateLoader templateLoader = createTemplateLoader(messageContent);
Configuration cfg = createConfiguration(templateLoader, messageContent.getCharset());
return cfg.getTemplate(TEMPLATE_NAME);
}
use of freemarker.cache.TemplateLoader in project jforum2 by rafaelsteil.
the class JForumBaseServlet method init.
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
String appPath = config.getServletContext().getRealPath("");
debug = "true".equals(config.getInitParameter("development"));
DOMConfigurator.configure(appPath + "/WEB-INF/log4j.xml");
logger.info("Starting JForum. Debug mode is " + debug);
ConfigLoader.startSystemglobals(appPath);
ConfigLoader.startCacheEngine();
// Configure the template engine
Configuration templateCfg = new Configuration();
templateCfg.setTemplateUpdateDelay(2);
templateCfg.setSetting("number_format", "#");
templateCfg.setSharedVariable("startupTime", new Long(new Date().getTime()));
// Create the default template loader
String defaultPath = SystemGlobals.getApplicationPath() + "/templates";
FileTemplateLoader defaultLoader = new FileTemplateLoader(new File(defaultPath));
String extraTemplatePath = SystemGlobals.getValue(ConfigKeys.FREEMARKER_EXTRA_TEMPLATE_PATH);
if (StringUtils.isNotBlank(extraTemplatePath)) {
// An extra template path is configured, we need a MultiTemplateLoader
FileTemplateLoader extraLoader = new FileTemplateLoader(new File(extraTemplatePath));
TemplateLoader[] loaders = new TemplateLoader[] { extraLoader, defaultLoader };
MultiTemplateLoader multiLoader = new MultiTemplateLoader(loaders);
templateCfg.setTemplateLoader(multiLoader);
} else {
// An extra template path is not configured, we only need the default loader
templateCfg.setTemplateLoader(defaultLoader);
}
ModulesRepository.init(SystemGlobals.getValue(ConfigKeys.CONFIG_DIR));
this.loadConfigStuff();
if (!this.debug) {
templateCfg.setTemplateUpdateDelay(3600);
}
JForumExecutionContext.setTemplateConfig(templateCfg);
} catch (Exception e) {
throw new ForumStartupException("Error while starting JForum", e);
}
}
Aggregations