« Return to Thread: mergeTemplate - non-standard resource

Re: mergeTemplate - non-standard resource

by Byron Foster-2 :: Rate this Message:

Reply to Author | View in Thread


On May 5, 2009, at 5:01 , Mark Fenbers wrote:

> I need to give some background info before I ask the Velocity  
> question...
>
> I've changed my code to use class resources instead of absolute or  
> relative pathnames.  In other words, I changed this:
>
> Image myImage = new Image(display, "bin/gov/noaa/ohrfc/resources/
> rvf.icon.png");
>
> to this:
>
> Image myImage = new Image(display,  
> this.getClass().getResource("resources/rvf.icon.png").getFile());
>
> This is less direct, but has several advantages, including that  
> getResource uses the class location (bin/gov/noaa/ohrfc) to complete  
> the full pathname of "resource/rvf.icon.png". The getFile() method  
> will return the full pathname of the file (which is: /home/oper/
> workspace/GIFTS/bin/gov/noaa/ohrfc/resources/rvf.icon.png).  This  
> makes it more portable, and makes it more compatible with using Java  
> Web Start for this application.  Using getResource(), my code  
> doesn't break if I move my classes somewhere else besides "bin/gov/
> noaa/ohrfc".
>
> The problem comes in with Velocity's mergeTemplate() method which  
> chokes when an absolute pathname is used.  It also chokes when I use  
> a pathname that is relative to the class location.  Rather, it  
> requires something in between whereby I have to specify the class  
> location *and* the relative path to the template file, namely, I  
> need to specify "bin/gov/noaa/ohrfc/resources/SHEF.template",  
> whereas I want to specify "resources/SHEF.template" or give it the  
> full absolute pathname.
>
> I am using version 1.6.1-dep.  Does a later version fix this  
> problem?  Or is there another work-around where I can either: 1)  
> avoid giving the class location (bin/gov/noaa/ohrfc)' or 2) give the  
> entire absolute path location [which can be determined on-the-fly  
> using getResource().getFile()]?

Could you do something like so with your absolute paths:

file.resource.loader.path = /

There may be security considerations, but you could also set the  
resource loader path to something more reasonable, and within a  
wrapper method around templateMerge you could remove any base  
directory from the merge path.



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

 « Return to Thread: mergeTemplate - non-standard resource