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" <[EMAIL PROTECTED]>
To: <[email protected]>
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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to