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