Search in sources :

Example 1 with ConditionalTemplateConfigurationFactory

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

the class NumberFormatTest method testAlieses.

@Test
public void testAlieses() throws Exception {
    Configuration cfg = getConfiguration();
    cfg.setCustomNumberFormats(ImmutableMap.of("f", new AliasTemplateNumberFormatFactory("0.#'f'"), "d", new AliasTemplateNumberFormatFactory("0.0#"), "hex", HexTemplateNumberFormatFactory.INSTANCE));
    TemplateConfiguration tc = new TemplateConfiguration();
    tc.setCustomNumberFormats(ImmutableMap.of("d", new AliasTemplateNumberFormatFactory("0.#'d'"), "i", new AliasTemplateNumberFormatFactory("@hex")));
    cfg.setTemplateConfigurations(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*2*"), tc));
    String commonFtl = "${1?string.@f} ${1?string.@d} " + "<#setting locale='fr_FR'>${1.5?string.@d} " + "<#attempt>${10?string.@i}<#recover>E</#attempt>";
    addTemplate("t1.ftl", commonFtl);
    addTemplate("t2.ftl", commonFtl);
    assertOutputForNamed("t1.ftl", "1f 1.0 1,5 E");
    assertOutputForNamed("t2.ftl", "1f 1d 1,5d a");
}
Also used : Configuration(freemarker.template.Configuration) FileNameGlobMatcher(freemarker.cache.FileNameGlobMatcher) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Example 2 with ConditionalTemplateConfigurationFactory

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

the class OutputFormatTest method testStandardFileExtensions.

@Test
public void testStandardFileExtensions() throws Exception {
    String commonContent = "${.outputFormat}";
    addTemplate("t", commonContent);
    addTemplate("t.ftl", commonContent);
    addTemplate("t.ftlh", commonContent);
    addTemplate("t.FTLH", commonContent);
    addTemplate("t.fTlH", commonContent);
    addTemplate("t.ftlx", commonContent);
    addTemplate("t.FTLX", commonContent);
    addTemplate("t.fTlX", commonContent);
    addTemplate("tWithHeader.ftlx", "<#ftl outputFormat='HTML'>" + commonContent);
    Configuration cfg = getConfiguration();
    for (int setupNumber = 1; setupNumber <= 5; setupNumber++) {
        final OutputFormat cfgOutputFormat;
        final OutputFormat ftlhOutputFormat;
        final OutputFormat ftlxOutputFormat;
        switch(setupNumber) {
            case 1:
                cfgOutputFormat = UndefinedOutputFormat.INSTANCE;
                ftlhOutputFormat = HTMLOutputFormat.INSTANCE;
                ftlxOutputFormat = XMLOutputFormat.INSTANCE;
                break;
            case 2:
                cfgOutputFormat = RTFOutputFormat.INSTANCE;
                cfg.setOutputFormat(cfgOutputFormat);
                ftlhOutputFormat = HTMLOutputFormat.INSTANCE;
                ftlxOutputFormat = XMLOutputFormat.INSTANCE;
                break;
            case 3:
                cfgOutputFormat = UndefinedOutputFormat.INSTANCE;
                cfg.unsetOutputFormat();
                TemplateConfiguration tcXml = new TemplateConfiguration();
                tcXml.setOutputFormat(XMLOutputFormat.INSTANCE);
                cfg.setTemplateConfigurations(new ConditionalTemplateConfigurationFactory(new OrMatcher(new FileNameGlobMatcher("*.ftlh"), new FileNameGlobMatcher("*.FTLH"), new FileNameGlobMatcher("*.fTlH")), tcXml));
                // can't be overidden
                ftlhOutputFormat = HTMLOutputFormat.INSTANCE;
                ftlxOutputFormat = XMLOutputFormat.INSTANCE;
                break;
            case 4:
                cfg.setIncompatibleImprovements(Configuration.VERSION_2_3_23);
                cfgOutputFormat = UndefinedOutputFormat.INSTANCE;
                // now gets overidden
                ftlhOutputFormat = XMLOutputFormat.INSTANCE;
                ftlxOutputFormat = UndefinedOutputFormat.INSTANCE;
                break;
            case 5:
                cfg.setTemplateConfigurations(null);
                cfgOutputFormat = UndefinedOutputFormat.INSTANCE;
                ftlhOutputFormat = UndefinedOutputFormat.INSTANCE;
                ftlxOutputFormat = UndefinedOutputFormat.INSTANCE;
                break;
            default:
                throw new AssertionError();
        }
        assertEquals(cfgOutputFormat, cfg.getOutputFormat());
        {
            Template t = cfg.getTemplate("t");
            assertEquals(cfgOutputFormat, t.getOutputFormat());
            assertOutput(t, t.getOutputFormat().getName());
        }
        {
            Template t = cfg.getTemplate("t.ftl");
            assertEquals(cfgOutputFormat, t.getOutputFormat());
            assertOutput(t, t.getOutputFormat().getName());
        }
        for (String name : new String[] { "t.ftlh", "t.FTLH", "t.fTlH" }) {
            Template t = cfg.getTemplate(name);
            assertEquals(ftlhOutputFormat, t.getOutputFormat());
            assertOutput(t, t.getOutputFormat().getName());
        }
        for (String name : new String[] { "t.ftlx", "t.FTLX", "t.fTlX" }) {
            Template t = cfg.getTemplate(name);
            assertEquals(ftlxOutputFormat, t.getOutputFormat());
            assertOutput(t, t.getOutputFormat().getName());
        }
        {
            Template t = cfg.getTemplate("tWithHeader.ftlx");
            assertEquals(HTMLOutputFormat.INSTANCE, t.getOutputFormat());
            assertOutput(t, t.getOutputFormat().getName());
        }
        cfg.clearTemplateCache();
    }
}
Also used : Configuration(freemarker.template.Configuration) OrMatcher(freemarker.cache.OrMatcher) FileNameGlobMatcher(freemarker.cache.FileNameGlobMatcher) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory) Template(freemarker.template.Template) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Example 3 with ConditionalTemplateConfigurationFactory

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

