Search in sources :

Example 6 with TemplateConfiguration

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

the class TemplateConfigurationFactoryTest method testFirstMatch.

@Test
public void testFirstMatch() throws IOException, TemplateConfigurationFactoryException {
    TemplateConfiguration tc1 = newTemplateConfiguration(1);
    TemplateConfiguration tc2 = newTemplateConfiguration(2);
    TemplateConfiguration tc3 = newTemplateConfiguration(3);
    FirstMatchTemplateConfigurationFactory tcf = new FirstMatchTemplateConfigurationFactory(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*.ftlx"), tc1), new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*a*.*"), tc2), new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*b*.*"), tc3));
    tcf.setConfiguration(cfg);
    try {
        assertNotApplicable(tcf, "x.ftl");
    } catch (TemplateConfigurationFactoryException e) {
        assertThat(e.getMessage(), containsString("x.ftl"));
    }
    tcf.setNoMatchErrorDetails("Test details");
    try {
        assertNotApplicable(tcf, "x.ftl");
    } catch (TemplateConfigurationFactoryException e) {
        assertThat(e.getMessage(), containsString("Test details"));
    }
    tcf.setAllowNoMatch(true);
    assertNotApplicable(tcf, "x.ftl");
    assertApplicable(tcf, "x.ftlx", tc1);
    assertApplicable(tcf, "a.ftl", tc2);
    assertApplicable(tcf, "b.ftl", tc3);
    assertApplicable(tcf, "a.ftlx", tc1);
    assertApplicable(tcf, "b.ftlx", tc1);
    assertApplicable(tcf, "ab.ftl", tc2);
    assertApplicable(tcf, "ab.ftlx", tc1);
    assertNotApplicable(new FirstMatchTemplateConfigurationFactory().allowNoMatch(true), "x.ftl");
}
Also used : TemplateConfiguration(freemarker.core.TemplateConfiguration) Test(org.junit.Test)

Example 7 with TemplateConfiguration

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

the class TemplateConfigurationFactoryTest method assertApplicable.

private void assertApplicable(TemplateConfigurationFactory tcf, String sourceName, TemplateConfiguration... expectedTCs) throws IOException, TemplateConfigurationFactoryException {
    TemplateConfiguration mergedTC = tcf.get(sourceName, "dummy");
    assertNotNull("TC should have its parents Configuration set", mergedTC.getParentConfiguration());
    List<String> mergedTCAttNames = Arrays.asList(mergedTC.getCustomAttributeNames());
    for (TemplateConfiguration expectedTC : expectedTCs) {
        Integer tcId = (Integer) expectedTC.getCustomAttribute("id");
        if (tcId == null) {
            fail("TemplateConfiguration-s must be created with newTemplateConfiguration(id) in this test");
        }
        if (!mergedTCAttNames.contains("contains" + tcId)) {
            fail("TemplateConfiguration with ID " + tcId + " is missing from the asserted value");
        }
    }
    for (String attName : mergedTCAttNames) {
        if (!containsCustomAttr(attName, expectedTCs)) {
            fail("The asserted TemplateConfiguration contains an unexpected custom attribute: " + attName);
        }
    }
    assertEquals(expectedTCs[expectedTCs.length - 1].getCustomAttribute("id"), mergedTC.getCustomAttribute("id"));
}
Also used : TemplateConfiguration(freemarker.core.TemplateConfiguration)

Example 8 with TemplateConfiguration

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

the class TemplateConfigurationFactoryTest method testMerging.

