Search in sources :

Example 11 with FileNameGlobMatcher

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

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

use of freemarker.cache.FileNameGlobMatcher 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 14 with FileNameGlobMatcher

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

Example 15 with FileNameGlobMatcher

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

Aggregations

ConditionalTemplateConfigurationFactory (freemarker.cache.ConditionalTemplateConfigurationFactory)17 FileNameGlobMatcher (freemarker.cache.FileNameGlobMatcher)17 Configuration (freemarker.template.Configuration)17 Test (org.junit.Test)15 Template (freemarker.template.Template)9 TemplateTest (freemarker.test.TemplateTest)9 StringWriter (java.io.StringWriter)5 MergingTemplateConfigurationFactory (freemarker.cache.MergingTemplateConfigurationFactory)4 FirstMatchTemplateConfigurationFactory (freemarker.cache.FirstMatchTemplateConfigurationFactory)3 StringTemplateLoader (freemarker.cache.StringTemplateLoader)3 OrMatcher (freemarker.cache.OrMatcher)2 PathGlobMatcher (freemarker.cache.PathGlobMatcher)2 TemplateConfiguration (freemarker.core.TemplateConfiguration)2 ByteArrayTemplateLoader (freemarker.cache.ByteArrayTemplateLoader)1 FileExtensionMatcher (freemarker.cache.FileExtensionMatcher)1 Date (java.util.Date)1