Search in sources :

Example 46 with Configuration

use of freemarker.template.Configuration 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", "' ' '");
    cfg.setTemplateConfigurations(tcfHTML);
    // Can't override it
    assertOutputForNamed("t.ftlx", "' ' '");
    cfg.setTemplateConfigurations(tcfNoAutoEsc);
    // Can't override it
    assertOutputForNamed("t.ftlx", "' ' '");
    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", "' ' '");
    cfg.setOutputFormat(XMLOutputFormat.INSTANCE);
    assertOutputForNamed("t.ftlx", "' ' '");
    cfg.setTemplateConfigurations(tcfHTML);
    assertOutputForNamed("t.ftlx", "' ' '");
    cfg.setTemplateConfigurations(tcfNoAutoEsc);
    assertOutputForNamed("t.ftlx", "' ' '");
    cfg.setRecognizeStandardFileExtensions(true);
    cfg.setTemplateConfigurations(tcfHTML);
    // Can't override it
    assertOutputForNamed("t.ftlx", "' ' '");
    cfg.setTemplateConfigurations(tcfNoAutoEsc);
    // Can't override it
    assertOutputForNamed("t.ftlx", "' ' '");
    cfg.setTemplateConfigurations(null);
    cfg.unsetOutputFormat();
    cfg.setIncompatibleImprovements(Configuration.VERSION_2_3_24);
    cfg.setTemplateConfigurations(tcfHTML);
    // Can't override it
    assertOutputForNamed("t.ftlx", "' ' '");
    cfg.setRecognizeStandardFileExtensions(false);
    assertOutputForNamed("t.ftlx", "' ' '");
}
Also used : Configuration(freemarker.template.Configuration) FileNameGlobMatcher(freemarker.cache.FileNameGlobMatcher) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Example 47 with Configuration

use of freemarker.template.Configuration in project freemarker by apache.

the class OutputFormatTest method testOutputFormatSettingLayers.

@Test
public void testOutputFormatSettingLayers() throws Exception {
    addTemplate("t", "${.outputFormat}");
    addTemplate("t.xml", "${.outputFormat}");
    addTemplate("tWithHeader", "<#ftl outputFormat='HTML'>${.outputFormat}");
    Configuration cfg = getConfiguration();
    for (OutputFormat cfgOutputFormat : new OutputFormat[] { UndefinedOutputFormat.INSTANCE, RTFOutputFormat.INSTANCE }) {
        if (!cfgOutputFormat.equals(UndefinedOutputFormat.INSTANCE)) {
            cfg.setOutputFormat(cfgOutputFormat);
        }
        assertEquals(cfgOutputFormat, cfg.getOutputFormat());
        {
            Template t = cfg.getTemplate("t");
            assertEquals(cfgOutputFormat, t.getOutputFormat());
            assertOutput(t, t.getOutputFormat().getName());
        }
        {
            Template t = cfg.getTemplate("t.xml");
            assertEquals(XMLOutputFormat.INSTANCE, t.getOutputFormat());
            assertOutput(t, t.getOutputFormat().getName());
        }
        {
            Template t = cfg.getTemplate("tWithHeader");
            assertEquals(HTMLOutputFormat.INSTANCE, t.getOutputFormat());
            assertOutput(t, t.getOutputFormat().getName());
        }
        cfg.clearTemplateCache();
    }
}
Also used : Configuration(freemarker.template.Configuration) Template(freemarker.template.Template) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Example 48 with Configuration

use of freemarker.template.Configuration in project freemarker by apache.

the class OutputFormatTest method testAutoEscapingSettingLayers.

@Test
public void testAutoEscapingSettingLayers() throws Exception {
    addTemplate("t", "${'a&b'}");
    addTemplate("tWithHeaderFalse", "<#ftl autoEsc=false>${'a&b'}");
    addTemplate("tWithHeaderTrue", "<#ftl autoEsc=true>${'a&b'}");
    Configuration cfg = getConfiguration();
    assertEquals(Configuration.ENABLE_IF_DEFAULT_AUTO_ESCAPING_POLICY, cfg.getAutoEscapingPolicy());
    cfg.setOutputFormat(XMLOutputFormat.INSTANCE);
    for (boolean cfgAutoEscaping : new boolean[] { true, false }) {
        if (!cfgAutoEscaping) {
            cfg.setAutoEscapingPolicy(Configuration.DISABLE_AUTO_ESCAPING_POLICY);
        }
        {
            Template t = cfg.getTemplate("t");
            if (cfgAutoEscaping) {
                assertTrue(t.getAutoEscaping());
                assertOutput(t, "a&amp;b");
            } else {
                assertFalse(t.getAutoEscaping());
                assertOutput(t, "a&b");
            }
        }
        {
            Template t = cfg.getTemplate("tWithHeaderFalse");
            assertFalse(t.getAutoEscaping());
            assertOutput(t, "a&b");
        }
        {
            Template t = cfg.getTemplate("tWithHeaderTrue");
            assertTrue(t.getAutoEscaping());
            assertOutput(t, "a&amp;b");
        }
        cfg.clearTemplateCache();
    }
}
Also used : Configuration(freemarker.template.Configuration) Template(freemarker.template.Template) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Example 49 with Configuration

