MCPcopy Create free account
hub / github.com/apache/freemarker / URLTemplateLoader

Class URLTemplateLoader

src/main/java/freemarker/cache/URLTemplateLoader.java:69–128  ·  view source on GitHub ↗

This is an abstract template loader that can load templates whose location can be described by an URL. Subclasses only need to override the #getURL(String) method. Both ClassTemplateLoader and WebappTemplateLoader are (quite trivial) subclasses of this class. @version $Id: UR

Source from the content-addressed store, hash-verified

67 * @author Attila Szegedi
68 */
69public abstract class URLTemplateLoader implements TemplateLoader
70{
71 public Object findTemplateSource(String name)
72 throws
73 IOException
74 {
75 URL url = getURL(name);
76 return url == null ? null : new URLTemplateSource(url);
77 }
78
79 /**
80 * Given a template name (plus potential locale decorations) retrieves
81 * an URL that points the template source.
82 * @param name the name of the sought template, including the locale
83 * decorations.
84 * @return an URL that points to the template source, or null if it can
85 * determine that the template source does not exist.
86 */
87 protected abstract URL getURL(String name);
88
89 public long getLastModified(Object templateSource)
90 {
91 return ((URLTemplateSource) templateSource).lastModified();
92 }
93
94 public Reader getReader(Object templateSource, String encoding)
95 throws
96 IOException
97 {
98 return new InputStreamReader(
99 ((URLTemplateSource) templateSource).getInputStream(),
100 encoding);
101 }
102
103 public void closeTemplateSource(Object templateSource)
104 throws
105 IOException
106 {
107 ((URLTemplateSource) templateSource).close();
108 }
109
110 /**
111 * Can be used by subclasses to canonicalize URL path prefixes.
112 * @param prefix the path prefix to canonicalize
113 * @return the canonicalized prefix. All backslashes are replaced with
114 * forward slashes, and a trailing slash is appended if the original
115 * prefix wasn't empty and didn't already end with a slash.
116 */
117 protected static String canonicalizePrefix(String prefix)
118 {
119 // make it foolproof
120 prefix = prefix.replace('\\', '/');
121 // ensure there's a trailing slash
122 if (prefix.length() > 0 && !prefix.endsWith("/"))
123 {
124 prefix += "/";
125 }
126 return prefix;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…