Search in sources :

Example 1 with TemplateConfiguration

use of freemarker.core.TemplateConfiguration in project freemarker by apache.

the class MergingTemplateConfigurationFactory method get.

@Override
public TemplateConfiguration get(String sourceName, Object templateSource) throws IOException, TemplateConfigurationFactoryException {
    TemplateConfiguration mergedTC = null;
    TemplateConfiguration resultTC = null;
    for (TemplateConfigurationFactory tcf : templateConfigurationFactories) {
        TemplateConfiguration tc = tcf.get(sourceName, templateSource);
        if (tc != null) {
            if (resultTC == null) {
                resultTC = tc;
            } else {
                if (mergedTC == null) {
                    Configuration cfg = getConfiguration();
                    if (cfg == null) {
                        throw new IllegalStateException("The TemplateConfigurationFactory wasn't associated to a Configuration yet.");
                    }
                    mergedTC = new TemplateConfiguration();
                    mergedTC.setParentConfiguration(cfg);
                    mergedTC.merge(resultTC);
                    resultTC = mergedTC;
                }
                mergedTC.merge(tc);
            }
        }
    }
    return resultTC;
}
Also used : Configuration(freemarker.template.Configuration) TemplateConfiguration(freemarker.core.TemplateConfiguration) TemplateConfiguration(freemarker.core.TemplateConfiguration)

Example 2 with TemplateConfiguration

use of freemarker.core.TemplateConfiguration in project freemarker by apache.

the class TemplateConfigurationExamples method example1.

@Test
public void example1() throws Exception {
    Configuration cfg = getConfiguration();
    addTemplate("t.xml", "");
    TemplateConfiguration tcUTF8XML = new TemplateConfiguration();
    tcUTF8XML.setEncoding("utf-8");
    tcUTF8XML.setOutputFormat(XMLOutputFormat.INSTANCE);
    {
        cfg.setTemplateConfigurations(new ConditionalTemplateConfigurationFactory(new FileExtensionMatcher("xml"), tcUTF8XML));
        Template t = cfg.getTemplate("t.xml");
        assertEquals("utf-8", t.getEncoding());
        assertEquals(XMLOutputFormat.INSTANCE, t.getOutputFormat());
    }
    {
        cfg.setTemplateConfigurations(null);
        cfg.setSettings(loadPropertiesFile("TemplateConfigurationExamples1.properties"));
        Template t = cfg.getTemplate("t.xml");
        assertEquals("utf-8", t.getEncoding());
        assertEquals(XMLOutputFormat.INSTANCE, t.getOutputFormat());
    }
}
Also used : TemplateConfiguration(freemarker.core.TemplateConfiguration) Configuration(freemarker.template.Configuration) TemplateConfiguration(freemarker.core.TemplateConfiguration) FileExtensionMatcher(freemarker.cache.FileExtensionMatcher) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory) Template(freemarker.template.Template) Test(org.junit.Test)

Example 3 with TemplateConfiguration

use of freemarker.core.TemplateConfiguration 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());
}
Also used : MergingTemplateConfigurationFactory(freemarker.cache.MergingTemplateConfigurationFactory) PathGlobMatcher(freemarker.cache.PathGlobMatcher) TemplateConfiguration(freemarker.core.TemplateConfiguration) Configuration(freemarker.template.Configuration) OrMatcher(freemarker.cache.OrMatcher) FileNameGlobMatcher(freemarker.cache.FileNameGlobMatcher) FirstMatchTemplateConfigurationFactory(freemarker.cache.FirstMatchTemplateConfigurationFactory) TemplateConfiguration(freemarker.core.TemplateConfiguration) FileExtensionMatcher(freemarker.cache.FileExtensionMatcher) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory) Date(java.util.Date) Test(org.junit.Test)

Example 4 with TemplateConfiguration

use of freemarker.core.TemplateConfiguration in project freemarker by apache.

