dimo414
dimo414

Reputation: 48864

Using An Absolute Path With FreeMarker

I've been using FreeMarker for a little while now, but there's one glaring piece of functionality that is either missing or I just can't figure out (I hope the latter!). If you pass cfg.getTemplate() an absolute path, it just doesn't work. I know you can specify a template directory, but I can't afford to do that, my use case could deal with files in any directory. Is there any way to set FreeMarker to render absolute paths the way any user would expect?

Upvotes: 1

Views: 6575

Answers (4)

Erhard Siegl
Erhard Siegl

Reputation: 577

The problem with the accepted solution is, that the path name is destroyed in FreeMarker before the TemplateLoader is used. See TemplateCache:

    name = normalizeName(name);
    if(name == null) {
        return null;
    }
    Template result = null;
    if (templateLoader != null) {
        result = getTemplate(templateLoader, name, locale, encoding, parseAsFTL);
    }

So I think it is better to use a solution as proposed in this answer

For example

        Configuration config = new Configuration();
        File templateFile = new File(templateFilename);
        File templateDir = templateFile.getParentFile();
        if ( null == templateDir ){
            templateDir = new File("./");
        }
        config.setDirectoryForTemplateLoading(templateDir);
        Template template = config.getTemplate(templateFile.getName());

Upvotes: 0

Choppy The Lumberjack
Choppy The Lumberjack

Reputation: 777

Actually it remove the beginning "/" so you need to add it back in

public Object findTemplateSource(String name) throws IOException {
    File source = new File("/" + name);
    return source.isFile() ? source : null;
}

Upvotes: 2

Vladimir
Vladimir

Reputation: 6871

I had to use the absolute path because the templating is happening in an Ant script and the templates are on the file system and discovered with an Ant fileset. I guess these are quite some unique requirements...

Anyway,for the posterity (as long as SO is up), here's a solution that works:

public class TemplateAbsolutePathLoader implements TemplateLoader {

    public Object findTemplateSource(String name) throws IOException {
        File source = new File(name);
        return source.isFile() ? source : null;
    }

    public long getLastModified(Object templateSource) {
        return ((File) templateSource).lastModified();
    }

    public Reader getReader(Object templateSource, String encoding)
            throws IOException {
        if (!(templateSource instanceof File)) {
            throw new IllegalArgumentException("templateSource is a: " + templateSource.getClass().getName());
        }
        return new InputStreamReader(new FileInputStream((File) templateSource), encoding);
    }

    public void closeTemplateSource(Object templateSource) throws IOException {
        // Do nothing.
    }

}

and the initialization is:

public String generate(File template) {

    Configuration cfg = new Configuration();
    cfg.setTemplateLoader(new TemplateAbsolutePathLoader());
    Template tpl = cfg.getTemplate(template.getAbsolutePath());

    // ...
}

Upvotes: 6

ChssPly76
ChssPly76

Reputation: 100736

Freemarker uses FileTemplateLoader by default that will not allow you to get templates from outside of "base" directory (which by default is taken from 'user.dir' system property, so it's your home dir). What you can do is:

  1. Explicitly create FileTemplateLoader with baseDir set to your top-most directory from under which you'll be getting templates (you could in theory set it to root in order to use absolute paths but that's a VERY BAD THING © from security standpoint).
  2. Write your own template loader that would take an absolute path but then ensure the template is still inside your template folder. If you do that, take care to compare canonical file paths.
  3. Rethink your approach. Do you really need absolute paths for templates?

Upvotes: 1

Related Questions