Class StringResourceLoader

java.lang.Object
org.apache.velocity.runtime.resource.loader.ResourceLoader
org.apache.velocity.runtime.resource.loader.StringResourceLoader
Direct Known Subclasses:
VelocityScriptEngine.SingleResourceReader

public class StringResourceLoader extends ResourceLoader
Resource loader that works with Strings. Users should manually add resources to the repository that is used by the resource loader instance. Below is an example configuration for this loader. Note that 'repository.class' is not necessary; if not provided, the factory will fall back on using StringResourceRepositoryImpl as the default.
 resource.loaders = string
 resource.loader.string.description = Velocity StringResource loader
 resource.loader.string.class = org.apache.velocity.runtime.resource.loader.StringResourceLoader
 resource.loader.string.repository.name = MyRepositoryName (optional, to avoid using the default repository)
 resource.loader.string.repository.class = org.apache.velocity.runtime.resource.loader.StringResourceRepositoryImpl
 
Resources 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 'resource.loader.string.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 'resource.loader.string.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 '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 'resource.loader.string.repository.name' property is set to 'some.name', the StringResourceLoader will use that already created repository, rather than creating a new one.

Since:
1.5
Version:
$Id$
Author:
Eelco Hillenius, Henning P. Schmiedehausen, Nathan Bubna