Search in sources :

Example 81 with Configuration

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");
}
Also used : AliasTemplateNumberFormatFactory(freemarker.core.AliasTemplateNumberFormatFactory) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) BaseNTemplateNumberFormatFactory(freemarker.core.BaseNTemplateNumberFormatFactory) TemplateNumberFormatFactory(freemarker.core.TemplateNumberFormatFactory) AliasTemplateNumberFormatFactory(freemarker.core.AliasTemplateNumberFormatFactory) Test(org.junit.Test)

Example 82 with Configuration

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.
}
Also used : Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) OutputStreamWriter(java.io.OutputStreamWriter) Map(java.util.Map) HashMap(java.util.HashMap) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) Template(freemarker.template.Template) Test(org.junit.Test)

Example 83 with Configuration

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());
    }
}
Also used : Configuration(freemarker.template.Configuration) Version(freemarker.template.Version) MonitoredTemplateLoader(freemarker.test.MonitoredTemplateLoader) Test(org.junit.Test)

Example 84 with Configuration

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());
}
Also used : Configuration(freemarker.template.Configuration) Test(org.junit.Test)

Example 85 with Configuration

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());
    }
}
Also used : Locale(java.util.Locale) Configuration(freemarker.template.Configuration) Template(freemarker.template.Template) Test(org.junit.Test)

Aggregations

Configuration (freemarker.template.Configuration)282 Template (freemarker.template.Template)106 Test (org.junit.Test)86 TemplateTest (freemarker.test.TemplateTest)52 HashMap (java.util.HashMap)51 IOException (java.io.IOException)41 StringWriter (java.io.StringWriter)39 File (java.io.File)33 TemplateException (freemarker.template.TemplateException)29 Writer (java.io.Writer)27 ClassTemplateLoader (freemarker.cache.ClassTemplateLoader)19 ConditionalTemplateConfigurationFactory (freemarker.cache.ConditionalTemplateConfigurationFactory)19 StringTemplateLoader (freemarker.cache.StringTemplateLoader)18 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)18 FileNameGlobMatcher (freemarker.cache.FileNameGlobMatcher)17 TemplateLoader (freemarker.cache.TemplateLoader)16 MultiTemplateLoader (freemarker.cache.MultiTemplateLoader)15 FileWriter (java.io.FileWriter)13 ArrayList (java.util.ArrayList)13 User (org.vcell.util.document.User)13