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");
}
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);
}
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());
}
}
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());
}
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);
}
Aggregations