Class TemplateConfiguration
- java.lang.Object
-
- freemarker.core.Configurable
-
- freemarker.core.TemplateConfiguration
-
- All Implemented Interfaces:
ParserConfiguration
public final class TemplateConfiguration extends Configurable implements ParserConfiguration
Used for customizing the configuration settings for individualTemplate-s (or rather groups of templates), relatively to the common setting values coming from theConfiguration. This was designed with the standard template loading mechanism of FreeMarker in mind (Configuration.getTemplate(String)andTemplateCache), though can also be reused for custom template loading and caching solutions.Note on the
localesetting: When used with the standard template loading/caching mechanism (Configuration.getTemplate(String)and its overloads), localized lookup happens before thelocalespecified here could have effect. Thelocalewill be only set in the template that the localized lookup has already found.Note on the encoding setting
encoding: SeesetEncoding(String).Note that the result value of the reader methods (getter and "is" methods) is usually not useful unless the value of that setting was already set on this object. Otherwise you will get the value from the parent
Configuration, or anIllegalStateExceptionbefore this object is associated to aConfiguration.If you are using this class for your own template loading and caching solution, rather than with the standard one, you should be aware of a few more details:
- This class implements both
ConfigurableandParserConfiguration. This means that it can influence both the template parsing phase and the runtime settings. For both aspects (i.e.,ParserConfigurationandConfigurable) to take effect, you have first pass this object to theTemplateconstructor (this is where theParserConfigurationinterface is used), and then you have to callapply(Template)on the resultingTemplateobject (this is where theConfigurableaspect is used). apply(Template)only change the settings that weren't yet set on theTemplate(but are inherited from theConfiguration). This is primarily because if the template configures itself via the#ftlheader, those values should have precedence. A consequence of this is that if you want to configure the sameTemplatewith multipleTemplateConfiguration-s, you either should merge them to a single one before that (withmerge(TemplateConfiguration)), or you have to apply them in reverse order of their intended precedence.
- Since:
- 2.3.24
- See Also:
Template(String, String, Reader, Configuration, ParserConfiguration, String)
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class freemarker.core.Configurable
Configurable.SettingValueAssignmentException, Configurable.UnknownSettingException
-
-
Field Summary
-
Fields inherited from class freemarker.core.Configurable
API_BUILTIN_ENABLED_KEY, API_BUILTIN_ENABLED_KEY_CAMEL_CASE, API_BUILTIN_ENABLED_KEY_SNAKE_CASE, ARITHMETIC_ENGINE_KEY, ARITHMETIC_ENGINE_KEY_CAMEL_CASE, ARITHMETIC_ENGINE_KEY_SNAKE_CASE, ATTEMPT_EXCEPTION_REPORTER_KEY, ATTEMPT_EXCEPTION_REPORTER_KEY_CAMEL_CASE, ATTEMPT_EXCEPTION_REPORTER_KEY_SNAKE_CASE, AUTO_FLUSH_KEY, AUTO_FLUSH_KEY_CAMEL_CASE, AUTO_FLUSH_KEY_SNAKE_CASE, AUTO_IMPORT_KEY, AUTO_IMPORT_KEY_CAMEL_CASE, AUTO_IMPORT_KEY_SNAKE_CASE, AUTO_INCLUDE_KEY, AUTO_INCLUDE_KEY_CAMEL_CASE, AUTO_INCLUDE_KEY_SNAKE_CASE, BOOLEAN_FORMAT_KEY, BOOLEAN_FORMAT_KEY_CAMEL_CASE, BOOLEAN_FORMAT_KEY_SNAKE_CASE, CLASSIC_COMPATIBLE_KEY, CLASSIC_COMPATIBLE_KEY_CAMEL_CASE, CLASSIC_COMPATIBLE_KEY_SNAKE_CASE, CUSTOM_DATE_FORMATS_KEY, CUSTOM_DATE_FORMATS_KEY_CAMEL_CASE, CUSTOM_DATE_FORMATS_KEY_SNAKE_CASE, CUSTOM_NUMBER_FORMATS_KEY, CUSTOM_NUMBER_FORMATS_KEY_CAMEL_CASE, CUSTOM_NUMBER_FORMATS_KEY_SNAKE_CASE, DATE_FORMAT_KEY, DATE_FORMAT_KEY_CAMEL_CASE, DATE_FORMAT_KEY_SNAKE_CASE, DATETIME_FORMAT_KEY, DATETIME_FORMAT_KEY_CAMEL_CASE, DATETIME_FORMAT_KEY_SNAKE_CASE, LAZY_AUTO_IMPORTS_KEY, LAZY_AUTO_IMPORTS_KEY_CAMEL_CASE, LAZY_AUTO_IMPORTS_KEY_SNAKE_CASE, LAZY_IMPORTS_KEY, LAZY_IMPORTS_KEY_CAMEL_CASE, LAZY_IMPORTS_KEY_SNAKE_CASE, LOCALE_KEY, LOCALE_KEY_CAMEL_CASE, LOCALE_KEY_SNAKE_CASE, LOG_TEMPLATE_EXCEPTIONS_KEY, LOG_TEMPLATE_EXCEPTIONS_KEY_CAMEL_CASE, LOG_TEMPLATE_EXCEPTIONS_KEY_SNAKE_CASE, NEW_BUILTIN_CLASS_RESOLVER_KEY, NEW_BUILTIN_CLASS_RESOLVER_KEY_CAMEL_CASE, NEW_BUILTIN_CLASS_RESOLVER_KEY_SNAKE_CASE, NUMBER_FORMAT_KEY, NUMBER_FORMAT_KEY_CAMEL_CASE, NUMBER_FORMAT_KEY_SNAKE_CASE, OBJECT_WRAPPER_KEY, OBJECT_WRAPPER_KEY_CAMEL_CASE, OBJECT_WRAPPER_KEY_SNAKE_CASE, OUTPUT_ENCODING_KEY, OUTPUT_ENCODING_KEY_CAMEL_CASE, OUTPUT_ENCODING_KEY_SNAKE_CASE, SHOW_ERROR_TIPS_KEY, SHOW_ERROR_TIPS_KEY_CAMEL_CASE, SHOW_ERROR_TIPS_KEY_SNAKE_CASE, SQL_DATE_AND_TIME_TIME_ZONE_KEY, SQL_DATE_AND_TIME_TIME_ZONE_KEY_CAMEL_CASE, SQL_DATE_AND_TIME_TIME_ZONE_KEY_SNAKE_CASE, STRICT_BEAN_MODELS, STRICT_BEAN_MODELS_KEY, STRICT_BEAN_MODELS_KEY_CAMEL_CASE, STRICT_BEAN_MODELS_KEY_SNAKE_CASE, TEMPLATE_EXCEPTION_HANDLER_KEY, TEMPLATE_EXCEPTION_HANDLER_KEY_CAMEL_CASE, TEMPLATE_EXCEPTION_HANDLER_KEY_SNAKE_CASE, TIME_FORMAT_KEY, TIME_FORMAT_KEY_CAMEL_CASE, TIME_FORMAT_KEY_SNAKE_CASE, TIME_ZONE_KEY, TIME_ZONE_KEY_CAMEL_CASE, TIME_ZONE_KEY_SNAKE_CASE, TRUNCATE_BUILTIN_ALGORITHM_KEY, TRUNCATE_BUILTIN_ALGORITHM_KEY_CAMEL_CASE, TRUNCATE_BUILTIN_ALGORITHM_KEY_SNAKE_CASE, URL_ESCAPING_CHARSET_KEY, URL_ESCAPING_CHARSET_KEY_CAMEL_CASE, URL_ESCAPING_CHARSET_KEY_SNAKE_CASE, WRAP_UNCHECKED_EXCEPTIONS_KEY, WRAP_UNCHECKED_EXCEPTIONS_KEY_CAMEL_CASE, WRAP_UNCHECKED_EXCEPTIONS_KEY_SNAKE_CASE
-
-
Constructor Summary
Constructors Constructor Description TemplateConfiguration()Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidapply(Template template)Sets those settings of theTemplatewhich aren't yet set in theTemplateand are set in thisTemplateConfiguration, leaves the other settings as is.intgetAutoEscapingPolicy()The getter pair ofsetAutoEscapingPolicy(int).java.lang.StringgetEncoding()VersiongetIncompatibleImprovements()ReturnsConfiguration.getIncompatibleImprovements()from the parentConfiguration.intgetInterpolationSyntax()The getter pair ofsetInterpolationSyntax(int).intgetNamingConvention()The getter pair ofsetNamingConvention(int).OutputFormatgetOutputFormat()The getter pair ofsetOutputFormat(OutputFormat).ConfigurationgetParentConfiguration()Returns the parentConfiguration, ornullif none was associated yet.booleangetRecognizeStandardFileExtensions()Getter pair ofsetRecognizeStandardFileExtensions(boolean).booleangetStrictSyntaxMode()The getter pair ofsetStrictSyntaxMode(boolean).intgetTabSize()Getter pair ofsetTabSize(int).intgetTagSyntax()The getter pair ofsetTagSyntax(int).booleangetWhitespaceStripping()The getter pair ofgetWhitespaceStripping().booleanisAutoEscapingPolicySet()Tells if this setting is set directly in this object or its value is coming from theparent.booleanisEncodingSet()booleanisInterpolationSyntaxSet()Tells if this setting is set directly in this object or its value is coming from theparent.booleanisNamingConventionSet()Tells if this setting is set directly in this object or its value is coming from theparent.booleanisOutputFormatSet()Tells if this setting is set directly in this object or its value is coming from theparent.booleanisRecognizeStandardFileExtensionsSet()Tells if this setting is set directly in this object or its value is coming from theparent.booleanisStrictSyntaxModeSet()Tells if this setting is set directly in this object or its value is coming from theparent.booleanisTabSizeSet()Tells if this setting is set directly in this object or its value is coming from theparent.booleanisTagSyntaxSet()Tells if this setting is set directly in this object or its value is coming from theparent.booleanisWhitespaceStrippingSet()Tells if this setting is set directly in this object or its value is coming from theparent.voidmerge(TemplateConfiguration tc)Set all settings in thisTemplateConfigurationthat were set in the parameterTemplateConfiguration, possibly overwriting the earlier value in this object.voidsetAutoEscapingPolicy(int autoEscapingPolicy)Sets the output format of the template; seeConfiguration.setAutoEscapingPolicy(int)for more.voidsetEncoding(java.lang.String encoding)When the standard template loading/caching mechanism is used, this forces the charset used for reading the template "file", overriding everything but the encoding coming from the#ftlheader.voidsetInterpolationSyntax(int interpolationSyntax)voidsetNamingConvention(int namingConvention)voidsetOutputFormat(OutputFormat outputFormat)Sets the output format of the template; seeConfiguration.setOutputFormat(OutputFormat)for more.voidsetParentConfiguration(Configuration cfg)Associates this instance with aConfiguration; usually you don't call this, as it's called internally when this instance is added to aConfiguration.voidsetRecognizeStandardFileExtensions(boolean recognizeStandardFileExtensions)voidsetStrictBeanModels(boolean strict)voidsetStrictSyntaxMode(boolean strictSyntaxMode)voidsetTabSize(int tabSize)voidsetTagSyntax(int tagSyntax)voidsetWhitespaceStripping(boolean whitespaceStripping)-
Methods inherited from class freemarker.core.Configurable
addAutoImport, addAutoInclude, clone, doAutoImportsAndIncludes, getArithmeticEngine, getAttemptExceptionReporter, getAutoFlush, getAutoImports, getAutoImportsWithoutFallback, getAutoIncludes, getAutoIncludesWithoutFallback, getBooleanFormat, getClassicCompatibleAsInt, getCorrectedNameForUnknownSetting, getCustomAttribute, getCustomAttributeNames, getCustomDateFormat, getCustomDateFormats, getCustomDateFormatsWithoutFallback, getCustomNumberFormat, getCustomNumberFormats, getCustomNumberFormatsWithoutFallback, getDateFormat, getDateTimeFormat, getEnvironment, getLazyAutoImports, getLazyImports, getLocale, getLogTemplateExceptions, getNewBuiltinClassResolver, getNumberFormat, getObjectWrapper, getOutputEncoding, getParent, getSetting, getSettingNames, getSettings, getShowErrorTips, getSQLDateAndTimeTimeZone, getTemplateExceptionHandler, getTimeFormat, getTimeZone, getTruncateBuiltinAlgorithm, getURLEscapingCharset, getWrapUncheckedExceptions, hasCustomFormats, invalidSettingValueException, isAPIBuiltinEnabled, isAPIBuiltinEnabledSet, isArithmeticEngineSet, isAttemptExceptionReporterSet, isAutoFlushSet, isAutoImportsSet, isAutoIncludesSet, isBooleanFormatSet, isClassicCompatible, isClassicCompatibleSet, isCustomDateFormatsSet, isCustomNumberFormatsSet, isDateFormatSet, isDateTimeFormatSet, isLazyAutoImportsSet, isLazyImportsSet, isLocaleSet, isLogTemplateExceptionsSet, isNewBuiltinClassResolverSet, isNumberFormatSet, isObjectWrapperSet, isOutputEncodingSet, isShowErrorTipsSet, isSQLDateAndTimeTimeZoneSet, isTemplateExceptionHandlerSet, isTimeFormatSet, isTimeZoneSet, isTruncateBuiltinAlgorithmSet, isURLEscapingCharsetSet, isWrapUncheckedExceptionsSet, parseAsImportList, parseAsList, parseAsSegmentedList, removeAutoImport, removeAutoInclude, removeCustomAttribute, setAPIBuiltinEnabled, setArithmeticEngine, setAttemptExceptionReporter, setAutoFlush, setAutoImports, setAutoIncludes, setBooleanFormat, setClassicCompatible, setClassicCompatibleAsInt, setCustomAttribute, setCustomDateFormats, setCustomNumberFormats, setDateFormat, setDateTimeFormat, setLazyAutoImports, setLazyImports, setLocale, setLogTemplateExceptions, setNewBuiltinClassResolver, setNumberFormat, setObjectWrapper, setOutputEncoding, setSetting, setSettings, setSettings, setShowErrorTips, setSQLDateAndTimeTimeZone, setTemplateExceptionHandler, setTimeFormat, setTimeZone, settingValueAssignmentException, setTruncateBuiltinAlgorithm, setURLEscapingCharset, setWrapUncheckedExceptions, unknownSettingException
-
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface freemarker.core.ParserConfiguration
getArithmeticEngine
-
-
-
-
Constructor Detail
-
TemplateConfiguration
public TemplateConfiguration()
Creates a new instance. The parent will beConfiguration.getDefaultConfiguration()initially, but it will be changed to the real parentConfigurationwhen this object is added to theConfiguration. (It's not allowed to add the same instance to multipleConfiguration-s).
-
-
Method Detail
-
setParentConfiguration
public void setParentConfiguration(Configuration cfg)
Associates this instance with aConfiguration; usually you don't call this, as it's called internally when this instance is added to aConfiguration. This method can be called only once (except with the sameConfigurationparameter again, as that changes nothing anyway).- Throws:
java.lang.IllegalArgumentException- if the argument isnullor not aConfigurationjava.lang.IllegalStateException- if this object is already associated to a differentConfigurationobject, or if theConfigurationhas#getIncompatibleImprovements()less than 2.3.22 and this object tries to change any non-parser settings
-
getParentConfiguration
public Configuration getParentConfiguration()
Returns the parentConfiguration, ornullif none was associated yet.
-
merge
public void merge(TemplateConfiguration tc)
Set all settings in thisTemplateConfigurationthat were set in the parameterTemplateConfiguration, possibly overwriting the earlier value in this object. (A setting is said to be set in aTemplateConfigurationif it was explicitly set via a setter method, as opposed to be inherited.)
-
apply
public void apply(Template template)
Sets those settings of theTemplatewhich aren't yet set in theTemplateand are set in thisTemplateConfiguration, leaves the other settings as is. A setting is said to be set in aTemplateConfigurationorTemplateif it was explicitly set via a setter method on that object, as opposed to be inherited from theConfiguration.Note that this method doesn't deal with settings that influence the parser, as those are already baked in at this point via the
ParserConfiguration.Note that the
encodingsetting of theTemplatecounts as unset if it'snull, even ifnullwas set viaTemplate.setEncoding(String).- Throws:
java.lang.IllegalStateException- If the parent configuration wasn't yet set.
-
setTagSyntax
public void setTagSyntax(int tagSyntax)
-
getTagSyntax
public int getTagSyntax()
The getter pair ofsetTagSyntax(int).- Specified by:
getTagSyntaxin interfaceParserConfiguration
-
isTagSyntaxSet
public boolean isTagSyntaxSet()
Tells if this setting is set directly in this object or its value is coming from theparent.
-
setInterpolationSyntax
public void setInterpolationSyntax(int interpolationSyntax)
-
getInterpolationSyntax
public int getInterpolationSyntax()
The getter pair ofsetInterpolationSyntax(int).- Specified by:
getInterpolationSyntaxin interfaceParserConfiguration
-
isInterpolationSyntaxSet
public boolean isInterpolationSyntaxSet()
Tells if this setting is set directly in this object or its value is coming from theparent.
-
setNamingConvention
public void setNamingConvention(int namingConvention)
-
getNamingConvention
public int getNamingConvention()
The getter pair ofsetNamingConvention(int).- Specified by:
getNamingConventionin interfaceParserConfiguration
-
isNamingConventionSet
public boolean isNamingConventionSet()
Tells if this setting is set directly in this object or its value is coming from theparent.
-
setWhitespaceStripping
public void setWhitespaceStripping(boolean whitespaceStripping)
-
getWhitespaceStripping
public boolean getWhitespaceStripping()
The getter pair ofgetWhitespaceStripping().- Specified by:
getWhitespaceStrippingin interfaceParserConfiguration
-
isWhitespaceStrippingSet
public boolean isWhitespaceStrippingSet()
Tells if this setting is set directly in this object or its value is coming from theparent.
-
setAutoEscapingPolicy
public void setAutoEscapingPolicy(int autoEscapingPolicy)
Sets the output format of the template; seeConfiguration.setAutoEscapingPolicy(int)for more.
-
getAutoEscapingPolicy
public int getAutoEscapingPolicy()
The getter pair ofsetAutoEscapingPolicy(int).- Specified by:
getAutoEscapingPolicyin interfaceParserConfiguration
-
isAutoEscapingPolicySet
public boolean isAutoEscapingPolicySet()
Tells if this setting is set directly in this object or its value is coming from theparent.
-
setOutputFormat
public void setOutputFormat(OutputFormat outputFormat)
Sets the output format of the template; seeConfiguration.setOutputFormat(OutputFormat)for more.
-
getOutputFormat
public OutputFormat getOutputFormat()
The getter pair ofsetOutputFormat(OutputFormat).- Specified by:
getOutputFormatin interfaceParserConfiguration
-
isOutputFormatSet
public boolean isOutputFormatSet()
Tells if this setting is set directly in this object or its value is coming from theparent.
-
setRecognizeStandardFileExtensions
public void setRecognizeStandardFileExtensions(boolean recognizeStandardFileExtensions)
-
getRecognizeStandardFileExtensions
public boolean getRecognizeStandardFileExtensions()
Getter pair ofsetRecognizeStandardFileExtensions(boolean).- Specified by:
getRecognizeStandardFileExtensionsin interfaceParserConfiguration
-
isRecognizeStandardFileExtensionsSet
public boolean isRecognizeStandardFileExtensionsSet()
Tells if this setting is set directly in this object or its value is coming from theparent.
-
setStrictSyntaxMode
public void setStrictSyntaxMode(boolean strictSyntaxMode)
-
getStrictSyntaxMode
public boolean getStrictSyntaxMode()
The getter pair ofsetStrictSyntaxMode(boolean).- Specified by:
getStrictSyntaxModein interfaceParserConfiguration
-
isStrictSyntaxModeSet
public boolean isStrictSyntaxModeSet()
Tells if this setting is set directly in this object or its value is coming from theparent.
-
setStrictBeanModels
public void setStrictBeanModels(boolean strict)
- Overrides:
setStrictBeanModelsin classConfigurable
-
getEncoding
public java.lang.String getEncoding()
-
setEncoding
public void setEncoding(java.lang.String encoding)
When the standard template loading/caching mechanism is used, this forces the charset used for reading the template "file", overriding everything but the encoding coming from the#ftlheader. This setting overrides the locale-specific encodings set viaConfiguration.setEncoding(java.util.Locale, String). It also overrides theencodingparameter ofConfiguration.getTemplate(String, String)(and of its overloads) and theencodingparameter of the#includedirective. This works like that because specifying the encoding where you are requesting the template is error prone and deprecated.If you are developing your own template loading/caching mechanism instead of the standard one, note that the above behavior is not guaranteed by this class alone; you have to ensure it. Also, read the note on
encodingin the documentation ofapply(Template).
-
isEncodingSet
public boolean isEncodingSet()
-
setTabSize
public void setTabSize(int tabSize)
- Since:
- 2.3.25
-
getTabSize
public int getTabSize()
Getter pair ofsetTabSize(int).- Specified by:
getTabSizein interfaceParserConfiguration- Since:
- 2.3.25
-
isTabSizeSet
public boolean isTabSizeSet()
Tells if this setting is set directly in this object or its value is coming from theparent.- Since:
- 2.3.25
-
getIncompatibleImprovements
public Version getIncompatibleImprovements()
ReturnsConfiguration.getIncompatibleImprovements()from the parentConfiguration. This mostly just exist to satisfy theParserConfigurationinterface.- Specified by:
getIncompatibleImprovementsin interfaceParserConfiguration- Throws:
java.lang.IllegalStateException- If the parent configuration wasn't yet set.
-
-