blob: 5ecc11802dde5ea69691bc77b0ffdf4f1907c352 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- NewPage -->
<html lang="en">
<head>
<!-- Generated by javadoc (1.8.0_281) on Sun Jan 15 15:58:33 CET 2023 -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>TemplateLoader (FreeMarker 2.3.32 API)</title>
<meta name="date" content="2023-01-15">
<link rel="stylesheet" type="text/css" href="../../stylesheet.css" title="Style">
<script type="text/javascript" src="../../script.js"></script>
</head>
<body>
<script type="text/javascript"><!--
try {
if (location.href.indexOf('is-external=true') == -1) {
parent.document.title="TemplateLoader (FreeMarker 2.3.32 API)";
}
}
catch(err) {
}
//-->
var methods = {"i0":6,"i1":6,"i2":6,"i3":6};
var tabs = {65535:["t0","All Methods"],2:["t2","Instance Methods"],4:["t3","Abstract Methods"]};
var altColor = "altColor";
var rowColor = "rowColor";
var tableTab = "tableTab";
var activeTableTab = "activeTableTab";
</script>
<noscript>
<div>JavaScript is disabled on your browser.</div>
</noscript>
<!-- ========= START OF TOP NAVBAR ======= -->
<div class="topNav"><a name="navbar.top">
<!-- -->
</a>
<div class="skipNav"><a href="#skip.navbar.top" title="Skip navigation links">Skip navigation links</a></div>
<a name="navbar.top.firstrow">
<!-- -->
</a>
<ul class="navList" title="Navigation">
<li><a href="../../overview-summary.html">Overview</a></li>
<li><a href="package-summary.html">Package</a></li>
<li class="navBarCell1Rev">Class</li>
<li><a href="class-use/TemplateLoader.html">Use</a></li>
<li><a href="package-tree.html">Tree</a></li>
<li><a href="../../deprecated-list.html">Deprecated</a></li>
<li><a href="../../index-all.html">Index</a></li>
<li><a href="../../help-doc.html">Help</a></li>
</ul>
</div>
<div class="subNav">
<ul class="navList">
<li><a href="../../freemarker/cache/TemplateConfigurationFactoryException.html" title="class in freemarker.cache"><span class="typeNameLink">Prev&nbsp;Class</span></a></li>
<li><a href="../../freemarker/cache/TemplateLookupContext.html" title="class in freemarker.cache"><span class="typeNameLink">Next&nbsp;Class</span></a></li>
</ul>
<ul class="navList">
<li><a href="../../index.html?freemarker/cache/TemplateLoader.html" target="_top">Frames</a></li>
<li><a href="TemplateLoader.html" target="_top">No&nbsp;Frames</a></li>
</ul>
<ul class="navList" id="allclasses_navbar_top">
<li><a href="../../allclasses-noframe.html">All&nbsp;Classes</a></li>
</ul>
<div>
<script type="text/javascript"><!--
allClassesLink = document.getElementById("allclasses_navbar_top");
if(window==top) {
allClassesLink.style.display = "block";
}
else {
allClassesLink.style.display = "none";
}
//-->
</script>
</div>
<div>
<ul class="subNavList">
<li>Summary:&nbsp;</li>
<li>Nested&nbsp;|&nbsp;</li>
<li>Field&nbsp;|&nbsp;</li>
<li>Constr&nbsp;|&nbsp;</li>
<li><a href="#method.summary">Method</a></li>
</ul>
<ul class="subNavList">
<li>Detail:&nbsp;</li>
<li>Field&nbsp;|&nbsp;</li>
<li>Constr&nbsp;|&nbsp;</li>
<li><a href="#method.detail">Method</a></li>
</ul>
</div>
<a name="skip.navbar.top">
<!-- -->
</a></div>
<!-- ========= END OF TOP NAVBAR ========= -->
<!-- ======== START OF CLASS DATA ======== -->
<div class="header">
<div class="subTitle">freemarker.cache</div>
<h2 title="Interface TemplateLoader" class="title">Interface TemplateLoader</h2>
</div>
<div class="contentContainer">
<div class="description">
<ul class="blockList">
<li class="blockList">
<dl>
<dt>All Known Subinterfaces:</dt>
<dd><a href="../../freemarker/cache/StatefulTemplateLoader.html" title="interface in freemarker.cache">StatefulTemplateLoader</a></dd>
</dl>
<dl>
<dt>All Known Implementing Classes:</dt>
<dd><a href="../../freemarker/cache/ByteArrayTemplateLoader.html" title="class in freemarker.cache">ByteArrayTemplateLoader</a>, <a href="../../freemarker/cache/ClassTemplateLoader.html" title="class in freemarker.cache">ClassTemplateLoader</a>, <a href="../../freemarker/cache/FileTemplateLoader.html" title="class in freemarker.cache">FileTemplateLoader</a>, <a href="../../freemarker/cache/MultiTemplateLoader.html" title="class in freemarker.cache">MultiTemplateLoader</a>, <a href="../../freemarker/cache/StringTemplateLoader.html" title="class in freemarker.cache">StringTemplateLoader</a>, <a href="../../freemarker/cache/URLTemplateLoader.html" title="class in freemarker.cache">URLTemplateLoader</a>, <a href="../../freemarker/cache/WebappTemplateLoader.html" title="class in freemarker.cache">WebappTemplateLoader</a></dd>
</dl>
<hr>
<br>
<pre>public interface <span class="typeNameLabel">TemplateLoader</span></pre>
<div class="block">FreeMarker loads template "files" through objects that implement this interface,
thus the templates need not be real files, and can come from any kind of data source
(like classpath, servlet context, database, etc). While FreeMarker provides a few
<a href="../../freemarker/cache/TemplateLoader.html" title="interface in freemarker.cache"><code>TemplateLoader</code></a> implementations out-of-the-box, it's normal for embedding
frameworks to use their own implementations.
<p>To set the <a href="../../freemarker/cache/TemplateLoader.html" title="interface in freemarker.cache"><code>TemplateLoader</code></a> used by FreeMaker, use
<a href="../../freemarker/template/Configuration.html#setTemplateLoader-freemarker.cache.TemplateLoader-"><code>Configuration.setTemplateLoader(TemplateLoader)</code></a>.
<p>Implementations of this interface should be thread-safe.
<p>Implementations should override <code>Object.toString()</code> to show information about from where the
<a href="../../freemarker/cache/TemplateLoader.html" title="interface in freemarker.cache"><code>TemplateLoader</code></a> loads the templates. For example, for a template loader that loads template from database
table <code>toString</code> could return something like
<code>"MyDatabaseTemplateLoader(user=\"cms\", table=\"mail_templates\")"</code>. This string will be shown in
<a href="../../freemarker/template/TemplateNotFoundException.html" title="class in freemarker.template"><code>TemplateNotFoundException</code></a> exception messages, next to the template name.
<p>For those who has to dig deeper, note that the <a href="../../freemarker/cache/TemplateLoader.html" title="interface in freemarker.cache"><code>TemplateLoader</code></a> is actually stored inside
the <a href="../../freemarker/cache/TemplateCache.html" title="class in freemarker.cache"><code>TemplateCache</code></a> of the <a href="../../freemarker/template/Configuration.html" title="class in freemarker.template"><code>Configuration</code></a>, and is normally only accessed directly
by the <a href="../../freemarker/cache/TemplateCache.html" title="class in freemarker.cache"><code>TemplateCache</code></a>, and templates are get via the <a href="../../freemarker/cache/TemplateCache.html" title="class in freemarker.cache"><code>TemplateCache</code></a> API-s.</div>
</li>
</ul>
</div>
<div class="summary">
<ul class="blockList">
<li class="blockList">
<!-- ========== METHOD SUMMARY =========== -->
<ul class="blockList">
<li class="blockList"><a name="method.summary">
<!-- -->
</a>
<h3>Method Summary</h3>
<table class="memberSummary" border="0" cellpadding="3" cellspacing="0" summary="Method Summary table, listing methods, and an explanation">
<caption><span id="t0" class="activeTableTab"><span>All Methods</span><span class="tabEnd">&nbsp;</span></span><span id="t2" class="tableTab"><span><a href="javascript:show(2);">Instance Methods</a></span><span class="tabEnd">&nbsp;</span></span><span id="t3" class="tableTab"><span><a href="javascript:show(4);">Abstract Methods</a></span><span class="tabEnd">&nbsp;</span></span></caption>
<tr>
<th class="colFirst" scope="col">Modifier and Type</th>
<th class="colLast" scope="col">Method and Description</th>
</tr>
<tr id="i0" class="altColor">
<td class="colFirst"><code>void</code></td>
<td class="colLast"><code><span class="memberNameLink"><a href="../../freemarker/cache/TemplateLoader.html#closeTemplateSource-java.lang.Object-">closeTemplateSource</a></span>(java.lang.Object&nbsp;templateSource)</code>
<div class="block">Closes the template source, releasing any resources held that are only required for reading the template and/or
its metadata.</div>
</td>
</tr>
<tr id="i1" class="rowColor">
<td class="colFirst"><code>java.lang.Object</code></td>
<td class="colLast"><code><span class="memberNameLink"><a href="../../freemarker/cache/TemplateLoader.html#findTemplateSource-java.lang.String-">findTemplateSource</a></span>(java.lang.String&nbsp;name)</code>
<div class="block">Finds the template in the backing storage and returns an object that identifies the storage location where the
template can be loaded from.</div>
</td>
</tr>
<tr id="i2" class="altColor">
<td class="colFirst"><code>long</code></td>
<td class="colLast"><code><span class="memberNameLink"><a href="../../freemarker/cache/TemplateLoader.html#getLastModified-java.lang.Object-">getLastModified</a></span>(java.lang.Object&nbsp;templateSource)</code>
<div class="block">Returns the time of last modification of the specified template source.</div>
</td>
</tr>
<tr id="i3" class="rowColor">
<td class="colFirst"><code>java.io.Reader</code></td>
<td class="colLast"><code><span class="memberNameLink"><a href="../../freemarker/cache/TemplateLoader.html#getReader-java.lang.Object-java.lang.String-">getReader</a></span>(java.lang.Object&nbsp;templateSource,
java.lang.String&nbsp;encoding)</code>
<div class="block">Returns the character stream of a template represented by the specified template source.</div>
</td>
</tr>
</table>
</li>
</ul>
</li>
</ul>
</div>
<div class="details">
<ul class="blockList">
<li class="blockList">
<!-- ============ METHOD DETAIL ========== -->
<ul class="blockList">
<li class="blockList"><a name="method.detail">
<!-- -->
</a>
<h3>Method Detail</h3>
<a name="findTemplateSource-java.lang.String-">
<!-- -->
</a>
<ul class="blockList">
<li class="blockList">
<h4>findTemplateSource</h4>
<pre>java.lang.Object&nbsp;findTemplateSource(java.lang.String&nbsp;name)
throws java.io.IOException</pre>
<div class="block">Finds the template in the backing storage and returns an object that identifies the storage location where the
template can be loaded from. See the return value for more information.</div>
<dl>
<dt><span class="paramLabel">Parameters:</span></dt>
<dd><code>name</code> - The name (template root directory relative path) of the template, already localized and normalized by
the <a href="../../freemarker/cache/TemplateCache.html" title="class in freemarker.cache"><code>cache</code></a>. It is completely up to the loader implementation to
interpret the name, however it should expect to receive hierarchical paths where path components are
separated by a slash (not backslash). Backslashes (or any other OS specific separator character) are
not considered as separators by FreeMarker, and thus they will not be replaced with slash before
passing to this method, so it's up to the template loader to handle them (say, by throwing an
exception that tells the user that the path (s)he has entered is invalid, as (s)he must use slash --
typical mistake of Windows users). The passed names are always considered relative to some
loader-defined root location (often referred as the "template root directory"), and will never start
with a slash, nor will they contain a path component consisting of either a single or a double dot --
these are all resolved by the template cache before passing the name to the loader. As a side effect,
paths that trivially reach outside template root directory, such as <tt>../my.ftl</tt>, will be
rejected by the template cache, so they never reach the template loader. Note again, that if the path
uses backslash as path separator instead of slash as (the template loader should not accept that), the
normalization will not properly happen, as FreeMarker (the cache) recognizes only the slashes as
separators.</dd>
<dt><span class="returnLabel">Returns:</span></dt>
<dd>An object representing the template source, which can be supplied in subsequent calls to
<a href="../../freemarker/cache/TemplateLoader.html#getLastModified-java.lang.Object-"><code>getLastModified(Object)</code></a> and <a href="../../freemarker/cache/TemplateLoader.html#getReader-java.lang.Object-java.lang.String-"><code>getReader(Object, String)</code></a>, when those are called on the
same <a href="../../freemarker/cache/TemplateLoader.html" title="interface in freemarker.cache"><code>TemplateLoader</code></a>. <code>null</code> must be returned if the source for the template doesn't exist;
don't throw exception then! The exact type of this object is up to the <a href="../../freemarker/cache/TemplateLoader.html" title="interface in freemarker.cache"><code>TemplateLoader</code></a>
implementation. As this object is possibly used as hash key in caches, and is surly compared with another
template source for equality, <b>it must have a proper <code>Object.equals(Object)</code> and
<code>Object.hashCode()</code>) implementation</b>. Especially, template sources that refer to the same
physical source must be equivalent, otherwise template caching can become inefficient. This is only
expected from <code>Object.equals(Object)</code> when the compared template sources came from the same
<a href="../../freemarker/cache/TemplateLoader.html" title="interface in freemarker.cache"><code>TemplateLoader</code></a> instance. Also, it must not influence the equality if the source is open or
closed (<a href="../../freemarker/cache/TemplateLoader.html#closeTemplateSource-java.lang.Object-"><code>closeTemplateSource(Object)</code></a>).</dd>
<dt><span class="throwsLabel">Throws:</span></dt>
<dd><code>java.io.IOException</code> - When an error occurs that makes it impossible to find out if the template exists, or to access the
existing template. Don't throw exception if the template doesn't exist, instead return with
<code>null</code> then!</dd>
</dl>
</li>
</ul>
<a name="getLastModified-java.lang.Object-">
<!-- -->
</a>
<ul class="blockList">
<li class="blockList">
<h4>getLastModified</h4>
<pre>long&nbsp;getLastModified(java.lang.Object&nbsp;templateSource)</pre>
<div class="block">Returns the time of last modification of the specified template source. This method is called after
<code>findTemplateSource()</code>.</div>
<dl>
<dt><span class="paramLabel">Parameters:</span></dt>
<dd><code>templateSource</code> - an object representing a template source (the template file), obtained through a prior call to
<a href="../../freemarker/cache/TemplateLoader.html#findTemplateSource-java.lang.String-"><code>findTemplateSource(String)</code></a>. This must be an object on which
<a href="../../freemarker/cache/TemplateLoader.html#closeTemplateSource-java.lang.Object-"><code>closeTemplateSource(Object)</code></a> wasn't applied yet.</dd>
<dt><span class="returnLabel">Returns:</span></dt>
<dd>The time of last modification for the specified template source, or -1 if the time is not known. This
value meant to be milliseconds since the epoch, but in fact FreeMarker doesn't care what it means, it
only cares if it changes, in which case the template needs to be reloaded (even if the value has
decreased). -1 is not special in that regard either; if you keep returning it, FreeMarker won't
reload the template (as far as it's not evicted from the cache from some other
reason). Note that <code>Long.MIN_VALUE</code> is reserved for internal use.</dd>
</dl>
</li>
</ul>
<a name="getReader-java.lang.Object-java.lang.String-">
<!-- -->
</a>
<ul class="blockList">
<li class="blockList">
<h4>getReader</h4>
<pre>java.io.Reader&nbsp;getReader(java.lang.Object&nbsp;templateSource,
java.lang.String&nbsp;encoding)
throws java.io.IOException</pre>
<div class="block">Returns the character stream of a template represented by the specified template source. This method is possibly
called for multiple times for the same template source object, and it must always return a <code>Reader</code> that
reads the template from its beginning. Before this method is called for the second time (or later), its caller
must close the previously returned <code>Reader</code>, and it must not use it anymore. That is, this method is not
required to support multiple concurrent readers for the same source <code>templateSource</code> object.
<p>
Typically, this method is called if the template is missing from the cache, or if after calling
<a href="../../freemarker/cache/TemplateLoader.html#findTemplateSource-java.lang.String-"><code>findTemplateSource(String)</code></a> and <a href="../../freemarker/cache/TemplateLoader.html#getLastModified-java.lang.Object-"><code>getLastModified(Object)</code></a> it was determined that the cached copy
of the template is stale. Then, if it turns out that the <code>encoding</code> parameter used doesn't match the actual
template content (based on the <code>#ftl encoding=...</code> header), this method will be called for a second time
with the correct <code>encoding</code> parameter value.
<p>
Unlike <a href="../../freemarker/cache/TemplateLoader.html#findTemplateSource-java.lang.String-"><code>findTemplateSource(String)</code></a>, this method must not tolerate if the template is not found, and
must throw <code>IOException</code> in that case.</div>
<dl>
<dt><span class="paramLabel">Parameters:</span></dt>
<dd><code>templateSource</code> - an object representing a template source, obtained through a prior call to
<a href="../../freemarker/cache/TemplateLoader.html#findTemplateSource-java.lang.String-"><code>findTemplateSource(String)</code></a>. This must be an object on which
<a href="../../freemarker/cache/TemplateLoader.html#closeTemplateSource-java.lang.Object-"><code>closeTemplateSource(Object)</code></a> wasn't applied yet.</dd>
<dd><code>encoding</code> - the character encoding used to translate source bytes to characters. Some loaders may not have access
to the byte representation of the template stream, and instead directly obtain a character stream.
These loaders should ignore the encoding parameter.</dd>
<dt><span class="returnLabel">Returns:</span></dt>
<dd>A <code>Reader</code> representing the template character stream; not <code>null</code>. It's the responsibility of
the caller (which is <a href="../../freemarker/cache/TemplateCache.html" title="class in freemarker.cache"><code>TemplateCache</code></a> usually) to <code>close()</code> it. The <code>Reader</code> is not
required to work after the <code>templateSource</code> was closed (<a href="../../freemarker/cache/TemplateLoader.html#closeTemplateSource-java.lang.Object-"><code>closeTemplateSource(Object)</code></a>).</dd>
<dt><span class="throwsLabel">Throws:</span></dt>
<dd><code>java.io.IOException</code> - if an I/O error occurs while accessing the stream.</dd>
</dl>
</li>
</ul>
<a name="closeTemplateSource-java.lang.Object-">
<!-- -->
</a>
<ul class="blockListLast">
<li class="blockList">
<h4>closeTemplateSource</h4>
<pre>void&nbsp;closeTemplateSource(java.lang.Object&nbsp;templateSource)
throws java.io.IOException</pre>
<div class="block">Closes the template source, releasing any resources held that are only required for reading the template and/or
its metadata. This is the last method that is called by the <a href="../../freemarker/cache/TemplateCache.html" title="class in freemarker.cache"><code>TemplateCache</code></a> for a template source, except
that <code>Object.equals(Object)</code> is might called later too. <a href="../../freemarker/cache/TemplateCache.html" title="class in freemarker.cache"><code>TemplateCache</code></a> ensures that this method will
be called on every object that is returned from <a href="../../freemarker/cache/TemplateLoader.html#findTemplateSource-java.lang.String-"><code>findTemplateSource(String)</code></a>.</div>
<dl>
<dt><span class="paramLabel">Parameters:</span></dt>
<dd><code>templateSource</code> - the template source that should be closed.</dd>
<dt><span class="throwsLabel">Throws:</span></dt>
<dd><code>java.io.IOException</code></dd>
</dl>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<!-- ========= END OF CLASS DATA ========= -->
<!-- ======= START OF BOTTOM NAVBAR ====== -->
<div class="bottomNav"><a name="navbar.bottom">
<!-- -->
</a>
<div class="skipNav"><a href="#skip.navbar.bottom" title="Skip navigation links">Skip navigation links</a></div>
<a name="navbar.bottom.firstrow">
<!-- -->
</a>
<ul class="navList" title="Navigation">
<li><a href="../../overview-summary.html">Overview</a></li>
<li><a href="package-summary.html">Package</a></li>
<li class="navBarCell1Rev">Class</li>
<li><a href="class-use/TemplateLoader.html">Use</a></li>
<li><a href="package-tree.html">Tree</a></li>
<li><a href="../../deprecated-list.html">Deprecated</a></li>
<li><a href="../../index-all.html">Index</a></li>
<li><a href="../../help-doc.html">Help</a></li>
</ul>
</div>
<div class="subNav">
<ul class="navList">
<li><a href="../../freemarker/cache/TemplateConfigurationFactoryException.html" title="class in freemarker.cache"><span class="typeNameLink">Prev&nbsp;Class</span></a></li>
<li><a href="../../freemarker/cache/TemplateLookupContext.html" title="class in freemarker.cache"><span class="typeNameLink">Next&nbsp;Class</span></a></li>
</ul>
<ul class="navList">
<li><a href="../../index.html?freemarker/cache/TemplateLoader.html" target="_top">Frames</a></li>
<li><a href="TemplateLoader.html" target="_top">No&nbsp;Frames</a></li>
</ul>
<ul class="navList" id="allclasses_navbar_bottom">
<li><a href="../../allclasses-noframe.html">All&nbsp;Classes</a></li>
</ul>
<div>
<script type="text/javascript"><!--
allClassesLink = document.getElementById("allclasses_navbar_bottom");
if(window==top) {
allClassesLink.style.display = "block";
}
else {
allClassesLink.style.display = "none";
}
//-->
</script>
</div>
<div>
<ul class="subNavList">
<li>Summary:&nbsp;</li>
<li>Nested&nbsp;|&nbsp;</li>
<li>Field&nbsp;|&nbsp;</li>
<li>Constr&nbsp;|&nbsp;</li>
<li><a href="#method.summary">Method</a></li>
</ul>
<ul class="subNavList">
<li>Detail:&nbsp;</li>
<li>Field&nbsp;|&nbsp;</li>
<li>Constr&nbsp;|&nbsp;</li>
<li><a href="#method.detail">Method</a></li>
</ul>
</div>
<a name="skip.navbar.bottom">
<!-- -->
</a></div>
<!-- ======== END OF BOTTOM NAVBAR ======= -->
</body>
</html>