velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <skitch...@apache.org>
Subject ResourceLoader: loading files relative to template; using custom ResourceLoader
Date Mon, 16 Jun 2008 10:45:21 GMT
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 thinks 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.

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

Any suggestions?

Thanks,
Simon


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


Mime
View raw message