use of org.thymeleaf.templatemode.TemplateMode in project sling by apache.
the class PatternTemplateModeProviderIT method provideTemplateMode_XML.
@Test
public void provideTemplateMode_XML() throws Exception {
final Resource resource = mockResource("/apps/thymeleaf/page/foo.xml");
final TemplateMode templateMode = templateModeProvider.provideTemplateMode(resource);
assertThat(templateMode, is(TemplateMode.XML));
}
use of org.thymeleaf.templatemode.TemplateMode in project sling by apache.
the class PatternTemplateModeProviderIT method provideTemplateMode_HTML.
@Test
public void provideTemplateMode_HTML() throws Exception {
final Resource resource = mockResource("/apps/thymeleaf/page/foo.html");
final TemplateMode templateMode = templateModeProvider.provideTemplateMode(resource);
assertThat(templateMode, is(TemplateMode.HTML));
}
use of org.thymeleaf.templatemode.TemplateMode in project sling by apache.
the class PatternTemplateModeProviderIT method provideTemplateMode_TEXT.
@Test
public void provideTemplateMode_TEXT() throws Exception {
final Resource resource = mockResource("/apps/thymeleaf/text/foo.txt");
final TemplateMode templateMode = templateModeProvider.provideTemplateMode(resource);
assertThat(templateMode, is(TemplateMode.TEXT));
}
use of org.thymeleaf.templatemode.TemplateMode in project sling by apache.
the class TemplateManager method parseAndProcess.
/*
* -------------------------
* PARSE-AND-PROCESS methods
* -------------------------
*
* These methods perform the whole cycle of a template's processing: resolving, parsing and processing.
* This is only meant to be called from the TemplateEngine
*/
public void parseAndProcess(final TemplateSpec templateSpec, final IContext context, final Writer writer) {
Validate.notNull(templateSpec, "Template Specification cannot be null");
Validate.notNull(context, "Context cannot be null");
Validate.notNull(writer, "Writer cannot be null");
// TemplateSpec will already have validated its contents, so need to do it here (template selectors,
// resolution attributes, etc.)
final String template = templateSpec.getTemplate();
final Set<String> templateSelectors = templateSpec.getTemplateSelectors();
final TemplateMode templateMode = templateSpec.getTemplateMode();
final Map<String, Object> templateResolutionAttributes = templateSpec.getTemplateResolutionAttributes();
final TemplateCacheKey cacheKey = new TemplateCacheKey(// ownerTemplate
null, template, templateSelectors, // lineOffset, colOffset
0, // lineOffset, colOffset
0, templateMode, templateResolutionAttributes);
/*
* First look at the cache - it might be already cached
*/
if (this.templateCache != null) {
final TemplateModel cached = this.templateCache.get(cacheKey);
if (cached != null) {
final IEngineContext engineContext = EngineContextManager.prepareEngineContext(this.configuration, cached.getTemplateData(), templateResolutionAttributes, context);
/*
* Create the handler chain to process the data.
* This is PARSE + PROCESS, so its called from the TemplateEngine, and the only case in which we should apply
* both pre-processors and post-processors (besides creating a last output-to-writer step)
*/
final ProcessorTemplateHandler processorTemplateHandler = new ProcessorTemplateHandler();
final ITemplateHandler processingHandlerChain = createTemplateProcessingHandlerChain(engineContext, true, true, processorTemplateHandler, writer);
cached.process(processingHandlerChain);
EngineContextManager.disposeEngineContext(engineContext);
return;
}
}
/*
* Resolve the template
*/
final TemplateResolution templateResolution = resolveTemplate(this.configuration, context, null, template, templateResolutionAttributes, true);
/*
* Build the TemplateData object
*/
final TemplateData templateData = buildTemplateData(templateResolution, template, templateSelectors, templateMode, true);
/*
* Prepare the context instance that corresponds to this execution of the template engine
*/
final IEngineContext engineContext = EngineContextManager.prepareEngineContext(this.configuration, templateData, templateResolutionAttributes, context);
/*
* Create the handler chain to process the data.
* This is PARSE + PROCESS, so its called from the TemplateEngine, and the only case in which we should apply
* both pre-processors and post-processors (besides creating a last output-to-writer step)
*/
final ProcessorTemplateHandler processorTemplateHandler = new ProcessorTemplateHandler();
final ITemplateHandler processingHandlerChain = createTemplateProcessingHandlerChain(engineContext, true, true, processorTemplateHandler, writer);
/*
* Obtain the parser
*/
final ITemplateParser parser = getParserForTemplateMode(engineContext.getTemplateMode());
/*
* If the resolved template is cacheable, so we will first read it as an object, cache it, and then process it
*/
if (templateResolution.getValidity().isCacheable() && this.templateCache != null) {
// Create the handler chain to create the Template object
final ModelBuilderTemplateHandler builderHandler = new ModelBuilderTemplateHandler(this.configuration, templateData);
// Process the template into a TemplateModel
parser.parseStandalone(this.configuration, null, template, templateSelectors, templateData.getTemplateResource(), engineContext.getTemplateMode(), templateResolution.getUseDecoupledLogic(), builderHandler);
// Obtain the TemplateModel
final TemplateModel templateModel = builderHandler.getModel();
// Put the new template into cache
this.templateCache.put(cacheKey, templateModel);
// Process the read (+cached) template itself
templateModel.process(processingHandlerChain);
} else {
// Process the template, which is not cacheable (so no worry about caching)
parser.parseStandalone(this.configuration, null, template, templateSelectors, templateData.getTemplateResource(), engineContext.getTemplateMode(), templateResolution.getUseDecoupledLogic(), processingHandlerChain);
}
/*
* Dispose the engine context now that processing has been done
*/
EngineContextManager.disposeEngineContext(engineContext);
}
use of org.thymeleaf.templatemode.TemplateMode in project sling by apache.
the class TemplateManager method buildTemplateData.
private static TemplateData buildTemplateData(final TemplateResolution templateResolution, final String template, final Set<String> templateSelectors, final TemplateMode templateMode, final boolean useCache) {
final TemplateMode definitiveTemplateMode = (templateMode == null ? templateResolution.getTemplateMode() : templateMode);
final ICacheEntryValidity definitiveCacheEntryValidity = (useCache ? templateResolution.getValidity() : NonCacheableCacheEntryValidity.INSTANCE);
return new TemplateData(template, templateSelectors, templateResolution.getTemplateResource(), definitiveTemplateMode, definitiveCacheEntryValidity);
}
Aggregations