Class StringTemplateResolver

  • All Implemented Interfaces:
    ITemplateResolver

    public class StringTemplateResolver
    extends AbstractTemplateResolver

    Implementation of ITemplateResolver that extends AbstractTemplateResolver and creates StringTemplateResource instances for template resources.

    This template resolvers will consider the template being resolved as the template itself, this is, its contents. No external file or resource will be therefore accessed.

    This template resolver will consider its resolved templates non-cacheable by default, given its nature of being used for resolving arbitrary String objects.

    Also, the TemplateMode.HTML template mode will be used by default.

    Note this is the default template resolver that TemplateEngine instances will use if no other resolvers are configured.

    Since:
    3.0.0
    Author:
    Daniel Fernández
    • Field Detail

      • DEFAULT_CACHEABLE

        public static final boolean DEFAULT_CACHEABLE

        Default value for the cacheable flag: false

        See Also:
        Constant Field Values
      • DEFAULT_CACHE_TTL_MS

        public static final Long DEFAULT_CACHE_TTL_MS

        Default value for the cache TTL: null. This means the parsed template will live in cache until removed by LRU (because of being the oldest entry).

    • Constructor Detail

      • StringTemplateResolver

        public StringTemplateResolver()

        Creates a new instance of this template resolver.

    • Method Detail

      • getTemplateMode

        public final TemplateMode getTemplateMode()

        Returns the template mode to be applied to templates resolved by this template resolver.

        Returns:
        the template mode to be used.
      • setTemplateMode

        public final void setTemplateMode​(TemplateMode templateMode)

        Sets the template mode to be applied to templates resolved by this resolver.

        Parameters:
        templateMode - the template mode.
      • setTemplateMode

        public final void setTemplateMode​(String templateMode)

        Sets the template mode to be applied to templates resolved by this resolver.

        Allowed templates modes are defined by the TemplateMode class.

        Parameters:
        templateMode - the template mode.
      • isCacheable

        public final boolean isCacheable()

        Returns whether templates resolved by this resolver have to be considered cacheable or not.

        Returns:
        whether templates resolved are cacheable or not.
      • setCacheable

        public final void setCacheable​(boolean cacheable)

        Sets a new value for the cacheable flag.

        Parameters:
        cacheable - whether resolved patterns should be considered cacheable or not.
      • getCacheTTLMs

        public final Long getCacheTTLMs()

        Returns the TTL (Time To Live) in cache of templates resolved by this resolver.

        If a template is resolved as cacheable but cache TTL is null, this means the template will live in cache until evicted by LRU (Least Recently Used) algorithm for being the oldest entry in cache.

        Returns:
        the cache TTL for resolved templates.
      • setCacheTTLMs

        public final void setCacheTTLMs​(Long cacheTTLMs)

        Sets a new value for the cache TTL for resolved templates.

        If a template is resolved as cacheable but cache TTL is null, this means the template will live in cache until evicted by LRU (Least Recently Used) algorithm for being the oldest entry in cache.

        Parameters:
        cacheTTLMs - the new cache TTL, or null for using natural LRU eviction.
      • setUseDecoupledLogic

        public void setUseDecoupledLogic​(boolean useDecoupledLogic)
        Description copied from class: AbstractTemplateResolver

        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.

        Overrides:
        setUseDecoupledLogic in class AbstractTemplateResolver
        Parameters:
        useDecoupledLogic - true if resource existence should be checked, false if not
      • computeTemplateResource

        protected ITemplateResource computeTemplateResource​(IEngineConfiguration configuration,
                                                            String ownerTemplate,
                                                            String template,
                                                            Map<String,​Object> templateResolutionAttributes)
        Description copied from class: AbstractTemplateResolver

        Computes the resolved template resource.

        Specified by:
        computeTemplateResource in class AbstractTemplateResolver
        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 TemplateMode computeTemplateMode​(IEngineConfiguration configuration,
                                                   String ownerTemplate,
                                                   String template,
                                                   Map<String,​Object> templateResolutionAttributes)
        Description copied from class: AbstractTemplateResolver

        Computes the template mode that should be applied to a template, according to existing configuration.

        Specified by:
        computeTemplateMode in class AbstractTemplateResolver
        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 ICacheEntryValidity computeValidity​(IEngineConfiguration configuration,
                                                      String ownerTemplate,
                                                      String template,
                                                      Map<String,​Object> templateResolutionAttributes)
        Description copied from class: AbstractTemplateResolver

        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.

        Specified by:
        computeValidity in class AbstractTemplateResolver
        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