Search in sources :

Example 16 with ConditionalTemplateConfigurationFactory

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

the class IncludeAndImportConfigurableLayersTest method test3LayerIncludeClashes.

@Test
public void test3LayerIncludeClashes() throws Exception {
    Configuration cfg = getConfiguration();
    cfg.addAutoInclude("t1.ftl");
    cfg.addAutoInclude("t2.ftl");
    cfg.addAutoInclude("t3.ftl");
    TemplateConfiguration tc = new TemplateConfiguration();
    tc.addAutoInclude("t2.ftl");
    cfg.setTemplateConfigurations(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("main.ftl"), tc));
    {
        Template t = cfg.getTemplate("main.ftl");
        StringWriter sw = new StringWriter();
        Environment env = t.createProcessingEnvironment(null, sw);
        env.addAutoInclude("t3.ftl");
        env.process();
        assertEquals("T1;T2;T3;In main: t1;t2;t3;", sw.toString());
    }
    {
        Template t = cfg.getTemplate("main2.ftl");
        StringWriter sw = new StringWriter();
        Environment env = t.createProcessingEnvironment(null, sw);
        env.addAutoInclude("t3.ftl");
        env.process();
        assertEquals("T1;T2;T3;In main2: t1;t2;t3;", sw.toString());
    }
    {
        Template t = cfg.getTemplate("main.ftl");
        StringWriter sw = new StringWriter();
        Environment env = t.createProcessingEnvironment(null, sw);
        env.process();
        assertEquals("T1;T3;T2;In main: t1;t3;t2;", sw.toString());
    }
    {
        Template t = cfg.getTemplate("main.ftl");
        StringWriter sw = new StringWriter();
        Environment env = t.createProcessingEnvironment(null, sw);
        env.addAutoInclude("t1.ftl");
        env.process();
        assertEquals("T3;T2;T1;In main: t3;t2;t1;", sw.toString());
    }
}
Also used : Configuration(freemarker.template.Configuration) StringWriter(java.io.StringWriter) FileNameGlobMatcher(freemarker.cache.FileNameGlobMatcher) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory) Template(freemarker.template.Template) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Example 17 with ConditionalTemplateConfigurationFactory

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

the class DateFormatTest method testAlieses.

@Test
public void testAlieses() throws Exception {
    Configuration cfg = getConfiguration();
    cfg.setCustomDateFormats(ImmutableMap.of("d", new AliasTemplateDateFormatFactory("yyyy-MMM-dd"), "m", new AliasTemplateDateFormatFactory("yyyy-MMM"), "epoch", EpochMillisTemplateDateFormatFactory.INSTANCE));
    TemplateConfiguration tc = new TemplateConfiguration();
    tc.setCustomDateFormats(ImmutableMap.of("m", new AliasTemplateDateFormatFactory("yyyy-MMMM"), "i", new AliasTemplateDateFormatFactory("@epoch")));
    cfg.setTemplateConfigurations(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*2*"), tc));
    addToDataModel("d", TM);
    String commonFtl = "${d?string.@d} ${d?string.@m} " + "<#setting locale='fr_FR'>${d?string.@m} " + "<#attempt>${d?string.@i}<#recover>E</#attempt>";
    addTemplate("t1.ftl", commonFtl);
    addTemplate("t2.ftl", commonFtl);
    // 2015-09-06T12:00:00Z
    assertOutputForNamed("t1.ftl", "2015-Sep-06 2015-Sep 2015-sept. E");
    assertOutputForNamed("t2.ftl", "2015-Sep-06 2015-September 2015-septembre " + T);
}
Also used : Configuration(freemarker.template.Configuration) FileNameGlobMatcher(freemarker.cache.FileNameGlobMatcher) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Example 18 with ConditionalTemplateConfigurationFactory

use of freemarker.cache.ConditionalTemplateConfigurationFactory 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 19 with ConditionalTemplateConfigurationFactory

use of freemarker.cache.ConditionalTemplateConfigurationFactory 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)19 Configuration (freemarker.template.Configuration)19 FileNameGlobMatcher (freemarker.cache.FileNameGlobMatcher)17 Test (org.junit.Test)17 Template (freemarker.template.Template)10 TemplateTest (freemarker.test.TemplateTest)9 StringWriter (java.io.StringWriter)5 FirstMatchTemplateConfigurationFactory (freemarker.cache.FirstMatchTemplateConfigurationFactory)4 MergingTemplateConfigurationFactory (freemarker.cache.MergingTemplateConfigurationFactory)4 TemplateConfiguration (freemarker.core.TemplateConfiguration)4 FileExtensionMatcher (freemarker.cache.FileExtensionMatcher)3 OrMatcher (freemarker.cache.OrMatcher)3 PathGlobMatcher (freemarker.cache.PathGlobMatcher)3 StringTemplateLoader (freemarker.cache.StringTemplateLoader)3 ByteArrayTemplateLoader (freemarker.cache.ByteArrayTemplateLoader)1 Date (java.util.Date)1