Class StringResourceLoader


  • 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.loader = string
     string.resource.loader.description = Velocity StringResource loader
     string.resource.loader.class = org.apache.velocity.runtime.resource.loader.StringResourceLoader
     string.resource.loader.repository.class = org.apache.velocity.runtime.resource.util.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 '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 '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.

    Since:
    1.5
    Version:
    $Id: StringResourceLoader.java 825302 2009-10-14 21:51:39Z nbubna $
    • Field Detail

      • REPOSITORY_STATIC

        public static final java.lang.String REPOSITORY_STATIC
        Key to determine whether the repository should be set as the static one or not.
        Since:
        1.6
        See Also:
        Constant Field Values
      • REPOSITORY_STATIC_DEFAULT

        public static final boolean REPOSITORY_STATIC_DEFAULT
        By default, repositories are stored statically (shared across the VM).
        Since:
        1.6
        See Also:
        Constant Field Values
      • REPOSITORY_CLASS

        public static final java.lang.String REPOSITORY_CLASS
        Key to look up the repository implementation class.
        See Also:
        Constant Field Values
      • REPOSITORY_CLASS_DEFAULT

        public static final java.lang.String REPOSITORY_CLASS_DEFAULT
        The default implementation class.
      • REPOSITORY_NAME

        public static final java.lang.String REPOSITORY_NAME
        Key to look up the name for the repository to be used.
        Since:
        1.6
        See Also:
        Constant Field Values
      • REPOSITORY_NAME_DEFAULT

        public static final java.lang.String REPOSITORY_NAME_DEFAULT
        The default name for string resource repositories ('org.apache.velocity.runtime.resource.util.StringResourceRepository').
        Since:
        1.6
      • REPOSITORY_ENCODING

        public static final java.lang.String REPOSITORY_ENCODING
        Key to look up the repository char encoding.
        See Also:
        Constant Field Values
      • REPOSITORY_ENCODING_DEFAULT

        public static final java.lang.String REPOSITORY_ENCODING_DEFAULT
        The default repository encoding.
        See Also:
        Constant Field Values
      • STATIC_REPOSITORIES

        protected static final java.util.Map STATIC_REPOSITORIES
    • Constructor Detail

      • StringResourceLoader

        public StringResourceLoader()