velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <>
Subject ResourceLoader: loading files relative to template; using custom ResourceLoader
Date Mon, 16 Jun 2008 10:45:21 GMT

I'm using Velocity 1.5 as an install-time tool to generate configuration
files. Effectively I do:
  initialise velocity
  for each file named ".vm" under $installdir:

Within the processed template files, I would like thinks like
to look for the file relative to the one being processed.

It doesn't look like the standard FileResourceLoader will do that; it
expects to be configured with a fixed "root" directory at the time that
the VelocityEngine is initialised.

Therefore I want to use a custom ResourceLoader, and inform it of the
current "path":
  create custom ResourceLoader instance
  initialise velocity, attaching custom ResourceLoader
  for each file named ".vm" under $installdir:

I see in ResourceManagerImpl that there is support for passing an
*instance* of ResourceLoader rather than just the classname. This is
useful as it allows me to directly call setBasePath before processing
each input template.

But I just cannot get this "instance" support to work.

>From ResourceManagerImpl.initialize:
            String loaderClass =
            ResourceLoader loaderInstance = (ResourceLoader)

            if (loaderInstance != null)
                resourceLoader = loaderInstance;

This suggests to me that I should be doing this:
        CustomResourceLoader loader = new CustomResourceLoader().
        VelocityEngine e = new VelocityEngine();
        Properties p = new Properties();
        p.put( "resource.loader", "local" );
        p.put( "local.resource.loader.instance", loader);

However this causes a NullPointerException. The reason is that the
Properties object is converted into an ExtendedProperties object
internally. And that is done by iterating over the keys of the
Properties object, calling getProperty(key) for each one. But
getProperty is defined to return a String; when the corresponding value
for the key is not a String then null is returned - thus discarding my
custom ResourceLoader instance.

I cannot see any way to add a ResourceLoader instance to a
VelocityEngine instance after init() has been called.

Any suggestions?


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message