Search in sources :

Example 1 with MergingTemplateConfigurationFactory

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

the class TemplateConfigurationWithTemplateCacheTest method testPlainText.

@Test
public void testPlainText() throws Exception {
    Configuration cfg = createCommonEncodingTesterConfig();
    cfg.setIncompatibleImprovements(Configuration.VERSION_2_3_22);
    TemplateConfiguration tcDE = new TemplateConfiguration();
    tcDE.setLocale(Locale.GERMANY);
    TemplateConfiguration tcYN = new TemplateConfiguration();
    tcYN.setBooleanFormat("Y,N");
    cfg.setTemplateConfigurations(new MergingTemplateConfigurationFactory(cfg.getTemplateConfigurations(), new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("utf16.ftl"), tcDE), new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("utf16.ftl"), tcYN)));
    {
        Template t = cfg.getTemplate("utf8.ftl", null, null, false);
        assertEquals("utf-8", t.getEncoding());
        assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t));
        assertEquals(Locale.US, t.getLocale());
        assertEquals("true,false", t.getBooleanFormat());
    }
    {
        Template t = cfg.getTemplate("utf8.ftl", null, "iso-8859-1", false);
        assertEquals("utf-8", t.getEncoding());
        assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t));
    }
    {
        Template t = cfg.getTemplate("utf16.ftl", null, null, false);
        assertEquals("utf-16", t.getEncoding());
        assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t));
        assertEquals(Locale.GERMANY, t.getLocale());
        assertEquals("Y,N", t.getBooleanFormat());
    }
    {
        Template t = cfg.getTemplate("default.ftl", null, null, false);
        assertEquals("iso-8859-1", t.getEncoding());
        assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t));
    }
}
Also used : MergingTemplateConfigurationFactory(freemarker.cache.MergingTemplateConfigurationFactory) Configuration(freemarker.template.Configuration) FileNameGlobMatcher(freemarker.cache.FileNameGlobMatcher) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory) Template(freemarker.template.Template) Test(org.junit.Test)

Example 2 with MergingTemplateConfigurationFactory

use of freemarker.cache.MergingTemplateConfigurationFactory 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 3 with MergingTemplateConfigurationFactory

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

the class TemplateConfigurationWithTemplateCacheTest method testConfigurableSettings.

@Test
public void testConfigurableSettings() throws Exception {
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
    cfg.setLocale(Locale.US);
    TemplateConfiguration tcFR = new TemplateConfiguration();
    tcFR.setLocale(Locale.FRANCE);
    TemplateConfiguration tcYN = new TemplateConfiguration();
    tcYN.setBooleanFormat("Y,N");
    TemplateConfiguration tc00 = new TemplateConfiguration();
    tc00.setNumberFormat("0.00");
    cfg.setTemplateConfigurations(new MergingTemplateConfigurationFactory(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*(fr)*"), tcFR), new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*(yn)*"), tcYN), new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*(00)*"), tc00)));
    String commonFTL = "${.locale} ${true?string} ${1.2}";
    StringTemplateLoader tl = new StringTemplateLoader();
    tl.putTemplate("default", commonFTL);
    tl.putTemplate("(fr)", commonFTL);
    tl.putTemplate("(yn)(00)", commonFTL);
    tl.putTemplate("(00)(fr)", commonFTL);
    cfg.setTemplateLoader(tl);
    assertEquals("en_US true 1.2", getTemplateOutput(cfg.getTemplate("default")));
    assertEquals("fr_FR true 1,2", getTemplateOutput(cfg.getTemplate("(fr)")));
    assertEquals("en_US Y 1.20", getTemplateOutput(cfg.getTemplate("(yn)(00)")));
    assertEquals("fr_FR true 1,20", getTemplateOutput(cfg.getTemplate("(00)(fr)")));
}
Also used : MergingTemplateConfigurationFactory(freemarker.cache.MergingTemplateConfigurationFactory) StringTemplateLoader(freemarker.cache.StringTemplateLoader) Configuration(freemarker.template.Configuration) FileNameGlobMatcher(freemarker.cache.FileNameGlobMatcher) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory) Test(org.junit.Test)

Example 4 with MergingTemplateConfigurationFactory

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

the class TemplateConfigurationWithTemplateCacheTest method testCustomAttributes.

@Test
public void testCustomAttributes() throws Exception {
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
    TemplateConfiguration tc1 = new TemplateConfiguration();
    tc1.setCustomAttribute("a1", "a1tc1");
    tc1.setCustomAttribute("a2", "a2tc1");
    tc1.setCustomAttribute("a3", "a3tc1");
    CUST_ATT_1.set("ca1tc1", tc1);
    CUST_ATT_2.set("ca2tc1", tc1);
    TemplateConfiguration tc2 = new TemplateConfiguration();
    tc2.setCustomAttribute("a1", "a1tc2");
    CUST_ATT_1.set("ca1tc2", tc2);
    cfg.setTemplateConfigurations(new MergingTemplateConfigurationFactory(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*(tc1)*"), tc1), new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*(tc2)*"), tc2)));
    String commonFTL = "<#ftl attributes={ 'a3': 'a3temp' }>";
    StringTemplateLoader tl = new StringTemplateLoader();
    tl.putTemplate("(tc1)", commonFTL);
    tl.putTemplate("(tc1)noHeader", "");
    tl.putTemplate("(tc2)", commonFTL);
    tl.putTemplate("(tc1)(tc2)", commonFTL);
    cfg.setTemplateLoader(tl);
    {
        Template t = cfg.getTemplate("(tc1)");
        assertEquals("a1tc1", t.getCustomAttribute("a1"));
        assertEquals("a2tc1", t.getCustomAttribute("a2"));
        assertEquals("a3temp", t.getCustomAttribute("a3"));
        assertEquals("ca1tc1", CUST_ATT_1.get(t));
        assertEquals("ca2tc1", CUST_ATT_2.get(t));
    }
    {
        Template t = cfg.getTemplate("(tc1)noHeader");
        assertEquals("a1tc1", t.getCustomAttribute("a1"));
        assertEquals("a2tc1", t.getCustomAttribute("a2"));
        assertEquals("a3tc1", t.getCustomAttribute("a3"));
        assertEquals("ca1tc1", CUST_ATT_1.get(t));
        assertEquals("ca2tc1", CUST_ATT_2.get(t));
    }
    {
        Template t = cfg.getTemplate("(tc2)");
        assertEquals("a1tc2", t.getCustomAttribute("a1"));
        assertNull(t.getCustomAttribute("a2"));
        assertEquals("a3temp", t.getCustomAttribute("a3"));
        assertEquals("ca1tc2", CUST_ATT_1.get(t));
        assertNull(CUST_ATT_2.get(t));
    }
    {
        Template t = cfg.getTemplate("(tc1)(tc2)");
        assertEquals("a1tc2", t.getCustomAttribute("a1"));
        assertEquals("a2tc1", t.getCustomAttribute("a2"));
        assertEquals("a3temp", t.getCustomAttribute("a3"));
        assertEquals("ca1tc2", CUST_ATT_1.get(t));
        assertEquals("ca2tc1", CUST_ATT_2.get(t));
    }
}
Also used : MergingTemplateConfigurationFactory(freemarker.cache.MergingTemplateConfigurationFactory) StringTemplateLoader(freemarker.cache.StringTemplateLoader) Configuration(freemarker.template.Configuration) FileNameGlobMatcher(freemarker.cache.FileNameGlobMatcher) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory) Template(freemarker.template.Template) Test(org.junit.Test)

Aggregations

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