Class ClassLoaderTemplateResource

Object
org.thymeleaf.templateresource.ClassLoaderTemplateResource
All Implemented Interfaces:
ITemplateResource

public final class ClassLoaderTemplateResource extends Object implements ITemplateResource

Implementation of ITemplateResource representing a resource accessible by a ClassLoader (i.e. living at the class path).

Objects of this class are usually created by ClassLoaderTemplateResolver.

Since:
3.0.0
Author:
Daniel Fernández
  • Constructor Details

    • ClassLoaderTemplateResource

      public ClassLoaderTemplateResource(String path, String characterEncoding)

      Create a ClassLoader-based template resource, without specifying the specific class loader to be used for resolving the resource.

      If created this way, the sequence explained in ClassLoaderUtils.loadResourceAsStream(String) will be used for resolving the resource.

      Parameters:
      path - the path to the template resource.
      characterEncoding - the character encoding to be used to read the resource.
      Since:
      3.0.3
    • ClassLoaderTemplateResource

      public ClassLoaderTemplateResource(ClassLoader classLoader, String path, String characterEncoding)

      Create a ClassLoader-based template resource, specifying the specific class loader to be used for resolving the resource.

      Parameters:
      classLoader - the class loader to be used for resource resolution.
      path - the path to the template resource.
      characterEncoding - the character encoding to be used to read the resource.
      Since:
      3.0.3
  • Method Details

    • getDescription

      public String getDescription()
      Description copied from interface: ITemplateResource

      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.

      Specified by:
      getDescription in interface ITemplateResource
      Returns:
      the resource description. Should never return null.
    • getBaseName

      public String getBaseName()
      Description copied from interface: ITemplateResource

      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 ITemplateResource.relative(String).

      Specified by:
      getBaseName in interface ITemplateResource
      Returns:
      the base name, or null if it cannot be computed for the specific type of resource.
    • reader

      public Reader reader() throws IOException
      Description copied from interface: ITemplateResource

      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.

      Specified by:
      reader in interface ITemplateResource
      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

      public ITemplateResource relative(String relativeLocation)
      Description copied from interface: ITemplateResource

      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.

      Specified by:
      relative in interface ITemplateResource
      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.
    • exists

      public boolean exists()
      Description copied from interface: ITemplateResource

      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.

      Specified by:
      exists in interface ITemplateResource
      Returns:
      true if the resource exists, false if not.