Search in sources :

Example 11 with ITemplateResource

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());
}
Also used : Context(org.thymeleaf.context.Context) ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) TemplateEngine(org.thymeleaf.TemplateEngine) IEngineConfiguration(org.thymeleaf.IEngineConfiguration) DecoupledTemplateLogic(org.thymeleaf.templateparser.markup.decoupled.DecoupledTemplateLogic) TemplateResolution(org.thymeleaf.templateresolver.TemplateResolution) ITemplateResource(org.thymeleaf.templateresource.ITemplateResource)

Example 12 with ITemplateResource

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();
}
Also used : ITemplateResource(org.thymeleaf.templateresource.ITemplateResource)

Aggregations

ITemplateResource (org.thymeleaf.templateresource.ITemplateResource)12 TemplateResolution (org.thymeleaf.templateresolver.TemplateResolution)6 Map (java.util.Map)2 TreeSet (java.util.TreeSet)2 Resource (org.apache.sling.api.resource.Resource)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 IEngineConfiguration (org.thymeleaf.IEngineConfiguration)2 TemplateCacheKey (org.thymeleaf.cache.TemplateCacheKey)2 TemplateMode (org.thymeleaf.templatemode.TemplateMode)2 ITemplateParser (org.thymeleaf.templateparser.ITemplateParser)2 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 Properties (java.util.Properties)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 PathBuilder (org.apache.sling.api.resource.path.PathBuilder)1 SlingContext (org.apache.sling.scripting.thymeleaf.SlingContext)1 Test (org.junit.Test)1