You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "simon.kitching@chello.at" <si...@chello.at> on 2008/06/16 12:41:52 UTC

ResourceLoader: loading files relative to template; using custom ResourceLoader

Hi,

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:
     velocityEngine.evaluate($file)

Within the processed template files, I would like things like
   #parse("foo.inc")
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:
     customResourceLoader.setBasePath($file)
     velocityEngine.evaluate($file)

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 =
StringUtils.nullTrim(configuration.getString("class"));
            ResourceLoader loaderInstance = (ResourceLoader)
configuration.get("instance");
            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);
        e.init(p);

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.

So as the init method is currently written, it appears to support
nothing but String values in the Properties object passed to init.

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

I've checked the FAQ and the email archives and found no hints. Can
anyone offer a hint?

Thanks,
Simon


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org