use of org.thymeleaf.templateresource.ITemplateResource in project thymeleaf-tests by thymeleaf.
the class ParsingDecoupled01Test method testParsingDecoupled.
private static void testParsingDecoupled(final String decoupledTemplate, final TemplateMode templateMode, final String expectedResult) throws Exception {
final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("templateparser/markup/");
templateResolver.setSuffix(templateMode == TemplateMode.HTML ? ".html" : ".xml");
templateResolver.setTemplateMode(templateMode);
final TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
// We only to this in order to initialize the engine
templateEngine.process("parsingdecoupled", new Context());
final IEngineConfiguration configuration = templateEngine.getConfiguration();
final TemplateResolution templateResolution = templateResolver.resolveTemplate(configuration, null, decoupledTemplate, null);
final ITemplateResource templateResource = templateResolution.getTemplateResource();
final DecoupledTemplateLogic decoupledTemplateLogic = DecoupledTemplateLogicUtils.computeDecoupledTemplateLogic(configuration, null, decoupledTemplate, null, templateResource, templateMode, (templateMode == TemplateMode.HTML ? htmlParser : xmlParser));
Assert.assertEquals(expectedResult, decoupledTemplateLogic.toString());
}
use of org.thymeleaf.templateresource.ITemplateResource in project thymeleaf by thymeleaf.
the class DecoupledTemplateLogicUtils method computeDecoupledTemplateLogic.
public static DecoupledTemplateLogic computeDecoupledTemplateLogic(final IEngineConfiguration configuration, final String ownerTemplate, final String template, final Set<String> templateSelectors, final ITemplateResource resource, final TemplateMode templateMode, final IMarkupParser parser) throws IOException, ParseException {
Validate.notNull(configuration, "Engine Configuration cannot be null");
Validate.notNull(template, "Template cannot be null");
Validate.notNull(resource, "Template Resource cannot be null");
Validate.notNull(templateMode, "Template Mode cannot be null");
final IDecoupledTemplateLogicResolver decoupledTemplateLogicResolver = configuration.getDecoupledTemplateLogicResolver();
final ITemplateResource decoupledResource = decoupledTemplateLogicResolver.resolveDecoupledTemplateLogic(configuration, ownerTemplate, template, templateSelectors, resource, templateMode);
if (!decoupledResource.exists()) {
if (logger.isTraceEnabled()) {
logger.trace("[THYMELEAF][{}] Decoupled logic for template \"{}\" could not be resolved as relative resource \"{}\". " + "This does not need to be an error, as templates may lack a corresponding decoupled logic file.", new Object[] { TemplateEngine.threadIndex(), LoggingUtils.loggifyTemplateName(template), decoupledResource.getDescription() });
}
return null;
}
if (logger.isTraceEnabled()) {
logger.trace("[THYMELEAF][{}] Decoupled logic for template \"{}\" has been resolved as relative resource \"{}\"", new Object[] { TemplateEngine.threadIndex(), LoggingUtils.loggifyTemplateName(template), decoupledResource.getDescription() });
}
/*
* The decoupled template logic resource exists, so we should parse it before the template itself, in order
* to obtain the logic to be injected on the "real" template during parsing.
*/
final DecoupledTemplateLogicBuilderMarkupHandler decoupledMarkupHandler = new DecoupledTemplateLogicBuilderMarkupHandler(template, templateMode);
parser.parse(decoupledResource.reader(), decoupledMarkupHandler);
return decoupledMarkupHandler.getDecoupledTemplateLogic();
}
Aggregations