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
Modifier and Type | Field and 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.
|
Modifier | Constructor and Description |
---|---|
protected |
AbstractTemplateResolver() |
Modifier and Type | Method and 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 resolver
|
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.
|
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.
|
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).
public static final boolean DEFAULT_USE_DECOUPLED_LOGIC
By default, resources will not be marked to look for decoupled logic.
public final String getName()
Returns the name of the template resolver
getName
in interface ITemplateResolver
public void setName(String name)
Sets a new name for the Template Resolver.
name
- the new namepublic 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.
getOrder
in interface ITemplateResolver
public void setOrder(Integer order)
Sets a new order for the template engine in the chain. Order should start with 1.
order
- the new order.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.
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()
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>)
resolvablePatterns
- the new patternspublic 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 a TemplateResolution
, returning null 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 at getResolvablePatterns()
. 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 return null.
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 a TemplateResolution
, returning null 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 at getResolvablePatterns()
. 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 return null.
checkExistence
- true if resource existence should be checked, false if notpublic 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 the TemplateMode.HTML
and TemplateMode.XML
template modes. Also, note that setting this flag to true 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.
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 the TemplateMode.HTML
and TemplateMode.XML
template modes. Also, note that setting this flag to true 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.
useDecoupledLogic
- true if resource existence should be checked, false if notpublic final TemplateResolution resolveTemplate(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String,Object> templateResolutionAttributes)
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 calling ITemplateResource.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.
resolveTemplate
in interface ITemplateResolver
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 a
TemplateSpec
instance. Can be null.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.
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.protected abstract ITemplateResource computeTemplateResource(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String,Object> templateResolutionAttributes)
Computes the resolved template resource.
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.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.
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.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.
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.Copyright © 2017 The THYMELEAF team. All rights reserved.