Class FreemarkerTemplateLoader

  • All Implemented Interfaces:
    freemarker.cache.TemplateLoader

    public class FreemarkerTemplateLoader
    extends Object
    implements freemarker.cache.TemplateLoader
    Loads Freemarker templates from a given directory. Different from a file loader in two ways: 1) Flattens the directory. When it searches for a template, it will look in the base directory and in any sub-directories. While doing this, it ignores any path that is attached to the template name. So if you were to ask for 'admin/silly.ftl', it would search for 'silly.ftl' in the base directory, and in any sub-directories, until it finds one. 2) Accepts approximate matches on locales. When asked for a template, it will accepts an approximate match that matches the basename and extension, and language or region if specifed. So a search for a template with no language or region will prefer an exact match, but will accept one with language or both language and region.
     "this_es_MX.ftl" matches "this_es_MX.ftl"
     "this_es.ftl"    matches "this_es.ftl" or "this_es_MX.ftl"
     "this.ftl"       matches "this.ftl" or "this_es.ftl" or "this_es_MX.ftl"
     
    This allows Freemarker to mimic the behavior of the language filtering RDF service, because if Freemarker does not find a match for "this_es_MX.ftl", it will try again with "this_es.ftl" and "this.ftl". So the net effect is that a search for "silly_es_MX.ftl" would eventually return any of these, in order of preference:
     silly_es_MX.ftl
     silly_es.ftl
     silly_es_*.ftl
     silly.ftl
     silly_*.ftl
     
    If more than one template file qualifies, we choose by best fit, shortest path, and alphabetical order, to insure that identical requests produce identical results.
    • Constructor Detail

      • FreemarkerTemplateLoader

        public FreemarkerTemplateLoader​(File baseDir)
    • Method Detail

      • findTemplateSource

        public Object findTemplateSource​(String name)
                                  throws IOException
        Get the best template for this name. Walk the tree finding all possible matches, then choose our favorite.
        Specified by:
        findTemplateSource in interface freemarker.cache.TemplateLoader
        Throws:
        IOException
      • getLastModified

        public long getLastModified​(Object templateSource)
        Ask the file when it was last modified.
        Specified by:
        getLastModified in interface freemarker.cache.TemplateLoader
        Parameters:
        templateSource - a File that was obtained earlier from findTemplateSource().
      • getReader

        public Reader getReader​(Object templateSource,
                                String encoding)
                         throws IOException
        Get a Reader on this File. The framework will close the Reader after reading it.
        Specified by:
        getReader in interface freemarker.cache.TemplateLoader
        Parameters:
        templateSource - a File that was obtained earlier from findTemplateSource().
        Throws:
        IOException
      • closeTemplateSource

        public void closeTemplateSource​(Object templateSource)
                                 throws IOException
        Nothing to do here. No resources to free up.
        Specified by:
        closeTemplateSource in interface freemarker.cache.TemplateLoader
        Parameters:
        templateSource - a File that was obtained earlier from findTemplateSource().
        Throws:
        IOException