Search in sources :

Example 1 with FirstMatchTemplateConfigurationFactory

use of freemarker.cache.FirstMatchTemplateConfigurationFactory in project freemarker by apache.

the class TemplateConfigurationExamples method example3.

@Test
public void example3() throws Exception {
    Configuration cfg = getConfiguration();
    cfg.setDefaultEncoding("ISO-8859-1");
    cfg.setSharedVariable("ts", new Date(1440431606011L));
    addTemplate("t.stats.html", "${ts?datetime} ${ts?date} ${ts?time}");
    addTemplate("t.html", "");
    addTemplate("t.htm", "");
    addTemplate("t.xml", "");
    addTemplate("mail/t.html", "");
    TemplateConfiguration tcStats = new TemplateConfiguration();
    tcStats.setDateTimeFormat("iso");
    tcStats.setDateFormat("iso");
    tcStats.setTimeFormat("iso");
    tcStats.setTimeZone(DateUtil.UTC);
    TemplateConfiguration tcMail = new TemplateConfiguration();
    tcMail.setEncoding("utf-8");
    TemplateConfiguration tcHTML = new TemplateConfiguration();
    tcHTML.setOutputFormat(HTMLOutputFormat.INSTANCE);
    TemplateConfiguration tcXML = new TemplateConfiguration();
    tcXML.setOutputFormat(XMLOutputFormat.INSTANCE);
    cfg.setTemplateConfigurations(new MergingTemplateConfigurationFactory(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*.stats.*"), tcStats), new ConditionalTemplateConfigurationFactory(new PathGlobMatcher("mail/**"), tcMail), new FirstMatchTemplateConfigurationFactory(new ConditionalTemplateConfigurationFactory(new FileExtensionMatcher("xml"), tcXML), new ConditionalTemplateConfigurationFactory(new OrMatcher(new FileExtensionMatcher("html"), new FileExtensionMatcher("htm")), tcHTML)).allowNoMatch(true)));
    assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.html").getOutputFormat());
    assertEquals("ISO-8859-1", cfg.getTemplate("t.html").getEncoding());
    assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.htm").getOutputFormat());
    assertEquals(XMLOutputFormat.INSTANCE, cfg.getTemplate("t.xml").getOutputFormat());
    assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.stats.html").getOutputFormat());
    assertOutputForNamed("t.stats.html", "2015-08-24T15:53:26.011Z 2015-08-24 15:53:26.011Z");
    assertEquals("utf-8", cfg.getTemplate("mail/t.html").getEncoding());
    // From properties:
    cfg.setTemplateConfigurations(null);
    cfg.setSettings(loadPropertiesFile("TemplateConfigurationExamples3.properties"));
    assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.html").getOutputFormat());
    assertEquals("ISO-8859-1", cfg.getTemplate("t.html").getEncoding());
    assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.htm").getOutputFormat());
    assertEquals(XMLOutputFormat.INSTANCE, cfg.getTemplate("t.xml").getOutputFormat());
    assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.stats.html").getOutputFormat());
    assertOutputForNamed("t.stats.html", "2015-08-24T15:53:26.011Z 2015-08-24 15:53:26.011Z");
    assertEquals("utf-8", cfg.getTemplate("mail/t.html").getEncoding());
}
Also used : MergingTemplateConfigurationFactory(freemarker.cache.MergingTemplateConfigurationFactory) PathGlobMatcher(freemarker.cache.PathGlobMatcher) TemplateConfiguration(freemarker.core.TemplateConfiguration) Configuration(freemarker.template.Configuration) OrMatcher(freemarker.cache.OrMatcher) FileNameGlobMatcher(freemarker.cache.FileNameGlobMatcher) FirstMatchTemplateConfigurationFactory(freemarker.cache.FirstMatchTemplateConfigurationFactory) TemplateConfiguration(freemarker.core.TemplateConfiguration) FileExtensionMatcher(freemarker.cache.FileExtensionMatcher) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory) Date(java.util.Date) Test(org.junit.Test)

Example 2 with FirstMatchTemplateConfigurationFactory

use of freemarker.cache.FirstMatchTemplateConfigurationFactory in project freemarker by apache.

the class TemplateConfigurationWithTemplateCacheTest method createCommonEncodingTesterConfig.

private Configuration createCommonEncodingTesterConfig() throws UnsupportedEncodingException {
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_0);
    cfg.setDefaultEncoding("iso-8859-1");
    cfg.setLocale(Locale.US);
    ByteArrayTemplateLoader tl = new ByteArrayTemplateLoader();
    tl.putTemplate("utf8.ftl", TEXT_WITH_ACCENTS.getBytes("utf-8"));
    tl.putTemplate("utf16.ftl", TEXT_WITH_ACCENTS.getBytes("utf-16"));
    tl.putTemplate("default.ftl", TEXT_WITH_ACCENTS.getBytes("iso-8859-2"));
    tl.putTemplate("utf8-latin2.ftl", ("<#ftl encoding='iso-8859-2'>" + TEXT_WITH_ACCENTS).getBytes("iso-8859-2"));
    tl.putTemplate("default-latin2.ftl", ("<#ftl encoding='iso-8859-2'>" + TEXT_WITH_ACCENTS).getBytes("iso-8859-2"));
    cfg.setTemplateLoader(tl);
    TemplateConfiguration tcUtf8 = new TemplateConfiguration();
    tcUtf8.setEncoding("utf-8");
    TemplateConfiguration tcUtf16 = new TemplateConfiguration();
    tcUtf16.setEncoding("utf-16");
    cfg.setTemplateConfigurations(new FirstMatchTemplateConfigurationFactory(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*utf8*"), tcUtf8), new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*utf16*"), tcUtf16)).allowNoMatch(true));
    return cfg;
}
Also used : Configuration(freemarker.template.Configuration) ByteArrayTemplateLoader(freemarker.cache.ByteArrayTemplateLoader) FileNameGlobMatcher(freemarker.cache.FileNameGlobMatcher) FirstMatchTemplateConfigurationFactory(freemarker.cache.FirstMatchTemplateConfigurationFactory) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory)

Example 3 with FirstMatchTemplateConfigurationFactory

use of freemarker.cache.FirstMatchTemplateConfigurationFactory in project freemarker by apache.

the class TemplateConfigurationExamples method example2.

@Test
public void example2() throws Exception {
    Configuration cfg = getConfiguration();
    addTemplate("t.subject.ftl", "");
    addTemplate("mail/t.subject.ftl", "");
    addTemplate("mail/t.body.ftl", "");
    TemplateConfiguration tcSubject = new TemplateConfiguration();
    tcSubject.setOutputFormat(PlainTextOutputFormat.INSTANCE);
    TemplateConfiguration tcBody = new TemplateConfiguration();
    tcBody.setOutputFormat(HTMLOutputFormat.INSTANCE);
    cfg.setTemplateConfigurations(new ConditionalTemplateConfigurationFactory(new PathGlobMatcher("mail/**"), new FirstMatchTemplateConfigurationFactory(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*.subject.*"), tcSubject), new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*.body.*"), tcBody)).noMatchErrorDetails("Mail template names must contain \".subject.\" or \".body.\"!")));
    assertEquals(UndefinedOutputFormat.INSTANCE, cfg.getTemplate("t.subject.ftl").getOutputFormat());
    assertEquals(PlainTextOutputFormat.INSTANCE, cfg.getTemplate("mail/t.subject.ftl").getOutputFormat());
    assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("mail/t.body.ftl").getOutputFormat());
    // From properties:
    cfg.setTemplateConfigurations(null);
    cfg.setSettings(loadPropertiesFile("TemplateConfigurationExamples2.properties"));
    assertEquals(UndefinedOutputFormat.INSTANCE, cfg.getTemplate("t.subject.ftl").getOutputFormat());
    assertEquals(PlainTextOutputFormat.INSTANCE, cfg.getTemplate("mail/t.subject.ftl").getOutputFormat());
    assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("mail/t.body.ftl").getOutputFormat());
}
Also used : PathGlobMatcher(freemarker.cache.PathGlobMatcher) TemplateConfiguration(freemarker.core.TemplateConfiguration) Configuration(freemarker.template.Configuration) FileNameGlobMatcher(freemarker.cache.FileNameGlobMatcher) FirstMatchTemplateConfigurationFactory(freemarker.cache.FirstMatchTemplateConfigurationFactory) TemplateConfiguration(freemarker.core.TemplateConfiguration) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory) Test(org.junit.Test)

Example 4 with FirstMatchTemplateConfigurationFactory

use of freemarker.cache.FirstMatchTemplateConfigurationFactory in project freemarker by apache.

the class ConfigureOutputFormatExamples method test.

@Test
public void test() throws Exception {
    Configuration cfg = getConfiguration();
    addTemplate("mail/t.ftl", "");
    addTemplate("t.html", "");
    addTemplate("t.htm", "");
    addTemplate("t.xml", "");
    addTemplate("t.rtf", "");
    // Example 2/a:
    {
        TemplateConfiguration tcHTML = new TemplateConfiguration();
        tcHTML.setOutputFormat(HTMLOutputFormat.INSTANCE);
        cfg.setTemplateConfigurations(new ConditionalTemplateConfigurationFactory(new PathGlobMatcher("mail/**"), tcHTML));
        assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("mail/t.ftl").getOutputFormat());
    }
    // Example 2/b:
    {
        // Just to be sure...
        cfg.setTemplateConfigurations(null);
        cfg.setSettings(loadPropertiesFile("ConfigureOutputFormatExamples1.properties"));
        assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("mail/t.ftl").getOutputFormat());
    }
    // Example 3/a:
    {
        TemplateConfiguration tcHTML = new TemplateConfiguration();
        tcHTML.setOutputFormat(HTMLOutputFormat.INSTANCE);
        TemplateConfiguration tcXML = new TemplateConfiguration();
        tcXML.setOutputFormat(XMLOutputFormat.INSTANCE);
        TemplateConfiguration tcRTF = new TemplateConfiguration();
        tcRTF.setOutputFormat(RTFOutputFormat.INSTANCE);
        cfg.setTemplateConfigurations(new FirstMatchTemplateConfigurationFactory(new ConditionalTemplateConfigurationFactory(new FileExtensionMatcher("xml"), tcXML), new ConditionalTemplateConfigurationFactory(new OrMatcher(new FileExtensionMatcher("html"), new FileExtensionMatcher("htm")), tcHTML), new ConditionalTemplateConfigurationFactory(new FileExtensionMatcher("rtf"), tcRTF)).allowNoMatch(true));
        assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.html").getOutputFormat());
        assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.htm").getOutputFormat());
        assertEquals(XMLOutputFormat.INSTANCE, cfg.getTemplate("t.xml").getOutputFormat());
        assertEquals(RTFOutputFormat.INSTANCE, cfg.getTemplate("t.rtf").getOutputFormat());
    }
    // Example 3/b:
    {
        // Just to be sure...
        cfg.setTemplateConfigurations(null);
        cfg.setSettings(loadPropertiesFile("ConfigureOutputFormatExamples2.properties"));
        assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.html").getOutputFormat());
        assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.htm").getOutputFormat());
        assertEquals(XMLOutputFormat.INSTANCE, cfg.getTemplate("t.xml").getOutputFormat());
        assertEquals(RTFOutputFormat.INSTANCE, cfg.getTemplate("t.rtf").getOutputFormat());
    }
}
Also used : PathGlobMatcher(freemarker.cache.PathGlobMatcher) Configuration(freemarker.template.Configuration) TemplateConfiguration(freemarker.core.TemplateConfiguration) OrMatcher(freemarker.cache.OrMatcher) FirstMatchTemplateConfigurationFactory(freemarker.cache.FirstMatchTemplateConfigurationFactory) TemplateConfiguration(freemarker.core.TemplateConfiguration) FileExtensionMatcher(freemarker.cache.FileExtensionMatcher) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory) Test(org.junit.Test)

Aggregations

ConditionalTemplateConfigurationFactory (freemarker.cache.ConditionalTemplateConfigurationFactory)4 FirstMatchTemplateConfigurationFactory (freemarker.cache.FirstMatchTemplateConfigurationFactory)4 Configuration (freemarker.template.Configuration)4 FileNameGlobMatcher (freemarker.cache.FileNameGlobMatcher)3 PathGlobMatcher (freemarker.cache.PathGlobMatcher)3 TemplateConfiguration (freemarker.core.TemplateConfiguration)3 Test (org.junit.Test)3 FileExtensionMatcher (freemarker.cache.FileExtensionMatcher)2 OrMatcher (freemarker.cache.OrMatcher)2 ByteArrayTemplateLoader (freemarker.cache.ByteArrayTemplateLoader)1 MergingTemplateConfigurationFactory (freemarker.cache.MergingTemplateConfigurationFactory)1 Date (java.util.Date)1