}
}
final Configuration configuration = templateProcessingParameters.getConfiguration();
final Set<ITemplateResolver> templateResolvers = configuration.getTemplateResolvers();
TemplateResolution templateResolution = null;
InputStream templateInputStream = null;
for (final ITemplateResolver templateResolver : templateResolvers) {
templateResolution = templateResolver.resolveTemplate(templateProcessingParameters);
if (templateResolution != null) {
final String resourceName = templateResolution.getResourceName();
final IResourceResolver resourceResolver = templateResolution.getResourceResolver();
if (logger.isTraceEnabled()) {
logger.trace("[THYMELEAF][{}] Trying to resolve template \"{}\" as resource \"{}\" with resource resolver \"{}\"", new Object[] {TemplateEngine.threadIndex(), templateName, resourceName, resourceResolver.getName()});
}
templateInputStream =
resourceResolver.getResourceAsStream(templateProcessingParameters, resourceName);
if (templateInputStream == null) {
if (logger.isTraceEnabled()) {
logger.trace("[THYMELEAF][{}] Template \"{}\" could not be resolved as resource \"{}\" with resource resolver \"{}\"", new Object[] {TemplateEngine.threadIndex(), templateName, resourceName, resourceResolver.getName()});
}
} else {
if (logger.isDebugEnabled()) {
logger.debug("[THYMELEAF][{}] Template \"{}\" was correctly resolved as resource \"{}\" in mode {} with resource resolver \"{}\"", new Object[] {TemplateEngine.threadIndex(), templateName, resourceName, templateResolution.getTemplateMode(), resourceResolver.getName()});
}
break;
}
} else {
if (logger.isTraceEnabled()) {
logger.trace("[THYMELEAF][{}] Skipping template resolver \"{}\" for template \"{}\"", new Object[] {TemplateEngine.threadIndex(), templateResolver.getName(), templateName});
}
}
}
if (templateResolution == null || templateInputStream == null) {
throw new TemplateInputException(
"Error resolving template \"" + templateProcessingParameters.getTemplateName() + "\", " +
"template might not exist or might not be accessible by " +
"any of the configured Template Resolvers");
}
final String templateMode = templateResolution.getTemplateMode();
final ITemplateParser templateParser = this.parsersByTemplateMode.get(templateMode);
if (templateParser == null) {
throw new TemplateInputException(
"Template mode \"" + templateMode + "\" has not been configured");
}
if (logger.isTraceEnabled()) {
logger.trace("[THYMELEAF][{}] Starting parsing of template \"{}\"", TemplateEngine.threadIndex(), templateName);
}
final String characterEncoding = templateResolution.getCharacterEncoding();
Reader reader = null;
if (!StringUtils.isEmptyOrWhitespace(characterEncoding)) {
try {
reader = new InputStreamReader(templateInputStream, characterEncoding);
} catch (final UnsupportedEncodingException e) {
throw new TemplateInputException("Exception parsing document", e);
}
} else {
reader = new InputStreamReader(templateInputStream);
}
final Document document =
templateParser.parseTemplate(configuration, templateName, reader);
if (logger.isTraceEnabled()) {
logger.trace("[THYMELEAF][{}] Finished parsing of template \"{}\"", TemplateEngine.threadIndex(), templateName);
}
document.precompute(configuration);
final Template template =
new Template(templateName, templateResolution, document);
if (this.templateCache != null) {
if (templateResolution.getValidity().isCacheable()) {
this.templateCache.put(templateName, template);
return template.createDuplicate();
}
}