use of freemarker.template.Configuration in project freemarker by apache.
the class CustomFormatsExample method aliases2.
@Test
public void aliases2() throws IOException, TemplateException {
Configuration cfg = getConfiguration();
Map<String, TemplateNumberFormatFactory> customNumberFormats = new HashMap<String, TemplateNumberFormatFactory>();
customNumberFormats.put("base", BaseNTemplateNumberFormatFactory.INSTANCE);
customNumberFormats.put("oct", new AliasTemplateNumberFormatFactory("@base 8"));
cfg.setCustomNumberFormats(customNumberFormats);
assertOutputForNamed("CustomFormatsExample-alias2.ftlh");
}
use of freemarker.template.Configuration in project freemarker by apache.
the class GettingStartedExample method main.
@Test
public void main() throws Exception {
/* ------------------------------------------------------------------------ */
/* You should do this ONLY ONCE in the whole application life-cycle: */
/* Create and adjust the configuration singleton */
Configuration cfg = new Configuration(Configuration.VERSION_2_3_24);
cfg.setClassForTemplateLoading(GettingStartedExample.class, "");
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);
/* ------------------------------------------------------------------------ */
/* You usually do these for MULTIPLE TIMES in the application life-cycle: */
/* Create a data-model */
Map root = new HashMap();
root.put("user", "Big Joe");
Product latest = new Product();
latest.setUrl("products/greenmouse.html");
latest.setName("green mouse");
root.put("latestProduct", latest);
/* Get the template (uses cache internally) */
Template temp = cfg.getTemplate("test.ftlh");
/* Merge data-model with template */
Writer out = new OutputStreamWriter(System.out);
temp.process(root, out);
// Note: Depending on what `out` is, you may need to call `out.close()`.
// This is usually the case for file output, but not for servlet output.
}
use of freemarker.template.Configuration in project freemarker by apache.
the class TemplateCacheTest method testIncompatibleImprovementsChangesURLConCaching.
@Test
public void testIncompatibleImprovementsChangesURLConCaching() throws IOException {
Version newVersion = Configuration.VERSION_2_3_21;
Version oldVersion = Configuration.VERSION_2_3_20;
{
Configuration cfg = new Configuration(oldVersion);
cfg.setTemplateUpdateDelay(0);
final MonitoredClassTemplateLoader templateLoader = new MonitoredClassTemplateLoader();
assertNull(templateLoader.getURLConnectionUsesCaches());
cfg.setTemplateLoader(templateLoader);
assertNull(templateLoader.getLastTemplateSourceModification());
cfg.getTemplate("test.ftl");
assertNull(templateLoader.getLastTemplateSourceModification());
cfg.setIncompatibleImprovements(newVersion);
assertNull(templateLoader.getLastTemplateSourceModification());
cfg.getTemplate("test.ftl");
assertEquals(Boolean.FALSE, templateLoader.getLastTemplateSourceModification());
templateLoader.setURLConnectionUsesCaches(Boolean.valueOf(true));
templateLoader.setLastTemplateSourceModification(null);
cfg.getTemplate("test.ftl");
assertNull(templateLoader.getLastTemplateSourceModification());
templateLoader.setURLConnectionUsesCaches(Boolean.valueOf(false));
templateLoader.setLastTemplateSourceModification(null);
cfg.getTemplate("test.ftl");
assertNull(templateLoader.getLastTemplateSourceModification());
templateLoader.setURLConnectionUsesCaches(null);
templateLoader.setLastTemplateSourceModification(null);
cfg.getTemplate("test.ftl");
assertEquals(Boolean.FALSE, templateLoader.getLastTemplateSourceModification());
templateLoader.setURLConnectionUsesCaches(null);
cfg.setIncompatibleImprovements(oldVersion);
templateLoader.setLastTemplateSourceModification(null);
cfg.getTemplate("test.ftl");
assertNull(templateLoader.getLastTemplateSourceModification());
cfg.setTemplateLoader(new MultiTemplateLoader(new TemplateLoader[] { new MultiTemplateLoader(new TemplateLoader[] { templateLoader }) }));
cfg.setIncompatibleImprovements(newVersion);
cfg.getTemplate("test.ftl");
assertEquals(Boolean.FALSE, templateLoader.getLastTemplateSourceModification());
}
}
use of freemarker.template.Configuration in project freemarker by apache.
the class TemplateCacheTest method testManualRemovalI18ed.
@Test
public void testManualRemovalI18ed() throws IOException {
Configuration cfg = new Configuration();
cfg.setCacheStorage(new StrongCacheStorage());
cfg.setLocale(Locale.US);
StringTemplateLoader loader = new StringTemplateLoader();
cfg.setTemplateLoader(loader);
cfg.setTemplateUpdateDelay(Integer.MAX_VALUE);
loader.putTemplate("1_en_US.ftl", "1_en_US v1");
loader.putTemplate("1_en.ftl", "1_en v1");
loader.putTemplate("1.ftl", "1 v1");
assertEquals("1_en_US v1", cfg.getTemplate("1.ftl").toString());
assertEquals("1_en v1", cfg.getTemplate("1.ftl", Locale.UK).toString());
assertEquals("1 v1", cfg.getTemplate("1.ftl", Locale.GERMANY).toString());
loader.putTemplate("1_en_US.ftl", "1_en_US v2");
loader.putTemplate("1_en.ftl", "1_en v2");
loader.putTemplate("1.ftl", "1 v2");
assertEquals("1_en_US v1", cfg.getTemplate("1.ftl").toString());
assertEquals("1_en v1", cfg.getTemplate("1.ftl", Locale.UK).toString());
assertEquals("1 v1", cfg.getTemplate("1.ftl", Locale.GERMANY).toString());
cfg.removeTemplateFromCache("1.ftl");
assertEquals("1_en_US v2", cfg.getTemplate("1.ftl").toString());
assertEquals("1_en v1", cfg.getTemplate("1.ftl", Locale.UK).toString());
assertEquals("1 v1", cfg.getTemplate("1.ftl", Locale.GERMANY).toString());
assertEquals("1 v2", cfg.getTemplate("1.ftl", Locale.ITALY).toString());
cfg.removeTemplateFromCache("1.ftl", Locale.GERMANY);
assertEquals("1_en v1", cfg.getTemplate("1.ftl", Locale.UK).toString());
assertEquals("1 v2", cfg.getTemplate("1.ftl", Locale.GERMANY).toString());
cfg.removeTemplateFromCache("1.ftl", Locale.CANADA);
assertEquals("1_en v1", cfg.getTemplate("1.ftl", Locale.UK).toString());
cfg.removeTemplateFromCache("1.ftl", Locale.UK);
assertEquals("1_en v2", cfg.getTemplate("1.ftl", Locale.UK).toString());
}
use of freemarker.template.Configuration in project freemarker by apache.
the class TemplateCacheTest method testEncodingSelection.
@Test
public void testEncodingSelection() throws IOException {
Locale hungary = new Locale("hu", "HU");
Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
cfg.setDefaultEncoding("utf-8");
StringTemplateLoader tl = new StringTemplateLoader();
tl.putTemplate("t.ftl", "Foo");
tl.putTemplate("t_de.ftl", "Vuu");
tl.putTemplate("t2.ftl", "<#ftl encoding='UTF-16LE'>Foo");
tl.putTemplate("t2_de.ftl", "<#ftl encoding='UTF-16BE'>Vuu");
cfg.setTemplateLoader(tl);
// No locale-to-encoding mapping exists yet:
{
Template t = cfg.getTemplate("t.ftl", Locale.GERMANY);
assertEquals("t.ftl", t.getName());
assertEquals("t_de.ftl", t.getSourceName());
assertEquals("utf-8", t.getEncoding());
assertEquals("Vuu", t.toString());
}
cfg.setEncoding(Locale.GERMANY, "ISO-8859-1");
cfg.setEncoding(hungary, "ISO-8859-2");
{
Template t = cfg.getTemplate("t.ftl", Locale.CHINESE);
assertEquals("t.ftl", t.getName());
assertEquals("t.ftl", t.getSourceName());
assertEquals("utf-8", t.getEncoding());
assertEquals("Foo", t.toString());
}
{
Template t = cfg.getTemplate("t.ftl", Locale.GERMANY);
assertEquals("t.ftl", t.getName());
assertEquals("t_de.ftl", t.getSourceName());
assertEquals("ISO-8859-1", t.getEncoding());
assertEquals("Vuu", t.toString());
}
{
Template t = cfg.getTemplate("t.ftl", hungary);
assertEquals("t.ftl", t.getName());
assertEquals("t.ftl", t.getSourceName());
assertEquals("ISO-8859-2", t.getEncoding());
assertEquals("Foo", t.toString());
}
// #ftl header overrides:
{
Template t = cfg.getTemplate("t2.ftl", Locale.CHINESE);
assertEquals("t2.ftl", t.getName());
assertEquals("t2.ftl", t.getSourceName());
assertEquals("UTF-16LE", t.getEncoding());
assertEquals("Foo", t.toString());
}
{
Template t = cfg.getTemplate("t2.ftl", Locale.GERMANY);
assertEquals("t2.ftl", t.getName());
assertEquals("t2_de.ftl", t.getSourceName());
assertEquals("UTF-16BE", t.getEncoding());
assertEquals("Vuu", t.toString());
}
{
Template t = cfg.getTemplate("t2.ftl", hungary);
assertEquals("t2.ftl", t.getName());
assertEquals("t2.ftl", t.getSourceName());
assertEquals("UTF-16LE", t.getEncoding());
assertEquals("Foo", t.toString());
}
}
Aggregations