public class StringResourceLoader extends ResourceLoader
StringResourceRepositoryImpl as the default.
resource.loader = string string.resource.loader.description = Velocity StringResource loader string.resource.loader.class = bboss.org.apache.velocity.runtime.resource.loader.StringResourceLoader string.resource.loader.repository.class = bboss.org.apache.velocity.runtime.resource.util.StringResourceRepositoryImplResources can be added to the repository like this:
StringResourceRepository repo = StringResourceLoader.getRepository();
String myTemplateName = "/some/imaginary/path/hello.vm";
String myTemplate = "Hi, ${username}... this is some template!";
repo.putStringResource(myTemplateName, myTemplate);
After this, the templates can be retrieved as usual.
If there will be multiple StringResourceLoaders used in an application, you should consider specifying a 'string.resource.loader.repository.name = foo' property in order to keep you string resources in a non-default repository. This can help to avoid conflicts between different frameworks or components that are using StringResourceLoader. You can then retrieve your named repository like this:
StringResourceRepository repo = StringResourceLoader.getRepository("foo");
and add string resources to the repo just as in the previous example.
If you have concerns about memory leaks or for whatever reason do not wish to have your string repository stored statically as a class member, then you should set 'string.resource.loader.repository.static = false' in your properties. This will tell the resource loader that the string repository should be stored in the Velocity application attributes. To retrieve the repository, do:
StringResourceRepository repo = velocityEngine.getApplicationAttribute("foo");
If you did not specify a name for the repository, then it will be stored under the
class name of the repository implementation class (for which the default is
'bboss.org.apache.velocity.runtime.resource.util.StringResourceRepositoryImpl').
Incidentally, this is also true for the default statically stored repository.
Whether your repository is stored statically or in Velocity's application attributes, you can also manually create and set it prior to Velocity initialization. For a static repository, you can do something like this:
StringResourceRepository repo = new MyStringResourceRepository();
repo.magicallyAddSomeStringResources();
StringResourceLoader.setRepository("foo", repo);
Or for a non-static repository:
StringResourceRepository repo = new MyStringResourceRepository();
repo.magicallyAddSomeStringResources();
velocityEngine.setApplicationAttribute("foo", repo);
Then, assuming the 'string.resource.loader.repository.name' property is
set to 'some.name', the StringResourceLoader will use that already created
repository, rather than creating a new one.
| 限定符和类型 | 字段和说明 |
|---|---|
protected StringResourceRepository |
repository |
static java.lang.String |
REPOSITORY_CLASS
Key to look up the repository implementation class.
|
static java.lang.String |
REPOSITORY_CLASS_DEFAULT
The default implementation class.
|
static java.lang.String |
REPOSITORY_ENCODING
Key to look up the repository char encoding.
|
static java.lang.String |
REPOSITORY_ENCODING_DEFAULT
The default repository encoding.
|
static java.lang.String |
REPOSITORY_NAME
Key to look up the name for the repository to be used.
|
static java.lang.String |
REPOSITORY_NAME_DEFAULT
The default name for string resource repositories
('bboss.org.apache.velocity.runtime.resource.util.StringResourceRepository').
|
static java.lang.String |
REPOSITORY_STATIC
Key to determine whether the repository should be set as the static one or not.
|
static boolean |
REPOSITORY_STATIC_DEFAULT
By default, repositories are stored statically (shared across the VM).
|
protected static java.util.Map |
STATIC_REPOSITORIES |
className, isCachingOn, log, modificationCheckInterval, rsvc| 构造器和说明 |
|---|
StringResourceLoader() |
| 限定符和类型 | 方法和说明 |
|---|---|
static void |
clearRepositories()
Removes all statically stored
StringResourceRepositorys. |
StringResourceRepository |
createRepository(java.lang.String className,
java.lang.String encoding) |
long |
getLastModified(Resource resource)
Get the last modified time of the InputStream source
that was used to create the template.
|
static StringResourceRepository |
getRepository()
Returns a reference to the default static repository.
|
static StringResourceRepository |
getRepository(java.lang.String name)
Returns a reference to the repository stored statically under the
specified name.
|
java.io.InputStream |
getResourceStream(java.lang.String name)
Get an InputStream so that the Runtime can build a
template with it.
|
void |
init(org.apache.commons.collections.ExtendedProperties configuration)
Initialize the template loader with a
a resources class.
|
boolean |
isSourceModified(Resource resource)
Given a template, check to see if the source of InputStream
has been modified.
|
static StringResourceRepository |
removeRepository(java.lang.String name)
Removes the
StringResourceRepository stored under the specified
name. |
boolean |
resourceExists(java.lang.String name)
Overrides superclass for better performance.
|
static void |
setRepository(java.lang.String name,
StringResourceRepository repo)
Sets the specified
StringResourceRepository in static storage
under the specified name. |
commonInit, getClassName, getModificationCheckInterval, isCachingOn, setCachingOn, setModificationCheckIntervalpublic static final java.lang.String REPOSITORY_STATIC
public static final boolean REPOSITORY_STATIC_DEFAULT
public static final java.lang.String REPOSITORY_CLASS
public static final java.lang.String REPOSITORY_CLASS_DEFAULT
public static final java.lang.String REPOSITORY_NAME
public static final java.lang.String REPOSITORY_NAME_DEFAULT
public static final java.lang.String REPOSITORY_ENCODING
public static final java.lang.String REPOSITORY_ENCODING_DEFAULT
protected static final java.util.Map STATIC_REPOSITORIES
protected StringResourceRepository repository
public static StringResourceRepository getRepository()
public static StringResourceRepository getRepository(java.lang.String name)
public static void setRepository(java.lang.String name,
StringResourceRepository repo)
StringResourceRepository in static storage
under the specified name.public static StringResourceRepository removeRepository(java.lang.String name)
StringResourceRepository stored under the specified
name.public static void clearRepositories()
StringResourceRepositorys.public void init(org.apache.commons.collections.ExtendedProperties configuration)
ResourceLoaderpublic StringResourceRepository createRepository(java.lang.String className, java.lang.String encoding)
public boolean resourceExists(java.lang.String name)
resourceExists 在类中 ResourceLoadername - The name of a resource.public java.io.InputStream getResourceStream(java.lang.String name)
throws ResourceNotFoundException
getResourceStream 在类中 ResourceLoadername - name of template to get.ResourceNotFoundException - Ff template not found
in the RepositoryFactory.public boolean isSourceModified(Resource resource)
ResourceLoaderisSourceModified 在类中 ResourceLoaderResourceLoader.isSourceModified(bboss.org.apache.velocity.runtime.resource.Resource)public long getLastModified(Resource resource)
ResourceLoadergetLastModified 在类中 ResourceLoaderResourceLoader.getLastModified(bboss.org.apache.velocity.runtime.resource.Resource)