public class ClassTemplateLoader extends URLTemplateLoader
TemplateLoader
that can load templates from the "classpath". Naturally, it can load from jar files, or from
anywhere where Java can load classes from. Internally, it uses Class.getResource(String)
or
ClassLoader.getResource(String)
to load templates.Constructor and Description |
---|
ClassTemplateLoader()
Deprecated.
It's a confusing constructor, and seldom useful; use
ClassTemplateLoader(Class, String)
instead. |
ClassTemplateLoader(java.lang.Class<?> resourceLoaderClass)
Deprecated.
It's confusing that the base path is
"" ; use ClassTemplateLoader(Class, String)
instead. |
ClassTemplateLoader(java.lang.Class<?> resourceLoaderClass,
java.lang.String basePackagePath)
Creates a template loader that will use the
Class.getResource(String) method of the specified class to
load the resources, and the specified base package path (absolute or relative). |
ClassTemplateLoader(java.lang.ClassLoader classLoader,
java.lang.String basePackagePath)
Similar to
ClassTemplateLoader(Class, String) , but instead of Class.getResource(String) it uses
ClassLoader.getResource(String) . |
Modifier and Type | Method and Description |
---|---|
java.lang.String |
getBasePackagePath()
See the similar parameter of
ClassTemplateLoader(ClassLoader, String) ; note that this is a normalized
version of what was actually passed to the constructor. |
java.lang.ClassLoader |
getClassLoader()
See the similar parameter of
ClassTemplateLoader(ClassLoader, String) ; null when other mechanism
is used to load the resources. |
java.lang.Class |
getResourceLoaderClass()
See the similar parameter of
ClassTemplateLoader(Class, String) ; null when other mechanism is
used to load the resources. |
protected java.net.URL |
getURL(java.lang.String name)
Given a template name (plus potential locale decorations) retrieves
an URL that points the template source.
|
java.lang.String |
toString()
Show class name and some details that are useful in template-not-found errors.
|
canonicalizePrefix, closeTemplateSource, findTemplateSource, getLastModified, getReader, getURLConnectionUsesCaches, setURLConnectionUsesCaches
@Deprecated public ClassTemplateLoader()
ClassTemplateLoader(Class, String)
instead.Class.getResource(String)
method of its own class to load the
resources, and "/"
as base package path. This means that that template paths will be resolved relatively
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.
@Deprecated public ClassTemplateLoader(java.lang.Class<?> resourceLoaderClass)
""
; use ClassTemplateLoader(Class, String)
instead.Class.getResource(String)
method of the specified class to
load the resources, and ""
as base package path. This means that template paths will be resolved
relatively to the class location, that is, relatively to the directory (package) of the class.resourceLoaderClass
- the class whose Class.getResource(String)
will be used to load the templates.public ClassTemplateLoader(java.lang.Class<?> resourceLoaderClass, java.lang.String basePackagePath)
Class.getResource(String)
method of the specified class to
load the resources, and the specified base package path (absolute or relative).
Examples:
com.example.myapplication.templates
package):new ClassTemplateLoader(com.example.myapplication.SomeClass.class, "templates")
new ClassTemplateLoader(somepackage.SomeClass.class, "/com/example/myapplication/templates")
resourceLoaderClass
- The class whose Class.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 be
null
.basePackagePath
- The package that contains the templates, in path (/
-separated) format. If it doesn't start
with a /
then it's relative to the path (package) of the resourceLoaderClass
class. If
it starts with /
then it's relative to the root of the package hierarchy. Note that path
components should be separated by forward slashes independently of the separator character used by the
underlying operating system. This parameter can't be null
.ClassTemplateLoader(ClassLoader, String)
public ClassTemplateLoader(java.lang.ClassLoader classLoader, java.lang.String basePackagePath)
ClassTemplateLoader(Class, String)
, but instead of Class.getResource(String)
it uses
ClassLoader.getResource(String)
. Because a ClassLoader
isn't bound to any Java package, it
doesn't mater if the basePackagePath
starts with /
or not, it will be always relative to the root
of the package hierarchyprotected java.net.URL getURL(java.lang.String name)
URLTemplateLoader
getURL
in class URLTemplateLoader
name
- the name of the sought template, including the locale
decorations.null
if the template does not exist.public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.Class getResourceLoaderClass()
ClassTemplateLoader(Class, String)
; null
when other mechanism is
used to load the resources.public java.lang.ClassLoader getClassLoader()
ClassTemplateLoader(ClassLoader, String)
; null
when other mechanism
is used to load the resources.public java.lang.String getBasePackagePath()
ClassTemplateLoader(ClassLoader, String)
; note that this is a normalized
version of what was actually passed to the constructor.