use of freemarker.template.Configuration in project freemarker by apache.

the class TemplateConfigurationTest method testConfigureCustomAttributes.

@Test
public void testConfigureCustomAttributes() throws Exception {
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
    cfg.setCustomAttribute("k1", "c");
    cfg.setCustomAttribute("k2", "c");
    cfg.setCustomAttribute("k3", "c");
    TemplateConfiguration tc = new TemplateConfiguration();
    tc.setCustomAttribute("k2", "tc");
    tc.setCustomAttribute("k3", null);
    tc.setCustomAttribute("k4", "tc");
    tc.setCustomAttribute("k5", "tc");
    tc.setCustomAttribute("k6", "tc");
    CA1.set("tc", tc);
    CA2.set("tc", tc);
    CA3.set("tc", tc);
    Template t = new Template(null, "", cfg);
    t.setCustomAttribute("k5", "t");
    t.setCustomAttribute("k6", null);
    t.setCustomAttribute("k7", "t");
    CA2.set("t", t);
    CA3.set(null, t);
    CA4.set("t", t);
    tc.setParentConfiguration(cfg);
    tc.apply(t);
    assertEquals("c", t.getCustomAttribute("k1"));
    assertEquals("tc", t.getCustomAttribute("k2"));
    assertNull(t.getCustomAttribute("k3"));
    assertEquals("tc", t.getCustomAttribute("k4"));
    assertEquals("t", t.getCustomAttribute("k5"));
    assertNull(t.getCustomAttribute("k6"));
    assertEquals("t", t.getCustomAttribute("k7"));
    assertEquals("tc", CA1.get(t));
    assertEquals("t", CA2.get(t));
    assertNull(CA3.get(t));
    assertEquals("t", CA4.get(t));
}
Also used : Configuration(freemarker.template.Configuration) Template(freemarker.template.Template) Test(org.junit.Test)

Example 50 with Configuration

use of freemarker.template.Configuration in project freemarker by apache.

the class TemplateConfigurationTest method assertOutput.

private void assertOutput(TemplateConfiguration tc, String ftl, String expectedConfiguredOutput) throws TemplateException, IOException {
    StringWriter sw = new StringWriter();
    try {
        Configuration cfg = tc != null ? tc.getParentConfiguration() : DEFAULT_CFG;
        Template t = new Template("adhoc.ftlh", null, new StringReader(ftl), cfg, tc, null);
        if (tc != null) {
            tc.apply(t);
        }
        t.process(null, sw);
        if (expectedConfiguredOutput == null) {
            fail("Template should have fail.");
        }
    } catch (TemplateException e) {
        if (expectedConfiguredOutput != null) {
            throw e;
        }
    } catch (ParseException e) {
        if (expectedConfiguredOutput != null) {
            throw e;
        }
    }
    if (expectedConfiguredOutput != null) {
        assertEquals(expectedConfiguredOutput, sw.toString());
    }
}
Also used : StringWriter(java.io.StringWriter) Configuration(freemarker.template.Configuration) TemplateException(freemarker.template.TemplateException) StringReader(java.io.StringReader) Template(freemarker.template.Template)

Aggregations

Configuration (freemarker.template.Configuration)282 Template (freemarker.template.Template)106 Test (org.junit.Test)86 TemplateTest (freemarker.test.TemplateTest)52 HashMap (java.util.HashMap)51 IOException (java.io.IOException)41 StringWriter (java.io.StringWriter)39 File (java.io.File)33 TemplateException (freemarker.template.TemplateException)29 Writer (java.io.Writer)27 ClassTemplateLoader (freemarker.cache.ClassTemplateLoader)19 ConditionalTemplateConfigurationFactory (freemarker.cache.ConditionalTemplateConfigurationFactory)19 StringTemplateLoader (freemarker.cache.StringTemplateLoader)18 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)18 FileNameGlobMatcher (freemarker.cache.FileNameGlobMatcher)17 TemplateLoader (freemarker.cache.TemplateLoader)16 MultiTemplateLoader (freemarker.cache.MultiTemplateLoader)15 FileWriter (java.io.FileWriter)13 ArrayList (java.util.ArrayList)13 User (org.vcell.util.document.User)13