Search in sources :

Example 76 with Configuration

use of freemarker.template.Configuration in project freemarker by apache.

the class UnclosedCommentTest method testLegacyBehavior.

@Test
public void testLegacyBehavior() throws IOException, TemplateException {
    setConfiguration(new Configuration(Configuration.VERSION_2_3_20));
    assertErrorContains(UNCLOSED_COMMENT_0, "end of file");
    assertOutput(UNCLOSED_COMMENT_1, "foo");
    assertOutput(UNCLOSED_COMMENT_2, "foo");
    assertOutput(UNCLOSED_COMMENT_3, "foo\n");
    assertErrorContains(UNCLOSED_NOPARSE_0, "end of file");
    assertOutput(UNCLOSED_NOPARSE_1, "foo");
    assertOutput(UNCLOSED_NOPARSE_2, "foo");
    assertOutput(UNCLOSED_NOPARSE_3, "foo\n");
}
Also used : Configuration(freemarker.template.Configuration) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Example 77 with Configuration

use of freemarker.template.Configuration in project freemarker by apache.

the class TemplateConfiguration method apply.

/**
 * Sets those settings of the {@link Template} which aren't yet set in the {@link Template} and are set in this
 * {@link TemplateConfiguration}, leaves the other settings as is. A setting is said to be set in a
 * {@link TemplateConfiguration} or {@link Template} if it was explicitly set via a setter method on that object, as
 * opposed to be inherited from the {@link Configuration}.
 *
 * <p>
 * Note that this method doesn't deal with settings that influence the parser, as those are already baked in at this
 * point via the {@link ParserConfiguration}.
 *
 * <p>
 * Note that the {@code encoding} setting of the {@link Template} counts as unset if it's {@code null},
 * even if {@code null} was set via {@link Template#setEncoding(String)}.
 *
 * @throws IllegalStateException
 *             If the parent configuration wasn't yet set.
 */
