Search in sources :

Example 6 with FileNameGlobMatcher

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

the class IncludeAndImportConfigurableLayersTest method test3LayerIncludesClashes2.

@Test
public void test3LayerIncludesClashes2() throws Exception {
    Configuration cfg = getConfiguration();
    cfg.addAutoInclude("t1.ftl");
    cfg.addAutoInclude("t1.ftl");
    TemplateConfiguration tc = new TemplateConfiguration();
    tc.addAutoInclude("t2.ftl");
    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.addAutoInclude("t3.ftl");
        env.addAutoInclude("t1.ftl");
        env.addAutoInclude("t1.ftl");
        env.process();
        assertEquals("T2;T3;T1;In main: t2;t3;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 7 with FileNameGlobMatcher

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

the class IncludeAndImportConfigurableLayersTest method test3LayerImportNoClashes.

@Test
public void test3LayerImportNoClashes() throws Exception {
    Configuration cfg = getConfiguration();
    cfg.addAutoImport("t1", "t1.ftl");
    TemplateConfiguration tc = new TemplateConfiguration();
    tc.addAutoImport("t2", "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.addAutoImport("t3", "t3.ftl");
        env.process();
        assertEquals("In main: t1;t2;t3;", sw.toString());
    }
    {
        Template t = cfg.getTemplate("main.ftl");
        StringWriter sw = new StringWriter();
        Environment env = t.createProcessingEnvironment(null, sw);
        env.process();
        assertEquals("In main: t1;t2;", sw.toString());
    }
    {
        Template t = cfg.getTemplate("main2.ftl");
        StringWriter sw = new StringWriter();
        Environment env = t.createProcessingEnvironment(null, sw);
        env.addAutoImport("t3", "t3.ftl");
        env.process();
        assertEquals("In main2: t1;t3;", sw.toString());
    }
    cfg.removeAutoImport("t1");
    {
        Template t = cfg.getTemplate("main.ftl");
        StringWriter sw = new StringWriter();
        Environment env = t.createProcessingEnvironment(null, sw);
        env.addAutoImport("t3", "t3.ftl");
        env.process();
        assertEquals("In main: t2;t3;", 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 8 with FileNameGlobMatcher

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

the class IncludeAndImportConfigurableLayersTest method test3LayerImportClashes.

@Test
public void test3LayerImportClashes() throws Exception {
    Configuration cfg = getConfiguration();
    cfg.addAutoImport("t1", "t1.ftl");
    cfg.addAutoImport("t2", "t2.ftl");
    cfg.addAutoImport("t3", "t3.ftl");
    TemplateConfiguration tc = new TemplateConfiguration();
    tc.addAutoImport("t2", "t2b.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.addAutoImport("t3", "t3b.ftl");
        env.process();
        assertEquals("In main: t1;t2b;t3b;", sw.toString());
    }
    {
        Template t = cfg.getTemplate("main2.ftl");
        StringWriter sw = new StringWriter();
        Environment env = t.createProcessingEnvironment(null, sw);
        env.addAutoImport("t3", "t3b.ftl");
        env.process();
        assertEquals("In main2: t1;t2;t3b;", sw.toString());
    }
    {
        Template t = cfg.getTemplate("main.ftl");
        StringWriter sw = new StringWriter();
        Environment env = t.createProcessingEnvironment(null, sw);
        env.process();
        assertEquals("In main: t1;t3;t2b;", 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 9 with FileNameGlobMatcher

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

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

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