1

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?

dimo414
  • 47,227
  • 18
  • 148
  • 244

4 Answers4

6

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());

    // ...
}
Vladimir
  • 6,853
  • 2
  • 26
  • 25
  • Thank you very much. I'm no longer developing this project, so I can't test this, but I'm sure anyone else with my same question will appreciate this. – dimo414 Oct 26 '09 at 22:59
  • I accepted your answer since the community seems to agree, sorry it took so long! – dimo414 May 22 '15 at 15:00
2

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;
}
1

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?
ChssPly76
  • 99,456
  • 24
  • 206
  • 195
  • Thanks for the answer. I ended up just changing the template directory every time, but creating a custom FileTemplateLoader would be the more correct way to deal with it. I certainly understand why this would be a security hole if using it to generate web pages, but if it's in a program normal users are running, it seems to me standard linux permissions should be a good enough preventative measure, no? – dimo414 Aug 04 '09 at 23:20
  • As long as your permissions are set up adequately, yes, you should be fine. I still can't imagine why you would use an absolute path, though... it seems like you'd want to either keep template under some common (non-root) folder or, if they are customizable per user, somewhere relative to user's home. But I'm sure you have your reasons - good luck with your implementation. – ChssPly76 Aug 04 '09 at 23:29
0

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());
Community
  • 1
  • 1
Erhard Siegl
  • 557
  • 2
  • 8