the class TemplateConfigurationFactoryTest method testCondition1.

@Test
public void testCondition1() throws IOException, TemplateConfigurationFactoryException {
    TemplateConfiguration tc = newTemplateConfiguration(1);
    TemplateConfigurationFactory tcf = new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*.ftlx"), tc);
    tcf.setConfiguration(cfg);
    assertNotApplicable(tcf, "x.ftl");
    assertApplicable(tcf, "x.ftlx", tc);
}
Also used : TemplateConfiguration(freemarker.core.TemplateConfiguration) Test(org.junit.Test)

Example 5 with TemplateConfiguration

use of freemarker.core.TemplateConfiguration in project freemarker by apache.

the class TemplateConfigurationFactoryTest method testComplex.

@Test
public void testComplex() throws IOException, TemplateConfigurationFactoryException {
    TemplateConfiguration tcA = newTemplateConfiguration(1);
    TemplateConfiguration tcBSpec = newTemplateConfiguration(2);
    TemplateConfiguration tcBCommon = newTemplateConfiguration(3);
    TemplateConfiguration tcHH = newTemplateConfiguration(4);
    TemplateConfiguration tcHtml = newTemplateConfiguration(5);
    TemplateConfiguration tcXml = newTemplateConfiguration(6);
    TemplateConfiguration tcNWS = newTemplateConfiguration(7);
    TemplateConfigurationFactory tcf = new MergingTemplateConfigurationFactory(new FirstMatchTemplateConfigurationFactory(new ConditionalTemplateConfigurationFactory(new PathGlobMatcher("a/**"), tcA), new ConditionalTemplateConfigurationFactory(new PathGlobMatcher("b/**"), new MergingTemplateConfigurationFactory(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*"), tcBCommon), new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*.s.*"), tcBSpec)))).allowNoMatch(true), new FirstMatchTemplateConfigurationFactory(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*.hh"), tcHH), new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*.*h"), tcHtml), new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*.*x"), tcXml)).allowNoMatch(true), new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*.nws.*"), tcNWS));
    tcf.setConfiguration(cfg);
    assertNotApplicable(tcf, "x.ftl");
    assertApplicable(tcf, "b/x.ftl", tcBCommon);
    assertApplicable(tcf, "b/x.s.ftl", tcBCommon, tcBSpec);
    assertApplicable(tcf, "b/x.s.ftlh", tcBCommon, tcBSpec, tcHtml);
    assertApplicable(tcf, "b/x.s.nws.ftlx", tcBCommon, tcBSpec, tcXml, tcNWS);
    assertApplicable(tcf, "a/x.s.nws.ftlx", tcA, tcXml, tcNWS);
    assertApplicable(tcf, "a.hh", tcHH);
    assertApplicable(tcf, "a.nws.hh", tcHH, tcNWS);
}
Also used : TemplateConfiguration(freemarker.core.TemplateConfiguration) Test(org.junit.Test)

Aggregations

TemplateConfiguration (freemarker.core.TemplateConfiguration)14 Test (org.junit.Test)10 Configuration (freemarker.template.Configuration)5 ConditionalTemplateConfigurationFactory (freemarker.cache.ConditionalTemplateConfigurationFactory)4 FileExtensionMatcher (freemarker.cache.FileExtensionMatcher)3 FirstMatchTemplateConfigurationFactory (freemarker.cache.FirstMatchTemplateConfigurationFactory)3 PathGlobMatcher (freemarker.cache.PathGlobMatcher)3 FileNameGlobMatcher (freemarker.cache.FileNameGlobMatcher)2 OrMatcher (freemarker.cache.OrMatcher)2 Template (freemarker.template.Template)2 MergingTemplateConfigurationFactory (freemarker.cache.MergingTemplateConfigurationFactory)1 Reader (java.io.Reader)1 StringWriter (java.io.StringWriter)1 Date (java.util.Date)1