velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Xavier Noria <>
Subject Re: instantiating templates from Strings and absolute paths
Date Thu, 28 Jul 2005 12:58:31 GMT
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 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 
         } 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:
For additional commands, e-mail:

View raw message