use of freemarker.core.TemplateConfiguration in project freemarker by apache.
the class TemplateConfigurationFactoryTest method testSetConfiguration.
@Test
public void testSetConfiguration() {
TemplateConfiguration tc = new TemplateConfiguration();
ConditionalTemplateConfigurationFactory tcf = new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*"), tc);
assertNull(tcf.getConfiguration());
assertNull(tc.getParentConfiguration());
tcf.setConfiguration(cfg);
assertEquals(cfg, tcf.getConfiguration());
assertEquals(cfg, tc.getParentConfiguration());
// Ignored:
tcf.setConfiguration(cfg);
try {
tcf.setConfiguration(Configuration.getDefaultConfiguration());
fail();
} catch (IllegalStateException e) {
assertThat(e.getMessage(), containsString("TemplateConfigurationFactory"));
}
}
use of freemarker.core.TemplateConfiguration in project freemarker by apache.
the class TemplateConfigurationFactoryTest method newTemplateConfiguration.
@SuppressWarnings("boxing")
private TemplateConfiguration newTemplateConfiguration(int id) {
TemplateConfiguration tc = new TemplateConfiguration();
tc.setCustomAttribute("id", id);
tc.setCustomAttribute("contains" + id, true);
return tc;
}
use of freemarker.core.TemplateConfiguration in project freemarker by apache.
the class TemplateConfigurationExamples method example2.
@Test
public void example2() throws Exception {
Configuration cfg = getConfiguration();
addTemplate("t.subject.ftl", "");
addTemplate("mail/t.subject.ftl", "");
addTemplate("mail/t.body.ftl", "");
TemplateConfiguration tcSubject = new TemplateConfiguration();
tcSubject.setOutputFormat(PlainTextOutputFormat.INSTANCE);
TemplateConfiguration tcBody = new TemplateConfiguration();
tcBody.setOutputFormat(HTMLOutputFormat.INSTANCE);
cfg.setTemplateConfigurations(new ConditionalTemplateConfigurationFactory(new PathGlobMatcher("mail/**"), new FirstMatchTemplateConfigurationFactory(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*.subject.*"), tcSubject), new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*.body.*"), tcBody)).noMatchErrorDetails("Mail template names must contain \".subject.\" or \".body.\"!")));
assertEquals(UndefinedOutputFormat.INSTANCE, cfg.getTemplate("t.subject.ftl").getOutputFormat());
assertEquals(PlainTextOutputFormat.INSTANCE, cfg.getTemplate("mail/t.subject.ftl").getOutputFormat());
assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("mail/t.body.ftl").getOutputFormat());
// From properties:
cfg.setTemplateConfigurations(null);
cfg.setSettings(loadPropertiesFile("TemplateConfigurationExamples2.properties"));
assertEquals(UndefinedOutputFormat.INSTANCE, cfg.getTemplate("t.subject.ftl").getOutputFormat());
assertEquals(PlainTextOutputFormat.INSTANCE, cfg.getTemplate("mail/t.subject.ftl").getOutputFormat());
assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("mail/t.body.ftl").getOutputFormat());
}
use of freemarker.core.TemplateConfiguration 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