public final class FileTemplateResource extends Object implements ITemplateResource, Serializable
Implementation of ITemplateResource
representing a file in the file system.
Objects of this class are usually created by FileTemplateResolver
.
Constructor and Description |
---|
FileTemplateResource(File file,
String characterEncoding) |
FileTemplateResource(String path,
String characterEncoding) |
Modifier and Type | Method and Description |
---|---|
boolean |
exists()
Determines whether the resource represented by this object really exists or not.
|
String |
getBaseName()
Returns the base name of a resource.
|
String |
getDescription()
Returns a String describing the resource.
|
Reader |
reader()
Returns a
Reader that can be used for consuming the template contents. |
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. |
public String getDescription()
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.
getDescription
in interface ITemplateResource
public String getBaseName()
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)
.
getBaseName
in interface ITemplateResource
public Reader reader() throws IOException
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.
reader
in interface ITemplateResource
Reader
on the template contents. Should never return null.IOException
- if an input/output exception happens or if the resource does not exist (e.g.
FileNotFoundException
).public ITemplateResource relative(String relativeLocation) throws IOException
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.
relative
in interface ITemplateResource
relativeLocation
- the location of the resource we want to obtain, relative to the current one. Required.IOException
- if an input/output exception happens or if the resource does not exist (e.g.
FileNotFoundException
).public boolean exists()
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.
exists
in interface ITemplateResource
Copyright © 2015 The THYMELEAF team. All rights reserved.