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", "' ' '");
}
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();
}
}
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&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&b");
}
cfg.clearTemplateCache();
}
}
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));
}
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());
}
}
Aggregations