Search in sources :

Example 1 with ICacheEntryValidity

use of org.thymeleaf.cache.ICacheEntryValidity in project thymeleaf by thymeleaf.

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;
}
Also used : ITemplateParser(org.thymeleaf.templateparser.ITemplateParser) TemplateMode(org.thymeleaf.templatemode.TemplateMode) TemplateCacheKey(org.thymeleaf.cache.TemplateCacheKey) ICacheEntryValidity(org.thymeleaf.cache.ICacheEntryValidity)

Example 2 with ICacheEntryValidity

use of org.thymeleaf.cache.ICacheEntryValidity 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);
}
Also used : TemplateMode(org.thymeleaf.templatemode.TemplateMode) ICacheEntryValidity(org.thymeleaf.cache.ICacheEntryValidity)

Example 3 with ICacheEntryValidity

use of org.thymeleaf.cache.ICacheEntryValidity 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;
}
Also used : ITemplateParser(org.thymeleaf.templateparser.ITemplateParser) TemplateMode(org.thymeleaf.templatemode.TemplateMode) TemplateCacheKey(org.thymeleaf.cache.TemplateCacheKey) ICacheEntryValidity(org.thymeleaf.cache.ICacheEntryValidity)

Example 4 with ICacheEntryValidity

use of org.thymeleaf.cache.ICacheEntryValidity in project sling by apache.

the class SlingResourceTemplateResolver method resolveTemplate.

@Override
public TemplateResolution resolveTemplate(final IEngineConfiguration engineConfiguration, final IContext context, final String ownerTemplate, final String template, final Map<String, Object> templateResolutionAttributes) {
    logger.debug("resolving template '{}'", template);
    if (context instanceof SlingContext) {
        final SlingContext slingContext = (SlingContext) context;
        final ResourceResolver resourceResolver = slingContext.getResourceResolver();
        final Resource resource = resourceResolver.getResource(template);
        if (resource == null) {
            logger.warn("resource for template '{}' is null, not resolving template", template);
            return null;
        }
        final ITemplateResource templateResource = new SlingTemplateResource(resource);
        final boolean templateResourceExistenceVerified = false;
        final TemplateMode templateMode = templateModeProvider.provideTemplateMode(resource);
        if (templateMode == null) {
            logger.warn("template mode for template '{}' is null, not resolving template", template);
            return null;
        } else {
            logger.debug("using template mode {} for template '{}'", templateMode, template);
            final boolean useDecoupledLogic = templateMode.isMarkup() && configuration.useDecoupledLogic();
            final ICacheEntryValidity validity = NonCacheableCacheEntryValidity.INSTANCE;
            return new TemplateResolution(templateResource, templateResourceExistenceVerified, templateMode, useDecoupledLogic, validity);
        }
    } else {
        logger.error("context is not an instance of SlingContext");
        return null;
    }
}
Also used : TemplateMode(org.thymeleaf.templatemode.TemplateMode) SlingContext(org.apache.sling.scripting.thymeleaf.SlingContext) TemplateResolution(org.thymeleaf.templateresolver.TemplateResolution) ITemplateResource(org.thymeleaf.templateresource.ITemplateResource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ITemplateResource(org.thymeleaf.templateresource.ITemplateResource) Resource(org.apache.sling.api.resource.Resource) ICacheEntryValidity(org.thymeleaf.cache.ICacheEntryValidity)

Example 5 with ICacheEntryValidity

use of org.thymeleaf.cache.ICacheEntryValidity in project thymeleaf by thymeleaf.

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);
}
Also used : TemplateMode(org.thymeleaf.templatemode.TemplateMode) ICacheEntryValidity(org.thymeleaf.cache.ICacheEntryValidity)

Aggregations

ICacheEntryValidity (org.thymeleaf.cache.ICacheEntryValidity)5 TemplateMode (org.thymeleaf.templatemode.TemplateMode)5 TemplateCacheKey (org.thymeleaf.cache.TemplateCacheKey)2 ITemplateParser (org.thymeleaf.templateparser.ITemplateParser)2 Resource (org.apache.sling.api.resource.Resource)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 SlingContext (org.apache.sling.scripting.thymeleaf.SlingContext)1 TemplateResolution (org.thymeleaf.templateresolver.TemplateResolution)1 ITemplateResource (org.thymeleaf.templateresource.ITemplateResource)1