use of freemarker.cache.OrMatcher 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.OrMatcher 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());
}
use of freemarker.cache.OrMatcher in project freemarker by apache.
the class ConfigureOutputFormatExamples method test.
@Test
public void test() throws Exception {
Configuration cfg = getConfiguration();
addTemplate("mail/t.ftl", "");
addTemplate("t.html", "");
addTemplate("t.htm", "");
addTemplate("t.xml", "");
addTemplate("t.rtf", "");
// Example 2/a:
{
TemplateConfiguration tcHTML = new TemplateConfiguration();
tcHTML.setOutputFormat(HTMLOutputFormat.INSTANCE);
cfg.setTemplateConfigurations(new ConditionalTemplateConfigurationFactory(new PathGlobMatcher("mail/**"), tcHTML));
assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("mail/t.ftl").getOutputFormat());
}
// Example 2/b:
{
// Just to be sure...
cfg.setTemplateConfigurations(null);
cfg.setSettings(loadPropertiesFile("ConfigureOutputFormatExamples1.properties"));
assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("mail/t.ftl").getOutputFormat());
}
// Example 3/a:
{
TemplateConfiguration tcHTML = new TemplateConfiguration();
tcHTML.setOutputFormat(HTMLOutputFormat.INSTANCE);
TemplateConfiguration tcXML = new TemplateConfiguration();
tcXML.setOutputFormat(XMLOutputFormat.INSTANCE);
TemplateConfiguration tcRTF = new TemplateConfiguration();
tcRTF.setOutputFormat(RTFOutputFormat.INSTANCE);
cfg.setTemplateConfigurations(new FirstMatchTemplateConfigurationFactory(new ConditionalTemplateConfigurationFactory(new FileExtensionMatcher("xml"), tcXML), new ConditionalTemplateConfigurationFactory(new OrMatcher(new FileExtensionMatcher("html"), new FileExtensionMatcher("htm")), tcHTML), new ConditionalTemplateConfigurationFactory(new FileExtensionMatcher("rtf"), tcRTF)).allowNoMatch(true));
assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.html").getOutputFormat());
assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.htm").getOutputFormat());
assertEquals(XMLOutputFormat.INSTANCE, cfg.getTemplate("t.xml").getOutputFormat());
assertEquals(RTFOutputFormat.INSTANCE, cfg.getTemplate("t.rtf").getOutputFormat());
}
// Example 3/b:
{
// Just to be sure...
cfg.setTemplateConfigurations(null);
cfg.setSettings(loadPropertiesFile("ConfigureOutputFormatExamples2.properties"));
assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.html").getOutputFormat());
assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.htm").getOutputFormat());
assertEquals(XMLOutputFormat.INSTANCE, cfg.getTemplate("t.xml").getOutputFormat());
assertEquals(RTFOutputFormat.INSTANCE, cfg.getTemplate("t.rtf").getOutputFormat());
}
}
Aggregations