Package freemarker.cache
Class ClassTemplateLoader
- java.lang.Object
-
- freemarker.cache.URLTemplateLoader
-
- freemarker.cache.ClassTemplateLoader
-
- All Implemented Interfaces:
SecureTemplateLoader,TemplateLoader
public class ClassTemplateLoader extends URLTemplateLoader
ATemplateLoaderthat uses streams reachable throughClass.getResourceAsStream(String)as its source of templates.
-
-
Constructor Summary
Constructors Constructor Description ClassTemplateLoader()Deprecated.confusing constructor, and seldom useful; useClassTemplateLoader(Class, String)instead.ClassTemplateLoader(Class loaderClass)Deprecated.it is confusing that the base path is""; useClassTemplateLoader(Class, String)instead.ClassTemplateLoader(Class loaderClass, String path)Equivalent to invokingClassTemplateLoader(Class, String, boolean)with false as the last argument.ClassTemplateLoader(Class loaderClass, String path, boolean useClassCodeSource)Creates a template loader that will use theClass.getResource(String)method of the specified class to load the resources, and the specified base path (absolute or relative).
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CodeSourcegetCodeSource(Object templateSource)For jar: URLs, returns a code source that points to the URL of the JAR file as the code source URL.protected URLgetURL(String name)Given a template name (plus potential locale decorations) retrieves an URL that points the template source.-
Methods inherited from class freemarker.cache.URLTemplateLoader
canonicalizePrefix, closeTemplateSource, findTemplateSource, getLastModified, getReader
-
-
-
-
Constructor Detail
-
ClassTemplateLoader
public ClassTemplateLoader()
Deprecated.confusing constructor, and seldom useful; useClassTemplateLoader(Class, String)instead.Creates a template loader that will use theClass.getResource(String)method of its own class to load the resources, and"/"as base path. This means that that template paths will be resolved relatvively the root package of the class hierarchy, so you hardly ever should use this constructor, rather do something like this:
new ClassTemplateLoader(com.example.myapplication.SomeClass.class, "templates")If you extend this class, then the extending class will be used to load the resources.
Warning: this constructor was malfunctioned prior FreeMarker 2.3.4 -- please update FreeMarker if needed.
-
ClassTemplateLoader
public ClassTemplateLoader(Class loaderClass)
Deprecated.it is confusing that the base path is""; useClassTemplateLoader(Class, String)instead.Creates a template loader that will use theClass.getResource(String)method of the specified class to load the resources, and""as base path. This means that template paths will be resolved relatively to the class location, that is, relatively to the directory (package) of the class.- Parameters:
loaderClass- the class whoseClass.getResource(String)will be used to load the templates.
-
ClassTemplateLoader
public ClassTemplateLoader(Class loaderClass, String path)
Equivalent to invokingClassTemplateLoader(Class, String, boolean)with false as the last argument.- Parameters:
loaderClass- the class whoseClass.getResource(String)method will be used to load the templates. Be sure that you chose a class whose defining class-loader sees the templates. This parameter can't benull.path- the base path to template resources. A path that doesn't start with a slash (/) is relative to the path (package) of the specified class. A path that starts with a slash is an absolute path starting from the root of the package hierarchy. Path components should be separated by forward slashes independently of the separator character used by the underlying operating system. This parameter can't benull.
-
ClassTemplateLoader
public ClassTemplateLoader(Class loaderClass, String path, boolean useClassCodeSource)
Creates a template loader that will use theClass.getResource(String)method of the specified class to load the resources, and the specified base path (absolute or relative).Examples:
- Relative base path (will load from the
com.example.myapplication.templatespackage):
new ClassTemplateLoader(
com.example.myapplication.SomeClass.class,
"templates") - Absolute base path:
new ClassTemplateLoader(
somepackage.SomeClass.class,
"/com/example/myapplication/templates")
- Parameters:
loaderClass- the class whoseClass.getResource(String)method will be used to load the templates. Be sure that you chose a class whose defining class-loader sees the templates. This parameter can't benull.path- the base path to template resources. A path that doesn't start with a slash (/) is relative to the path (package) of the specified class. A path that starts with a slash is an absolute path starting from the root of the package hierarchy. Path components should be separated by forward slashes independently of the separator character used by the underlying operating system. This parameter can't benull.useClassCodeSource- if true, the code source of the class is used as the coude source for all templates loaded through this loader. If false, thenURLTemplateLoader.getCodeSource(Object)will be used to obtain (a different) code source for each template.
- Relative base path (will load from the
-
-
Method Detail
-
getURL
protected URL getURL(String name)
Description copied from class:URLTemplateLoaderGiven a template name (plus potential locale decorations) retrieves an URL that points the template source.- Specified by:
getURLin classURLTemplateLoader- Parameters:
name- the name of the sought template, including the locale decorations.- Returns:
- an URL that points to the template source, or null if it can determine that the template source does not exist.
-
getCodeSource
public CodeSource getCodeSource(Object templateSource) throws IOException
Description copied from class:URLTemplateLoaderFor jar: URLs, returns a code source that points to the URL of the JAR file as the code source URL. If the JAR file is signed, the code source will contain the appropriate certificates as well. For other URLs, returns the code source with URL itself and no certificates.- Specified by:
getCodeSourcein interfaceSecureTemplateLoader- Overrides:
getCodeSourcein classURLTemplateLoader- Parameters:
templateSource- the template source for which a code source is requested.- Returns:
- an appropriate CodeSource for this template source.
- Throws:
IOException- if an I/O exception occurs while trying to obtain the code source.
-
-