@Test
public void testMerging() throws IOException, TemplateConfigurationFactoryException {
    TemplateConfiguration tc1 = newTemplateConfiguration(1);
    TemplateConfiguration tc2 = newTemplateConfiguration(2);
    TemplateConfiguration tc3 = newTemplateConfiguration(3);
    TemplateConfigurationFactory tcf = new MergingTemplateConfigurationFactory(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*.ftlx"), tc1), new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*a*.*"), tc2), new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*b*.*"), tc3));
    tcf.setConfiguration(cfg);
    assertNotApplicable(tcf, "x.ftl");
    assertApplicable(tcf, "x.ftlx", tc1);
    assertApplicable(tcf, "a.ftl", tc2);
    assertApplicable(tcf, "b.ftl", tc3);
    assertApplicable(tcf, "a.ftlx", tc1, tc2);
    assertApplicable(tcf, "b.ftlx", tc1, tc3);
    assertApplicable(tcf, "ab.ftl", tc2, tc3);
    assertApplicable(tcf, "ab.ftlx", tc1, tc2, tc3);
    assertNotApplicable(new MergingTemplateConfigurationFactory(), "x.ftl");
}
Also used : TemplateConfiguration(freemarker.core.TemplateConfiguration) Test(org.junit.Test)

Example 9 with TemplateConfiguration

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

the class TemplateCache method loadTemplate.

private Template loadTemplate(final TemplateLoader templateLoader, final Object source, final String name, final String sourceName, Locale locale, final Object customLookupCondition, String initialEncoding, final boolean parseAsFTL) throws IOException {
    final TemplateConfiguration tc;
    try {
        tc = templateConfigurations != null ? templateConfigurations.get(sourceName, source) : null;
    } catch (TemplateConfigurationFactoryException e) {
        throw newIOException("Error while getting TemplateConfiguration; see cause exception.", e);
    }
    if (tc != null) {
        // TC.{encoding,locale} is stronger than the cfg.getTemplate arguments by design.
        if (tc.isEncodingSet()) {
            initialEncoding = tc.getEncoding();
        }
        if (tc.isLocaleSet()) {
            locale = tc.getLocale();
        }
    }
    Template template;
    {
        if (parseAsFTL) {
            try {
                final Reader reader = templateLoader.getReader(source, initialEncoding);
                try {
                    template = new Template(name, sourceName, reader, config, tc, initialEncoding);
                } finally {
                    reader.close();
                }
            } catch (Template.WrongEncodingException wee) {
                String actualEncoding = wee.getTemplateSpecifiedEncoding();
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Initial encoding \"" + initialEncoding + "\" was incorrect, re-reading with \"" + actualEncoding + "\". Template: " + sourceName);
                }
                final Reader reader = templateLoader.getReader(source, actualEncoding);
                try {
                    template = new Template(name, sourceName, reader, config, tc, actualEncoding);
                } finally {
                    reader.close();
                }
            }
        } else {
            // Read the contents into a StringWriter, then construct a single-text-block template from it.
            final StringWriter sw = new StringWriter();
            final char[] buf = new char[4096];
            final Reader reader = templateLoader.getReader(source, initialEncoding);
            try {
                fetchChars: while (true) {
                    int charsRead = reader.read(buf);
                    if (charsRead > 0) {
                        sw.write(buf, 0, charsRead);
                    } else if (charsRead < 0) {
                        break fetchChars;
                    }
                }
            } finally {
                reader.close();
            }
            template = Template.getPlainTextTemplate(name, sourceName, sw.toString(), config);
            template.setEncoding(initialEncoding);
        }
    }
    if (tc != null) {
        tc.apply(template);
    }
    template.setLocale(locale);
    template.setCustomLookupCondition(customLookupCondition);
    return template;
}
Also used : StringWriter(java.io.StringWriter) Reader(java.io.Reader) TemplateConfiguration(freemarker.core.TemplateConfiguration) Template(freemarker.template.Template)

Example 10 with TemplateConfiguration

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

the class TemplateConfigurationFactoryTest method testCondition2.

@Test
public void testCondition2() throws IOException, TemplateConfigurationFactoryException {
    TemplateConfiguration tc = newTemplateConfiguration(1);
    TemplateConfigurationFactory tcf = new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*.ftlx"), new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("x.*"), tc));
    tcf.setConfiguration(cfg);
    assertNotApplicable(tcf, "x.ftl");
    assertNotApplicable(tcf, "y.ftlx");
    assertApplicable(tcf, "x.ftlx", tc);
}
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