public void apply(Template template) {
    Configuration cfg = getNonNullParentConfiguration();
    if (template.getConfiguration() != cfg) {
        // This is actually not a problem right now, but for future BC we enforce this.
        throw new IllegalArgumentException("The argument Template doesn't belong to the same Configuration as the TemplateConfiguration");
    }
    if (isAPIBuiltinEnabledSet() && !template.isAPIBuiltinEnabledSet()) {
        template.setAPIBuiltinEnabled(isAPIBuiltinEnabled());
    }
    if (isArithmeticEngineSet() && !template.isArithmeticEngineSet()) {
        template.setArithmeticEngine(getArithmeticEngine());
    }
    if (isAutoFlushSet() && !template.isAutoFlushSet()) {
        template.setAutoFlush(getAutoFlush());
    }
    if (isBooleanFormatSet() && !template.isBooleanFormatSet()) {
        template.setBooleanFormat(getBooleanFormat());
    }
    if (isClassicCompatibleSet() && !template.isClassicCompatibleSet()) {
        template.setClassicCompatibleAsInt(getClassicCompatibleAsInt());
    }
    if (isCustomDateFormatsSet()) {
        template.setCustomDateFormats(mergeMaps(getCustomDateFormats(), template.getCustomDateFormatsWithoutFallback(), false));
    }
    if (isCustomNumberFormatsSet()) {
        template.setCustomNumberFormats(mergeMaps(getCustomNumberFormats(), template.getCustomNumberFormatsWithoutFallback(), false));
    }
    if (isDateFormatSet() && !template.isDateFormatSet()) {
        template.setDateFormat(getDateFormat());
    }
    if (isDateTimeFormatSet() && !template.isDateTimeFormatSet()) {
        template.setDateTimeFormat(getDateTimeFormat());
    }
    if (isEncodingSet() && template.getEncoding() == null) {
        template.setEncoding(getEncoding());
    }
    if (isLocaleSet() && !template.isLocaleSet()) {
        template.setLocale(getLocale());
    }
    if (isLogTemplateExceptionsSet() && !template.isLogTemplateExceptionsSet()) {
        template.setLogTemplateExceptions(getLogTemplateExceptions());
    }
    if (isWrapUncheckedExceptionsSet() && !template.isWrapUncheckedExceptionsSet()) {
        template.setWrapUncheckedExceptions(getWrapUncheckedExceptions());
    }
    if (isNewBuiltinClassResolverSet() && !template.isNewBuiltinClassResolverSet()) {
        template.setNewBuiltinClassResolver(getNewBuiltinClassResolver());
    }
    if (isNumberFormatSet() && !template.isNumberFormatSet()) {
        template.setNumberFormat(getNumberFormat());
    }
    if (isObjectWrapperSet() && !template.isObjectWrapperSet()) {
        template.setObjectWrapper(getObjectWrapper());
    }
    if (isOutputEncodingSet() && !template.isOutputEncodingSet()) {
        template.setOutputEncoding(getOutputEncoding());
    }
    if (isShowErrorTipsSet() && !template.isShowErrorTipsSet()) {
        template.setShowErrorTips(getShowErrorTips());
    }
    if (isSQLDateAndTimeTimeZoneSet() && !template.isSQLDateAndTimeTimeZoneSet()) {
        template.setSQLDateAndTimeTimeZone(getSQLDateAndTimeTimeZone());
    }
    if (isTemplateExceptionHandlerSet() && !template.isTemplateExceptionHandlerSet()) {
        template.setTemplateExceptionHandler(getTemplateExceptionHandler());
    }
    if (isAttemptExceptionReporterSet() && !template.isAttemptExceptionReporterSet()) {
        template.setAttemptExceptionReporter(getAttemptExceptionReporter());
    }
    if (isTimeFormatSet() && !template.isTimeFormatSet()) {
        template.setTimeFormat(getTimeFormat());
    }
    if (isTimeZoneSet() && !template.isTimeZoneSet()) {
        template.setTimeZone(getTimeZone());
    }
    if (isURLEscapingCharsetSet() && !template.isURLEscapingCharsetSet()) {
        template.setURLEscapingCharset(getURLEscapingCharset());
    }
    if (isLazyImportsSet() && !template.isLazyImportsSet()) {
        template.setLazyImports(getLazyImports());
    }
    if (isLazyAutoImportsSet() && !template.isLazyAutoImportsSet()) {
        template.setLazyAutoImports(getLazyAutoImports());
    }
    if (isAutoImportsSet()) {
        // Regarding the order of the maps in the merge:
        // - Existing template-level imports have precedence over those coming from the TC (just as with the others
        // apply()-ed settings), thus for clashing import prefixes they must win.
        // - Template-level imports count as more specific, and so come after the more generic ones from TC.
        template.setAutoImports(mergeMaps(getAutoImports(), template.getAutoImportsWithoutFallback(), true));
    }
    if (isAutoIncludesSet()) {
        template.setAutoIncludes(mergeLists(getAutoIncludes(), template.getAutoIncludesWithoutFallback()));
    }
    copyDirectCustomAttributes(template, false);
}
Also used : Configuration(freemarker.template.Configuration)

Example 78 with Configuration

use of freemarker.template.Configuration 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 79 with Configuration

use of freemarker.template.Configuration 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 80 with Configuration

use of freemarker.template.Configuration in project freemarker by apache.

the class TemplateTest method addTemplate.

protected void addTemplate(String name, String content) {
    Configuration cfg = getConfiguration();
    TemplateLoader tl = cfg.getTemplateLoader();
    StringTemplateLoader stl;
    if (tl != null) {
        stl = extractStringTemplateLoader(tl);
    } else {
        stl = new StringTemplateLoader();
        cfg.setTemplateLoader(stl);
    }
    stl.putTemplate(name, content);
}
Also used : StringTemplateLoader(freemarker.cache.StringTemplateLoader) Configuration(freemarker.template.Configuration) StringTemplateLoader(freemarker.cache.StringTemplateLoader) TemplateLoader(freemarker.cache.TemplateLoader) MultiTemplateLoader(freemarker.cache.MultiTemplateLoader)

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