use of freemarker.cache.WebappTemplateLoader in project freemarker by apache.
the class InitParamParser method createTemplateLoader.
static TemplateLoader createTemplateLoader(String templatePath, Configuration cfg, Class classLoaderClass, ServletContext srvCtx) throws IOException {
final int settingAssignmentsStart = findTemplatePathSettingAssignmentsStart(templatePath);
String pureTemplatePath = (settingAssignmentsStart == -1 ? templatePath : templatePath.substring(0, settingAssignmentsStart)).trim();
final TemplateLoader templateLoader;
if (pureTemplatePath.startsWith(TEMPLATE_PATH_PREFIX_CLASS)) {
String packagePath = pureTemplatePath.substring(TEMPLATE_PATH_PREFIX_CLASS.length());
packagePath = normalizeToAbsolutePackagePath(packagePath);
templateLoader = new ClassTemplateLoader(classLoaderClass, packagePath);
} else if (pureTemplatePath.startsWith(TEMPLATE_PATH_PREFIX_CLASSPATH)) {
// To be similar to Spring resource paths, we don't require "//":
String packagePath = pureTemplatePath.substring(TEMPLATE_PATH_PREFIX_CLASSPATH.length());
packagePath = normalizeToAbsolutePackagePath(packagePath);
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
LOG.warn("No Thread Context Class Loader was found. Falling back to the class loader of " + classLoaderClass.getName() + ".");
classLoader = classLoaderClass.getClassLoader();
}
templateLoader = new ClassTemplateLoader(classLoader, packagePath);
} else if (pureTemplatePath.startsWith(TEMPLATE_PATH_PREFIX_FILE)) {
String filePath = pureTemplatePath.substring(TEMPLATE_PATH_PREFIX_FILE.length());
templateLoader = new FileTemplateLoader(new File(filePath));
} else if (pureTemplatePath.startsWith("[") && cfg.getIncompatibleImprovements().intValue() >= _TemplateAPI.VERSION_INT_2_3_22) {
if (!pureTemplatePath.endsWith("]")) {
// B.C. constraint: Can't throw any checked exceptions.
throw new TemplatePathParsingException("Failed to parse template path; closing \"]\" is missing.");
}
String commaSepItems = pureTemplatePath.substring(1, pureTemplatePath.length() - 1).trim();
List listItems = parseCommaSeparatedTemplatePaths(commaSepItems);
TemplateLoader[] templateLoaders = new TemplateLoader[listItems.size()];
for (int i = 0; i < listItems.size(); i++) {
String pathItem = (String) listItems.get(i);
templateLoaders[i] = createTemplateLoader(pathItem, cfg, classLoaderClass, srvCtx);
}
templateLoader = new MultiTemplateLoader(templateLoaders);
} else if (pureTemplatePath.startsWith("{") && cfg.getIncompatibleImprovements().intValue() >= _TemplateAPI.VERSION_INT_2_3_22) {
throw new TemplatePathParsingException("Template paths starting with \"{\" are reseved for future purposes");
} else {
templateLoader = new WebappTemplateLoader(srvCtx, pureTemplatePath);
}
if (settingAssignmentsStart != -1) {
try {
int nextPos = _ObjectBuilderSettingEvaluator.configureBean(templatePath, templatePath.indexOf('(', settingAssignmentsStart) + 1, templateLoader, _SettingEvaluationEnvironment.getCurrent());
if (nextPos != templatePath.length()) {
throw new TemplatePathParsingException("Template path should end after the setting list in: " + templatePath);
}
} catch (Exception e) {
throw new TemplatePathParsingException("Failed to set properties in: " + templatePath, e);
}
}
return templateLoader;
}
use of freemarker.cache.WebappTemplateLoader in project freemarker by apache.
the class TemplateNotFoundMessageTest method testMultiTemplateLoader.
@Test
public void testMultiTemplateLoader() throws IOException {
final String errMsg = failWith(new MultiTemplateLoader(new TemplateLoader[] { new WebappTemplateLoader(new MockServletContext(), "WEB-INF/templates"), new ClassTemplateLoader(this.getClass(), "foo/bar") }));
showErrorMessage(errMsg);
assertThat(errMsg, containsString("MultiTemplateLoader"));
assertThat(errMsg, containsString("WebappTemplateLoader"));
assertThat(errMsg, containsString("MyApp"));
assertThat(errMsg, containsString("WEB-INF/templates"));
assertThat(errMsg, containsString("ClassTemplateLoader"));
assertThat(errMsg, containsString("foo/bar"));
}
use of freemarker.cache.WebappTemplateLoader in project drill by axbaretto.
the class DrillRestServer method getFreemarkerConfiguration.
/**
* Creates freemarker configuration settings,
* default output format to trigger auto-escaping policy
* and template loaders.
*
* @param servletContext servlet context
* @return freemarker configuration settings
*/
private Configuration getFreemarkerConfiguration(ServletContext servletContext) {
Configuration configuration = new Configuration(Configuration.VERSION_2_3_26);
configuration.setOutputFormat(HTMLOutputFormat.INSTANCE);
List<TemplateLoader> loaders = new ArrayList<>();
loaders.add(new WebappTemplateLoader(servletContext));
loaders.add(new ClassTemplateLoader(DrillRestServer.class, "/"));
try {
loaders.add(new FileTemplateLoader(new File("/")));
} catch (IOException e) {
logger.error("Could not set up file template loader.", e);
}
configuration.setTemplateLoader(new MultiTemplateLoader(loaders.toArray(new TemplateLoader[loaders.size()])));
return configuration;
}
use of freemarker.cache.WebappTemplateLoader in project freemarker by apache.
the class TemplateNotFoundMessageTest method testWebappTemplateLoader.
@Test
public void testWebappTemplateLoader() throws IOException {
final String errMsg = failWith(new WebappTemplateLoader(new MockServletContext(), "WEB-INF/templates"));
showErrorMessage(errMsg);
assertThat(errMsg, containsString("WebappTemplateLoader"));
assertThat(errMsg, containsString("MyApp"));
assertThat(errMsg, containsString("WEB-INF/templates"));
}
use of freemarker.cache.WebappTemplateLoader in project drill by apache.
the class DrillRestServer method getFreemarkerConfiguration.
/**
* Creates freemarker configuration settings,
* default output format to trigger auto-escaping policy
* and template loaders.
*
* @param servletContext servlet context
* @return freemarker configuration settings
*/
private Configuration getFreemarkerConfiguration(ServletContext servletContext) {
Configuration configuration = new Configuration(Configuration.VERSION_2_3_26);
configuration.setOutputFormat(HTMLOutputFormat.INSTANCE);
List<TemplateLoader> loaders = new ArrayList<>();
loaders.add(new WebappTemplateLoader(servletContext));
loaders.add(new ClassTemplateLoader(DrillRestServer.class, "/"));
try {
loaders.add(new FileTemplateLoader(new File("/")));
} catch (IOException e) {
logger.error("Could not set up file template loader.", e);
}
configuration.setTemplateLoader(new MultiTemplateLoader(loaders.toArray(new TemplateLoader[loaders.size()])));
return configuration;
}
Aggregations