You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Geir Magnusson Jr." <ge...@optonline.net> on 2001/11/07 14:40:10 UTC

Re: Problem Using Anakia with velocity.properties and templatePath (v elocity-1.2-rc2)

On 11/6/01 5:45 PM, "Hensley, Richard" <Ri...@McKesson.com> wrote:

> I'm using Anakia to build some static web pages, and I came across a
> problem when I use a velocity.properties file and a templatePath
> attribute. It seems that the templatePath is not used when the
> velocity.properties file is present.
> 
> The documentation says that the templatePath attribute of the Anakia
> task overrides the path defined in the velocity.properties. My
> velocity.properites contained only the following:
> 
> file.resource.loader.cache = true
> 
> However when I ran ant, I got the following error:
> 
>  [anakia] Error: 
> org.apache.velocity.exception.ResourceNotFoundException: Unable to find
> resource './site.vsl'
> 
> This sounded like a path problem so I started investigating the Anakia
> code and I found the following snippet in the Anakia.execute method:
> 
>           if ( velocityPropertiesFile.exists() )
>           { 
>               ve.init(velocityPropertiesFile.getAbsolutePath());
>           } 
>           else if (templatePath != null && templatePath.length() > 0)
>           { 
>               ve.setProperty(
>                         RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
>                   templatePath);
>               ve.init();
>           } 
> 
> If I'm reading this code correctly, it says if there is a velocity
> properties file, use it. Otherwise, set the file resource loader path to
> the template path.
> 
> I found this in velocity-1.2-rc2. I check the most recent
> AnakiaTask.java in cvs, and it has the same code  snippet.
> 
> I'm not sure which, but either the documentation is wrong, or the code
> is wrong. 
> 
> What should the fixed?
> 
> Would a legimate fix be to simply remove the "else"? Thus the code would
> read: 
> 
>           if ( velocityPropertiesFile.exists() )
>           { 
>               ve.init(velocityPropertiesFile.getAbsolutePath());
>           } 
>           if (templatePath != null && templatePath.length() > 0)
>           { 
>               ve.setProperty(
>                         RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
>                   templatePath);
>               ve.init();
>           } 
> 
> This would have init() being called twice. I'm not sure this works in
> Velocity. 

No, because once init()-ed, velocity doesn't pay attention to subsequent
inits.

The solution isn't clear to me.

On approach is to make the templatePath argument override whatever is in the
velocity.properties :


  Properties p = new Properties();
  p.load( velocityPropertiesFile.getAbsolutePath() );

  if( templatePath != null && etc... )
  { 
     p.set( RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templatePath );
  }

  ve.init( p );


That way you can set a default in the properties, yet override with the
template path on each invocation if you want.

The downside of this is the confusion that could arise if you are using a
jar for the templates, and setting the loader to be only the classpath
loader.  Then templatePath will have no effect.  The only way to be nice
about that is to start analyzing the properties file, which I think is
something you don't want to do in anakia.

So maybe you take a step forward and with the code above, state in docs that

0) Properties rule.
1) the templatePath can only override the file loader's template path, and
if you get clever and use another loader, templatePath will have no effect.

?

Geir



-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"He who throws mud only loses ground." - Fat Albert


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>