Class AbstractTemplateResolver
- Object
-
- org.thymeleaf.templateresolver.AbstractTemplateResolver
-
- All Implemented Interfaces:
ITemplateResolver
- Direct Known Subclasses:
AbstractConfigurableTemplateResolver
,DefaultTemplateResolver
,StringTemplateResolver
public abstract class AbstractTemplateResolver extends Object implements ITemplateResolver
Convenience base class for all Template Resolvers.
Note a class with this name existed since 1.0, but it was completely reimplemented in Thymeleaf 3.0
- Since:
- 3.0.0
- Author:
- Daniel Fernández
-
-
Field Summary
Fields Modifier and Type Field Description static boolean
DEFAULT_EXISTENCE_CHECK
By default, resources will not be checked their existence before being returned.static boolean
DEFAULT_USE_DECOUPLED_LOGIC
By default, resources will not be marked to look for decoupled logic.
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractTemplateResolver()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected boolean
computeResolvable(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String,Object> templateResolutionAttributes)
Computes whether a template can be resolved by this resolver or not, applying the corresponding patterns.protected abstract TemplateMode
computeTemplateMode(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String,Object> templateResolutionAttributes)
Computes the template mode that should be applied to a template, according to existing configuration.protected abstract ITemplateResource
computeTemplateResource(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String,Object> templateResolutionAttributes)
Computes the resolved template resource.protected abstract ICacheEntryValidity
computeValidity(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String,Object> templateResolutionAttributes)
Computes the validity to be applied to the template resolution.boolean
getCheckExistence()
Returns whether template resources will be checked for existence before being returned or not.String
getName()
Returns the name of the template resolverInteger
getOrder()
Returns the order in which this template resolver will be asked to resolve templates as a part of the chain of resolvers configured into the template engine.Set<String>
getResolvablePatterns()
Returns the patterns (as String) specified for establishing which templates can be resolved by this template resolver.PatternSpec
getResolvablePatternSpec()
Returns the pattern spec specified for establishing which templates can be resolved by this template resolver.boolean
getUseDecoupledLogic()
Returns whether a separate (decoupled) resource containing template logic should be checked for existence and its instructions included into the resolved template during parsing.TemplateResolution
resolveTemplate(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String,Object> templateResolutionAttributes)
Tries to resolve a template.void
setCheckExistence(boolean checkExistence)
Sets whether template resources will be checked for existence before being returned or not.void
setName(String name)
Sets a new name for the Template Resolver.void
setOrder(Integer order)
Sets a new order for the template engine in the chain.void
setResolvablePatterns(Set<String> resolvablePatterns)
Sets the new patterns to be applied for establishing which templates can be resolved by this template resolver.void
setUseDecoupledLogic(boolean useDecoupledLogic)
Sets whether a separate (decoupled) resource containing template logic should be checked for existence and its instructions included into the resolved template during parsing.
-
-
-
Field Detail
-
DEFAULT_EXISTENCE_CHECK
public static final boolean DEFAULT_EXISTENCE_CHECK
By default, resources will not be checked their existence before being returned. This tries to avoid a possible performance impact from performing a double access to the resource (one for checking existence, another one for reading it).
- See Also:
- Constant Field Values
-
DEFAULT_USE_DECOUPLED_LOGIC
public static final boolean DEFAULT_USE_DECOUPLED_LOGIC
By default, resources will not be marked to look for decoupled logic.
- See Also:
- Constant Field Values
-
-
Method Detail
-
getName
public final String getName()
Returns the name of the template resolver
- Specified by:
getName
in interfaceITemplateResolver
- Returns:
- the name of the template resolver
-
setName
public void setName(String name)
Sets a new name for the Template Resolver.
- Parameters:
name
- the new name
-
getOrder
public final Integer getOrder()
Returns the order in which this template resolver will be asked to resolve templates as a part of the chain of resolvers configured into the template engine.
Order should start with 1.
- Specified by:
getOrder
in interfaceITemplateResolver
- Returns:
- the order in which this template resolver will be called in the chain.
-
setOrder
public void setOrder(Integer order)
Sets a new order for the template engine in the chain. Order should start with 1.
- Parameters:
order
- the new order.
-
getResolvablePatternSpec
public final PatternSpec getResolvablePatternSpec()
Returns the pattern spec specified for establishing which templates can be resolved by this template resolver. For those templates which names do not match this patterns, the Template Resolver will return null.
This allows for a fast discard of those templates that the developer might know for sure that will not be resolvable by the Resource Resolver used by this Template Resolver, so that an execution of the resource resolver is not needed.
- Returns:
- the pattern spec
-
getResolvablePatterns
public final Set<String> getResolvablePatterns()
Returns the patterns (as String) specified for establishing which templates can be resolved by this template resolver. For those templates which names do not match this patterns, the Template Resolver will return null.
This allows for a fast discard of those templates that the developer might know for sure that will not be resolvable by the Resource Resolver used by this Template Resolver, so that an execution of the resource resolver is not needed.
This is a convenience method equivalent to
getResolvablePatternSpec()
.getPatterns()- Returns:
- the pattern spec
-
setResolvablePatterns
public void setResolvablePatterns(Set<String> resolvablePatterns)
Sets the new patterns to be applied for establishing which templates can be resolved by this template resolver. For those templates which names do not match this patterns, the Template Resolver will return null.
This allows for a fast discard of those templates that the developer might know for sure that will not be resolvable by the Resource Resolver used by this Template Resolver, so that an execution of the resource resolver is not needed.
This is a convenience method equivalent to
getResolvablePatternSpec()
.setPatterns(Set<String>)- Parameters:
resolvablePatterns
- the new patterns
-
getCheckExistence
public final boolean getCheckExistence()
Returns whether template resources will be checked for existence before being returned or not.
Default value is
FALSE
.Checking resources for existence will make the template resolver execute
ITemplateResource.exists()
for each resolved resource before returning aTemplateResolution
, returningnull
if the resource does not exist.This allows resolvers to pass control to the next
ITemplateResolver
in the chain based on real resource existence and not only on the matching performed by the resolvable patterns specified atgetResolvablePatterns()
. But at the same time, might pose a performance issue on certain scenarios (e.g. HTTP URL resolution) that require actually accessing the resource in order to determine its existence, being the resource accessed twice in those cases (once for determining its existence, another time for reading it).If this existence check is enabled and a resource is determined to not exist,
ITemplateResolver.resolveTemplate(IEngineConfiguration, String, String, Map)
will returnnull
.- Returns:
true
if resource existence will be checked,false
if not- Since:
- 3.0.0
-
setCheckExistence
public void setCheckExistence(boolean checkExistence)
Sets whether template resources will be checked for existence before being returned or not.
Default value is
FALSE
.Checking resources for existence will make the template resolver execute
ITemplateResource.exists()
for each resolved resource before returning aTemplateResolution
, returningnull
if the resource does not exist.This allows resolvers to pass control to the next
ITemplateResolver
in the chain based on real resource existence and not only on the matching performed by the resolvable patterns specified atgetResolvablePatterns()
. But at the same time, might pose a performance issue on certain scenarios (e.g. HTTP URL resolution) that require actually accessing the resource in order to determine its existence, being the resource accessed twice in those cases (once for determining its existence, another time for reading it).If this existence check is enabled and a resource is determined to not exist,
ITemplateResolver.resolveTemplate(IEngineConfiguration, String, String, Map)
will returnnull
.- Parameters:
checkExistence
-true
if resource existence should be checked,false
if not- Since:
- 3.0.0
-
getUseDecoupledLogic
public final boolean getUseDecoupledLogic()
Returns whether a separate (decoupled) resource containing template logic should be checked for existence and its instructions included into the resolved template during parsing.
This mechanism allows the creation of pure HTML or XML markup templates, which acquire their logic from an external resource. The way this decoupled resources are resolved is defined by a configured implementation of the
IDecoupledTemplateLogicResolver
interface.Note this flag can only be
true
for theTemplateMode1.html
andTemplateMode.XML
template modes. Also, note that setting this flag totrue
does not mean that a resource with decoupled logic must exist for the resolved template, only that it can exist.Decoupled logic extracted from these additional resources is injected into the resolved templates in real-time as the resolved templates are parsed and processed. This greatly reduces overhead caused by decoupled parsing for non-cacheable templates, and completely removes any overhead for cached templates.
Default value is
FALSE
.- Returns:
true
if decoupled logic resources should be checked,false
if not.- Since:
- 3.0.0
-
setUseDecoupledLogic
public void setUseDecoupledLogic(boolean useDecoupledLogic)
Sets whether a separate (decoupled) resource containing template logic should be checked for existence and its instructions included into the resolved template during parsing.
This mechanism allows the creation of pure HTML or XML markup templates, which acquire their logic from an external resource. The way this decoupled resources are resolved is defined by a configured implementation of the
IDecoupledTemplateLogicResolver
interface.Note this flag can only be
true
for theTemplateMode1.html
andTemplateMode.XML
template modes. Also, note that setting this flag totrue
does not mean that a resource with decoupled logic must exist for the resolved template, only that it can exist and therefore it should be checked.Decoupled logic extracted from these additional resources is injected into the resolved templates in real-time as the resolved templates are parsed and processed. This greatly reduces overhead caused by decoupled parsing for non-cacheable templates, and completely removes any overhead for cached templates.
Default value is
FALSE
.- Parameters:
useDecoupledLogic
-true
if resource existence should be checked,false
if not- Since:
- 3.0.0
-
resolveTemplate
public final TemplateResolution resolveTemplate(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String,Object> templateResolutionAttributes)
Description copied from interface:ITemplateResolver
Tries to resolve a template.
The method arguments contain all the info needed for trying to resolve the template. The Template Resolver will apply its configuration (prefixes/suffixes, template mode patterns, cache configurations, etc) and return a
TemplateResolution
object.The
ownerTemplate
, which might be null, will be specified when the template is resolved in order to be used as a fragent to be inserted into a higher level template (the owner). Most template resolver implementations will simply ignore this argument, but others might change their resolution results depending on the owner template that is inserting the resolved fragment.The fact that a Template Resolver returns a
TemplateResolution
does not necessarily mean that the resolved template resource exists. It might only be so if the template resolver is configured to perform an existence check on the resource before returning a resolution result (by means of callingITemplateResource.exists()
), which might be configurable on a per-ITemplateResolver
-implementation basis. Implementations might choose not to check resource existance by default in order to avoid the possible performance impact of a double access to the resource.Note that the template selectors that might be used for a executing or inserting a template are not specified to the template resolver. The reason is template selectors are applied by the parser, not the template resolvers, and allowing the resolver to take any decisions based on template selectors (like e.g. omitting some output from the resource) could harm the correctness of the selection operation performed by the parser.
- Specified by:
resolveTemplate
in interfaceITemplateResolver
- Parameters:
configuration
- the engine configuration.ownerTemplate
- the containing template from which we want to resolve a new one as a fragment. Can be null.template
- the template to be resolved (usually its name).templateResolutionAttributes
- the template resolution attributes to be used (usually coming from aTemplateSpec
instance. Can be null.- Returns:
- a TemplateResolution object (which might represent an existing resource or not), or null if the template could not be resolved.
-
computeResolvable
protected boolean computeResolvable(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String,Object> templateResolutionAttributes)
Computes whether a template can be resolved by this resolver or not, applying the corresponding patterns. Meant only for use or override by subclasses.
- Parameters:
configuration
- the engine configuration.ownerTemplate
- the owner template, if the resource being computed is a fragment. Might be null.template
- the template to be resolved (usually its name).templateResolutionAttributes
- the template resolution attributes, if any. Might be null.- Returns:
- whether the template is resolvable or not.
-
computeTemplateResource
protected abstract ITemplateResource computeTemplateResource(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String,Object> templateResolutionAttributes)
Computes the resolved template resource.
- Parameters:
configuration
- the engine configuration.ownerTemplate
- the owner template, if the resource being computed is a fragment. Might be null.template
- the template to be resolved (usually its name).templateResolutionAttributes
- the template resolution attributes, if any. Might be null.- Returns:
- the template resource, or null if this template cannot be resolved (or the resource does not exist).
-
computeTemplateMode
protected abstract TemplateMode computeTemplateMode(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String,Object> templateResolutionAttributes)
Computes the template mode that should be applied to a template, according to existing configuration.
- Parameters:
configuration
- the engine configuration.ownerTemplate
- the owner template, if the resource being computed is a fragment. Might be null.template
- the template to be resolved (usually its name).templateResolutionAttributes
- the template resolution attributes, if any. Might be null.- Returns:
- the template mode proposed by the template resolver for the resolved template.
-
computeValidity
protected abstract ICacheEntryValidity computeValidity(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String,Object> templateResolutionAttributes)
Computes the validity to be applied to the template resolution. This includes determining whether the template can be cached or not, and also in what circumstances (for instance, for how much time) can its cache entry be considered valid.
- Parameters:
configuration
- the engine configuration.ownerTemplate
- the owner template, if the resource being computed is a fragment. Might be null.template
- the template to be resolved (usually its name).templateResolutionAttributes
- the template resolution attributes, if any. Might be null.- Returns:
- the validity
-
-