You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Xavier Noria <fx...@hashref.com> on 2005/07/18 10:59:04 UTC

instantiating templates from Strings and absolute paths

I am writing an application that needs a couple of functionalities I  
don't see in the developer's manual.

On the one hand I have snippets of templates in a XML file. I've read  
Velocity.evaluate() can instantiate them, but I would prefer to have  
a "precompiled" Template object (I'm new to Velocity and I am in fact  
assuming an object of type Template is a precompiled template). Is  
that possible or should I need to write my own StringReso

On the other hand, looks like there is no direct way to create  
Template objects given an absolute path, except by computing its  
dirname, setting it as template root dir, then load the basename as  
relative path. I've seen another workaround which consists of setting  
the root dir to the empty string. Isn't there a more convinient way  
like a simple method call?

-- fxn



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


Re: instantiating templates from Strings and absolute paths

Posted by Will Glass-Husain <wg...@forio.com>.
There's a patch in progress to (optionally) make the file resource loader 
handle absolute paths.

There was some security concerns raised but I think they should be resolved, 
so we'll likely add that back in.

And yes, you should be able to configure multiple loaders - Velocity should 
search through the loaders in order for the template.

WILL

----- Original Message ----- 
From: "Xavier Noria" <fx...@hashref.com>
To: <ve...@jakarta.apache.org>
Sent: Thursday, July 28, 2005 5:58 AM
Subject: Re: instantiating templates from Strings and absolute paths


> On Jul 18, 2005, at 10:59, Xavier Noria wrote:
>
>> On the other hand, looks like there is no direct way to create  Template 
>> objects given an absolute path, except by computing its  dirname, setting 
>> it as template root dir, then load the basename as  relative path. I've 
>> seen another workaround which consists of  setting the root dir to the 
>> empty string. Isn't there a more  convinient way like a simple method 
>> call?
>
> Finally I wrote a simple ResourceLoader that only accepts absolute  paths, 
> it is just what I need and has been tested on Mac OS X.
>
> From the Velocity source code looks like if a loader does not handle  some 
> resource it raises ResourceNotFoundException so that the  resource manager 
> can try other loaders if more than one is  configured. Some quick tests 
> suggest this is right in 1.4, but the  ResourceManager interface does not 
> document such contract.
>
> -- fxn
>
> package com.example;
>
> import java.io.*;
> import org.apache.commons.collections.ExtendedProperties;
> import org.apache.velocity.exception.ResourceNotFoundException;
> import org.apache.velocity.runtime.resource.Resource;
> import org.apache.velocity.runtime.resource.loader.ResourceLoader;
>
> public class AbsolutePathResourceLoader extends ResourceLoader {
>
>     public void init(ExtendedProperties configuration) {
>         // do nothing
>     }
>
>     public synchronized InputStream getResourceStream(String fileName)
>             throws ResourceNotFoundException {
>         if (! new File(fileName).isAbsolute())
>             throw new ResourceNotFoundException("'" + fileName + "'  is 
> not an absolute path");
>         try {
>             return new BufferedInputStream(new FileInputStream 
> (fileName));
>         } catch (IOException wrapped) {
>             throw new ResourceNotFoundException(wrapped.getMessage());
>         }
>     }
>
>     public boolean isSourceModified(Resource resource) {
>         return getLastModified(resource) != resource.getLastModified();
>     }
>
>     public long getLastModified(Resource resource) {
>         return new File(resource.getName()).lastModified();
>     }
> }
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 


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


Re: instantiating templates from Strings and absolute paths

Posted by Xavier Noria <fx...@hashref.com>.
On Jul 18, 2005, at 10:59, Xavier Noria wrote:

> On the other hand, looks like there is no direct way to create  
> Template objects given an absolute path, except by computing its  
> dirname, setting it as template root dir, then load the basename as  
> relative path. I've seen another workaround which consists of  
> setting the root dir to the empty string. Isn't there a more  
> convinient way like a simple method call?

Finally I wrote a simple ResourceLoader that only accepts absolute  
paths, it is just what I need and has been tested on Mac OS X.

 From the Velocity source code looks like if a loader does not handle  
some resource it raises ResourceNotFoundException so that the  
resource manager can try other loaders if more than one is  
configured. Some quick tests suggest this is right in 1.4, but the  
ResourceManager interface does not document such contract.

-- fxn

package com.example;

import java.io.*;
import org.apache.commons.collections.ExtendedProperties;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;

public class AbsolutePathResourceLoader extends ResourceLoader {

     public void init(ExtendedProperties configuration) {
         // do nothing
     }

     public synchronized InputStream getResourceStream(String fileName)
             throws ResourceNotFoundException {
         if (! new File(fileName).isAbsolute())
             throw new ResourceNotFoundException("'" + fileName + "'  
is not an absolute path");
         try {
             return new BufferedInputStream(new FileInputStream 
(fileName));
         } catch (IOException wrapped) {
             throw new ResourceNotFoundException(wrapped.getMessage());
         }
     }

     public boolean isSourceModified(Resource resource) {
         return getLastModified(resource) != resource.getLastModified();
     }

     public long getLastModified(Resource resource) {
         return new File(resource.getName()).lastModified();
     }
}





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


Re: instantiating templates from Strings and absolute paths

Posted by Christian Reichlin <ch...@kerbtier.ch>.
i had the same problem and i solved it by caching the SimpleNodes which 
are produced by the parse method. i didn't profile my application so i 
don't know if it really works well and it might be a quite stupid way, i 
hadn't time to look at it properly.

private Map<String, SimpleNode> rootNodesCache = new HashMap<String, 
SimpleNode>();
   
public SimpleNode getRootNodeFor(String template, RuntimeInstance 
runtimeInstance) {

        SimpleNode sn = rootNodesCache.get(template);

        if(sn == null) {
            try {
                Reader reader = new StringReader(template);
                sn = runtimeInstance.parse(reader, tag);

                rootNodesCache.put( template, sn);
            } catch(ParseException e) {
                logger.error("VelocityException for template '" + value 
+ "' :" + e.getMessage());
            }
        }
        return sn;
}


Xavier Noria wrote:

> I am writing an application that needs a couple of functionalities I  
> don't see in the developer's manual.
>
> On the one hand I have snippets of templates in a XML file. I've read  
> Velocity.evaluate() can instantiate them, but I would prefer to have  
> a "precompiled" Template object (I'm new to Velocity and I am in fact  
> assuming an object of type Template is a precompiled template). Is  
> that possible or should I need to write my own StringReso
>
> On the other hand, looks like there is no direct way to create  
> Template objects given an absolute path, except by computing its  
> dirname, setting it as template root dir, then load the basename as  
> relative path. I've seen another workaround which consists of setting  
> the root dir to the empty string. Isn't there a more convinient way  
> like a simple method call?
>
> -- fxn
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>

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