Search in sources :

Example 11 with ConditionalTemplateConfigurationFactory

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

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

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

the class TemplateConfigurationWithTemplateCacheTest method testLocale.

@Test
public void testLocale() throws Exception {
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
    cfg.setLocale(Locale.US);
    StringTemplateLoader tl = new StringTemplateLoader();
    tl.putTemplate("(de).ftl", "${.locale}");
    tl.putTemplate("default.ftl", "${.locale}");
    tl.putTemplate("(de)-fr.ftl", ("<#ftl locale='fr_FR'>${.locale}"));
    tl.putTemplate("default-fr.ftl", ("<#ftl locale='fr_FR'>${.locale}"));
    cfg.setTemplateLoader(tl);
    TemplateConfiguration tcDe = new TemplateConfiguration();
    tcDe.setLocale(Locale.GERMANY);
    cfg.setTemplateConfigurations(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*(de)*"), tcDe));
    {
        Template t = cfg.getTemplate("(de).ftl");
        assertEquals(Locale.GERMANY, t.getLocale());
        assertEquals("de_DE", getTemplateOutput(t));
    }
    {
        Template t = cfg.getTemplate("(de).ftl", Locale.ITALY);
        assertEquals(Locale.GERMANY, t.getLocale());
        assertEquals("de_DE", getTemplateOutput(t));
    }
    {
        Template t = cfg.getTemplate("default.ftl");
        assertEquals(Locale.US, t.getLocale());
        assertEquals("en_US", getTemplateOutput(t));
    }
    {
        Template t = cfg.getTemplate("default.ftl", Locale.ITALY);
        assertEquals(Locale.ITALY, t.getLocale());
        assertEquals("it_IT", getTemplateOutput(t));
    }
}
Also used : StringTemplateLoader(freemarker.cache.StringTemplateLoader) Configuration(freemarker.template.Configuration) FileNameGlobMatcher(freemarker.cache.FileNameGlobMatcher) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory) Template(freemarker.template.Template) Test(org.junit.Test)

Example 14 with ConditionalTemplateConfigurationFactory

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

Example 15 with ConditionalTemplateConfigurationFactory

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

the class OutputFormatTest method createConfiguration.

@Override
protected Configuration createConfiguration() throws TemplateModelException {
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_24);
    TemplateConfiguration xmlTC = new TemplateConfiguration();
    xmlTC.setOutputFormat(XMLOutputFormat.INSTANCE);
    cfg.setTemplateConfigurations(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*.xml"), xmlTC));
    cfg.setSharedVariable("rtfPlain", RTFOutputFormat.INSTANCE.fromPlainTextByEscaping("\\par a & b"));
    cfg.setSharedVariable("rtfMarkup", RTFOutputFormat.INSTANCE.fromMarkup("\\par c"));
    cfg.setSharedVariable("htmlPlain", HTMLOutputFormat.INSTANCE.fromPlainTextByEscaping("a < {h'}"));
    cfg.setSharedVariable("htmlMarkup", HTMLOutputFormat.INSTANCE.fromMarkup("<p>c"));
    cfg.setSharedVariable("xmlPlain", XMLOutputFormat.INSTANCE.fromPlainTextByEscaping("a < {x'}"));
    cfg.setSharedVariable("xmlMarkup", XMLOutputFormat.INSTANCE.fromMarkup("<p>c</p>"));
    return cfg;
}
Also used : Configuration(freemarker.template.Configuration) FileNameGlobMatcher(freemarker.cache.FileNameGlobMatcher) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory)

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