Interface ITemplateResource

All Known Implementing Classes:
ClassLoaderTemplateResource, FileTemplateResource, StringTemplateResource, UrlTemplateResource, WebApplicationTemplateResource

public interface ITemplateResource

Interface implemented by all Template Resource instances.

Template resources are created and returned by Template Resolvers (ITemplateResolver) and represent the real resource that contains the template contents, but do not necessarily contain these contents themselves.

Most usually, template resources are used to obtain a Reader on the template contents, abstracting the real location of those contents.

Note that the existence of a template resource object does not imply the existence of the resource it represents. In order to check whether a resource really exists or not, the exists() method can be called. Some implementations of ITemplateResolver might in fact do so, but note that this can result in a loss of performance in some scenarios due to a double access to a resource that might be remote (an HTTP URL, for example): one access for checking existence, and another access for creating the Reader to be returned by reader().

Note that implementations of these interface might not be thread-safe.

Since:
3.0.0
Author:
Daniel Fernández
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Determines whether the resource represented by this object really exists or not.
    Returns the base name of a resource.
    Returns a String describing the resource.
    Returns a Reader that can be used for consuming the template contents.
    relative(String relativeLocation)
    Creates another ITemplateResource, usually of the same implementation class, for a resource living in a location relative to the current object's.
  • Method Details

    • getDescription

      String getDescription()

      Returns a String describing the resource.

      Note this should not be taken for a valid resource name, as depending on the implementation it could be too verbose/descriptive or not unique enough to be used for identification purposes.

      Returns:
      the resource description. Should never return null.
    • getBaseName

      String getBaseName()

      Returns the base name of a resource.

      The base name is aimed at creating derivative names from the name of the resource, usually from the deepest level of the resource path.

      For example, a file resource located at /home/user/template/main.html should return main as its base name, so that names like main.properties, main.th.xml or similar can be derived, and afterwards resolved using relative(String).

      Returns:
      the base name, or null if it cannot be computed for the specific type of resource.
    • exists

      boolean exists()

      Determines whether the resource represented by this object really exists or not.

      Note that, depending on the implementation, this might mean actually access the resource, and such operation could have a cost in performance in some scenarios (e.g. a resource representing a remote URL).

      This mechanism will be used by Template Resolvers extending from AbstractTemplateResolver for checking real resource existence if the AbstractTemplateResolver.setCheckExistence(boolean) flag is set to true.

      Returns:
      true if the resource exists, false if not.
    • reader

      Reader reader() throws IOException

      Returns a Reader that can be used for consuming the template contents.

      Most implementations of this interface will require specifying a character encoding during construction, so that this readers are correctly able to decode their underlying input streams.

      Note this readers should be closed after being fully consumed, just like any other resources.

      Returns:
      a Reader on the template contents. Should never return null.
      Throws:
      IOException - if an input/output exception happens or if the resource does not exist (e.g. FileNotFoundException).
    • relative

      ITemplateResource relative(String relativeLocation)

      Creates another ITemplateResource, usually of the same implementation class, for a resource living in a location relative to the current object's.

      Note some ITemplateResource implementations might not support this feature.

      Parameters:
      relativeLocation - the location of the resource we want to obtain, relative to the current one. Required.
      Returns:
      the relative resource. Should never return null.