Search in sources :

Example 11 with TemplateResolution

use of org.thymeleaf.templateresolver.TemplateResolution 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 TemplateResolution

use of org.thymeleaf.templateresolver.TemplateResolution in project thymeleaf-tests by thymeleaf.

the class TestTemplateResolver method resolveTemplate.

public TemplateResolution resolveTemplate(final TemplateProcessingParameters templateProcessingParameters) {
    final String templateName = templateProcessingParameters.getTemplateName();
    final int placeholderPos = this.template.indexOf("{%%}");
    final String result = this.template.substring(0, placeholderPos) + templateName + this.template.substring(placeholderPos + 4);
    final IResourceResolver resourceResolver = new TestResourceResolver(result);
    final TemplateResolution templateResolution = new TemplateResolution(templateName, "TEST", resourceResolver, "UTF-8", "HTML5", new NonCacheableCacheEntryValidity());
    return templateResolution;
}
Also used : IResourceResolver(org.thymeleaf.resourceresolver.IResourceResolver) TemplateResolution(org.thymeleaf.templateresolver.TemplateResolution) NonCacheableCacheEntryValidity(org.thymeleaf.cache.NonCacheableCacheEntryValidity)

Example 13 with TemplateResolution

use of org.thymeleaf.templateresolver.TemplateResolution in project thymeleaf-tests by thymeleaf.

the class SpringResourceTemplateResolverSpring3Test method testResolveTemplate.

@Test
public void testResolveTemplate() throws Exception {
    final String templateLocation = "spring321/view/test.html";
    final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring321/view/applicationContext.xml");
    final SpringResourceTemplateResolver resolver = (SpringResourceTemplateResolver) context.getBean("springResourceTemplateResolver");
    resolver.initialize();
    final String templateMode = resolver.getTemplateMode();
    Assert.assertEquals("HTML5", templateMode);
    final TemplateProcessingParameters parameters = new TemplateProcessingParameters(new Configuration(), "classpath:" + templateLocation, new Context());
    final TemplateResolution resolution = resolver.resolveTemplate(parameters);
    final IResourceResolver resourceResolver = resolution.getResourceResolver();
    final InputStream is = resourceResolver.getResourceAsStream(parameters, resolution.getResourceName());
    final String testResource = ResourceUtils.normalize(ResourceUtils.read(is, "US-ASCII"));
    final String expected = ResourceUtils.read(ClassLoaderUtils.getClassLoader(SpringResourceTemplateResolverSpring3Test.class).getResourceAsStream(templateLocation), "US-ASCII", true);
    Assert.assertEquals(expected, testResource);
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Context(org.thymeleaf.context.Context) IResourceResolver(org.thymeleaf.resourceresolver.IResourceResolver) TemplateProcessingParameters(org.thymeleaf.TemplateProcessingParameters) Configuration(org.thymeleaf.Configuration) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) TemplateResolution(org.thymeleaf.templateresolver.TemplateResolution) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 14 with TemplateResolution

use of org.thymeleaf.templateresolver.TemplateResolution in project thymeleaf-tests by thymeleaf.

the class SpringResourceTemplateResolverSpring4Test method testResolveTemplate.

@Test
public void testResolveTemplate() throws Exception {
    final String templateLocation = "spring421/view/test.html";
    final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring421/view/applicationContext.xml");
    final SpringResourceTemplateResolver resolver = (SpringResourceTemplateResolver) context.getBean("springResourceTemplateResolver");
    resolver.initialize();
    final String templateMode = resolver.getTemplateMode();
    Assert.assertEquals("HTML5", templateMode);
    final TemplateProcessingParameters parameters = new TemplateProcessingParameters(new Configuration(), "classpath:" + templateLocation, new Context());
    final TemplateResolution resolution = resolver.resolveTemplate(parameters);
    final IResourceResolver resourceResolver = resolution.getResourceResolver();
    final InputStream is = resourceResolver.getResourceAsStream(parameters, resolution.getResourceName());
    final String testResource = ResourceUtils.normalize(ResourceUtils.read(is, "US-ASCII"));
    final String expected = ResourceUtils.read(ClassLoaderUtils.getClassLoader(SpringResourceTemplateResolverSpring4Test.class).getResourceAsStream(templateLocation), "US-ASCII", true);
    Assert.assertEquals(expected, testResource);
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Context(org.thymeleaf.context.Context) IResourceResolver(org.thymeleaf.resourceresolver.IResourceResolver) TemplateProcessingParameters(org.thymeleaf.TemplateProcessingParameters) Configuration(org.thymeleaf.Configuration) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) TemplateResolution(org.thymeleaf.templateresolver.TemplateResolution) InputStream(java.io.InputStream) Test(org.junit.Test)

Aggregations

TemplateResolution (org.thymeleaf.templateresolver.TemplateResolution)14 TemplateCacheKey (org.thymeleaf.cache.TemplateCacheKey)6 TemplateMode (org.thymeleaf.templatemode.TemplateMode)6 ITemplateParser (org.thymeleaf.templateparser.ITemplateParser)6 ITemplateResource (org.thymeleaf.templateresource.ITemplateResource)6 Context (org.thymeleaf.context.Context)4 IEngineContext (org.thymeleaf.context.IEngineContext)4 Test (org.junit.Test)3 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)3 TemplateProcessingParameters (org.thymeleaf.TemplateProcessingParameters)3 NonCacheableCacheEntryValidity (org.thymeleaf.cache.NonCacheableCacheEntryValidity)3 IResourceResolver (org.thymeleaf.resourceresolver.IResourceResolver)3 InputStream (java.io.InputStream)2 TreeSet (java.util.TreeSet)2 Configuration (org.thymeleaf.Configuration)2 IEngineConfiguration (org.thymeleaf.IEngineConfiguration)2 TemplateEngine (org.thymeleaf.TemplateEngine)2 StringWriter (java.io.StringWriter)1 Resource (org.apache.sling.api.resource.Resource)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1