the class OutputFormatTest method testStandardFileExtensionsSettingOverriding.

@Test
public void testStandardFileExtensionsSettingOverriding() throws Exception {
    addTemplate("t.ftlx", "${\"'\"} ${\"'\"?esc} ${\"'\"?noEsc}");
    addTemplate("t.ftl", "${'{}'} ${'{}'?esc} ${'{}'?noEsc}");
    TemplateConfiguration tcHTML = new TemplateConfiguration();
    tcHTML.setOutputFormat(HTMLOutputFormat.INSTANCE);
    ConditionalTemplateConfigurationFactory tcfHTML = new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("t.*"), tcHTML);
    TemplateConfiguration tcNoAutoEsc = new TemplateConfiguration();
    tcNoAutoEsc.setAutoEscapingPolicy(Configuration.DISABLE_AUTO_ESCAPING_POLICY);
    ConditionalTemplateConfigurationFactory tcfNoAutoEsc = new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("t.*"), tcNoAutoEsc);
    Configuration cfg = getConfiguration();
    cfg.setOutputFormat(HTMLOutputFormat.INSTANCE);
    // Can't override it
    assertOutputForNamed("t.ftlx", "&apos; &apos; '");
    cfg.setTemplateConfigurations(tcfHTML);
    // Can't override it
    assertOutputForNamed("t.ftlx", "&apos; &apos; '");
    cfg.setTemplateConfigurations(tcfNoAutoEsc);
    // Can't override it
    assertOutputForNamed("t.ftlx", "&apos; &apos; '");
    cfg.setTemplateConfigurations(null);
    cfg.unsetOutputFormat();
    // Extensions has no effect
    cfg.setIncompatibleImprovements(Configuration.VERSION_2_3_23);
    assertErrorContainsForNamed("t.ftlx", UndefinedOutputFormat.INSTANCE.getName());
    cfg.setOutputFormat(HTMLOutputFormat.INSTANCE);
    assertOutputForNamed("t.ftlx", "&#39; &#39; '");
    cfg.setOutputFormat(XMLOutputFormat.INSTANCE);
    assertOutputForNamed("t.ftlx", "&apos; &apos; '");
    cfg.setTemplateConfigurations(tcfHTML);
    assertOutputForNamed("t.ftlx", "&#39; &#39; '");
    cfg.setTemplateConfigurations(tcfNoAutoEsc);
    assertOutputForNamed("t.ftlx", "' &apos; '");
    cfg.setRecognizeStandardFileExtensions(true);
    cfg.setTemplateConfigurations(tcfHTML);
    // Can't override it
    assertOutputForNamed("t.ftlx", "&apos; &apos; '");
    cfg.setTemplateConfigurations(tcfNoAutoEsc);
    // Can't override it
    assertOutputForNamed("t.ftlx", "&apos; &apos; '");
    cfg.setTemplateConfigurations(null);
    cfg.unsetOutputFormat();
    cfg.setIncompatibleImprovements(Configuration.VERSION_2_3_24);
    cfg.setTemplateConfigurations(tcfHTML);
    // Can't override it
    assertOutputForNamed("t.ftlx", "&apos; &apos; '");
    cfg.setRecognizeStandardFileExtensions(false);
    assertOutputForNamed("t.ftlx", "&#39; &#39; '");
}
Also used : Configuration(freemarker.template.Configuration) FileNameGlobMatcher(freemarker.cache.FileNameGlobMatcher) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Example 4 with ConditionalTemplateConfigurationFactory

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

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

the class IncludeAndImportConfigurableLayersTest method test3LayerIncludesNoClashes.

@Test
public void test3LayerIncludesNoClashes() throws Exception {
    Configuration cfg = getConfiguration();
    cfg.addAutoInclude("t1.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("main.ftl");
        StringWriter sw = new StringWriter();
        Environment env = t.createProcessingEnvironment(null, sw);
        env.process();
        assertEquals("T1;T2;In main: t1;t2;", 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;T3;In main2: t1;t3;", sw.toString());
    }
    cfg.removeAutoInclude("t1.ftl");
    {
        Template t = cfg.getTemplate("main.ftl");
        StringWriter sw = new StringWriter();
        Environment env = t.createProcessingEnvironment(null, sw);
        env.addAutoInclude("t3.ftl");
        env.process();
        assertEquals("T2;T3;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)

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