use of org.thymeleaf.templatemode.TemplateMode in project sling by apache.
the class TemplateManager method parseString.
public TemplateModel parseString(final TemplateData ownerTemplateData, final String template, final int lineOffset, final int colOffset, final TemplateMode templateMode, final boolean useCache) {
Validate.notNull(ownerTemplateData, "Owner template cannot be null");
Validate.notNull(template, "Template cannot be null");
// NOTE selectors cannot be specified when parsing a nested template
// templateMode CAN be null (if we are using the owner's)
final String ownerTemplate = ownerTemplateData.getTemplate();
final TemplateMode definitiveTemplateMode = (templateMode != null ? templateMode : ownerTemplateData.getTemplateMode());
final TemplateCacheKey cacheKey = useCache ? new TemplateCacheKey(ownerTemplate, template, null, lineOffset, colOffset, definitiveTemplateMode, // template resolution attributes do not affect string fragments: no resolution!
null) : null;
/*
* First look at the cache - it might be already cached
*/
if (useCache && this.templateCache != null) {
final TemplateModel cached = this.templateCache.get(cacheKey);
if (cached != null) {
return cached;
}
}
/*
* Compute the cache validity. In order for a String fragment to be cacheable, we will have to have
* specified the 'useCache' parameter as true, and the owner template must be cacheable
*/
final ICacheEntryValidity cacheValidity = (useCache && ownerTemplateData.getValidity().isCacheable() ? AlwaysValidCacheEntryValidity.INSTANCE : NonCacheableCacheEntryValidity.INSTANCE);
/*
* Build the TemplateData
*
* NOTE how, by default, we are using the owner's TemplateData. And even if the template mode changes
* and we need to create a new TemplateData object, we will keep the original name and resource.
* This is because we want the elements inside the fragment to me reported as belonging to the
* container template, not to the fragment String considered as a fragment in its own (which
* wouldn't make sense)
*/
final TemplateData templateData = (templateMode == null ? // No change in Template Mode -> simply use the owner's template data
ownerTemplateData : // Template Mode changed -> new TemplateData, very similar but different template mode
new TemplateData(ownerTemplateData.getTemplate(), ownerTemplateData.getTemplateSelectors(), ownerTemplateData.getTemplateResource(), templateMode, cacheValidity));
/*
* Create the Template Handler that will be in charge of building the TemplateModel
*
* NOTE how we are using the owner's TemplateData and not a new one created for this fragment, because
* we want the elements inside the fragment to me reported as belonging to the container template,
* not to the fragment String considered as a fragment in its own (which wouldn't make sense)
*/
final ModelBuilderTemplateHandler builderHandler = new ModelBuilderTemplateHandler(this.configuration, templateData);
/*
* PROCESS THE TEMPLATE
*/
final ITemplateParser parser = getParserForTemplateMode(templateData.getTemplateMode());
// NO RESOURCE is sent to the parser, in this case. We simply pass the String template
parser.parseString(this.configuration, ownerTemplate, template, lineOffset, colOffset, definitiveTemplateMode, builderHandler);
final TemplateModel parsedTemplate = builderHandler.getModel();
/*
* Cache the template if it is cacheable
*/
if (useCache && this.templateCache != null) {
if (cacheValidity.isCacheable()) {
this.templateCache.put(cacheKey, parsedTemplate);
}
}
return parsedTemplate;
}
use of org.thymeleaf.templatemode.TemplateMode in project sling by apache.
the class TemplateManager method parseAndProcessThrottled.
public ThrottledTemplateProcessor parseAndProcessThrottled(final TemplateSpec templateSpec, final IContext context) {
Validate.notNull(templateSpec, "Template Specification cannot be null");
Validate.notNull(context, "Context 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);
/*
* Instantiate the throttling artifacts
*/
final TemplateFlowController flowController = new TemplateFlowController();
final ThrottledTemplateWriter throttledTemplateWriter = new ThrottledTemplateWriter(template, flowController);
/*
* 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();
processorTemplateHandler.setFlowController(flowController);
final ITemplateHandler processingHandlerChain = createTemplateProcessingHandlerChain(engineContext, true, true, processorTemplateHandler, throttledTemplateWriter);
/*
* Return the throttled template processor
*/
return new ThrottledTemplateProcessor(templateSpec, engineContext, cached, processingHandlerChain, processorTemplateHandler, flowController, throttledTemplateWriter);
}
}
/*
* 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();
processorTemplateHandler.setFlowController(flowController);
final ITemplateHandler processingHandlerChain = createTemplateProcessingHandlerChain(engineContext, true, true, processorTemplateHandler, throttledTemplateWriter);
/*
* Obtain the parser
*/
final ITemplateParser parser = getParserForTemplateMode(engineContext.getTemplateMode());
/*
* Parse the template into a TemplateModel. Even if we are not using the cache, throttled template processings
* will always be processed first into a TemplateModel, so that throttling can then be applied on an
* already-in-memory sequence of events
*/
final ModelBuilderTemplateHandler builderHandler = new ModelBuilderTemplateHandler(this.configuration, templateData);
parser.parseStandalone(this.configuration, null, template, templateSelectors, templateData.getTemplateResource(), engineContext.getTemplateMode(), templateResolution.getUseDecoupledLogic(), builderHandler);
final TemplateModel templateModel = builderHandler.getModel();
/*
* If cache is active, put the cached TemplateModel into cache
*/
if (templateResolution.getValidity().isCacheable() && this.templateCache != null) {
// Put the new template into cache
this.templateCache.put(cacheKey, templateModel);
}
/*
* Return the throttled template processor
*/
return new ThrottledTemplateProcessor(templateSpec, engineContext, templateModel, processingHandlerChain, processorTemplateHandler, flowController, throttledTemplateWriter);
}
use of org.thymeleaf.templatemode.TemplateMode in project sling by apache.
the class PatternTemplateModeProviderIT method provideTemplateMode_fall_through.
@Test
public void provideTemplateMode_fall_through() throws Exception {
final Resource resource = mockResource("foohtml");
final TemplateMode templateMode = templateModeProvider.provideTemplateMode(resource);
assertThat(templateMode, is(nullValue()));
}
use of org.thymeleaf.templatemode.TemplateMode in project sling by apache.
the class PatternTemplateModeProviderIT method provideTemplateMode_CSS.
@Test
public void provideTemplateMode_CSS() throws Exception {
final Resource resource = mockResource("/apps/thymeleaf/assets/foo.css");
final TemplateMode templateMode = templateModeProvider.provideTemplateMode(resource);
assertThat(templateMode, is(TemplateMode.CSS));
}
use of org.thymeleaf.templatemode.TemplateMode in project sling by apache.
the class PatternTemplateModeProviderIT method provideTemplateMode_JAVASCRIPT.
@Test
public void provideTemplateMode_JAVASCRIPT() throws Exception {
final Resource resource = mockResource("/apps/thymeleaf/assets/foo.js");
final TemplateMode templateMode = templateModeProvider.provideTemplateMode(resource);
assertThat(templateMode, is(TemplateMode.JAVASCRIPT));
}
Aggregations