Be more robust about closing template Reader.

Also some tidying: we don't like tabs or trailing whitespace.
This commit is contained in:
Michael Bayne
2015-03-06 12:19:46 -08:00
parent 6a02d434ec
commit 43a51b3235
@@ -273,8 +273,8 @@ public class Mustache
/** Handles loading partial templates. */
public interface TemplateLoader
{
/** Returns a reader for the template with the supplied name.
* Reader will be closed by callee.
/** Returns a reader for the template with the supplied name.
* Reader will be closed by callee.
* @throws Exception if the template could not be loaded for any reason. */
Reader getTemplate (String name) throws Exception;
}
@@ -741,16 +741,22 @@ public class Mustache
// we compile our template lazily to avoid infinie recursion if a template includes
// itself (see issue #13)
if (_template == null) {
Reader tin = null;
try {
Reader t = _comp.loader.getTemplate(_name);
_template = _comp.compile(t);
t.close();
tin = _comp.loader.getTemplate(_name);
_template = _comp.compile(tin);
} catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException)e;
} else {
throw new MustacheException("Unable to load template: " + _name, e);
}
} finally {
if (tin != null) try {
tin.close();
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
}
// we must take care to preserve our context rather than creating a new one, which