Package freemarker.core
Class CustomAttribute
java.lang.Object
freemarker.core.CustomAttribute
A class that allows one to associate custom data with a
Configuration
, a Template
, or
Environment
.
This API has similar approach to that of ThreadLocal
(which allows one to associate
custom data with a thread). With an example:
// The object identity itself will serve as the attribute identifier; there's no attribute name String: public static final CustomAttribute MY_ATTR = new CustomAttribute(CustomAttribute.SCOPE_CONFIGURATION); ... // Set the attribute in this particular Configuration object: MY_ATTR.set(myAttrValue, cfg); ... // Read the attribute from this particular Configuration object: myAttrValue = MY_ATTR.get(cfg);
-
Field Summary
Modifier and TypeFieldDescriptionstatic int
Constant used in the constructor specifying that this attribute isConfiguration
-scoped.static int
Constant used in the constructor specifying that this attribute isEnvironment
-scoped.static int
Constant used in the constructor specifying that this attribute isTemplate
-scoped. -
Constructor Summary
ConstructorDescriptionCustomAttribute(int scope)
Creates a new custom attribute with the specified scope -
Method Summary
Modifier and TypeMethodDescriptionprotected Object
create()
This method is invoked whenget()
is invoked withoutset(Object)
being invoked before it to define the value in the current scope.get()
Same asget(Environment)
, but usesEnvironment.getCurrentEnvironment()
to fill the 2nd argument.get(Environment env)
Gets the attribute from the appropriate scope that's accessible through the specifiedEnvironment
.get(TemplateConfiguration templateConfiguration)
Same asget(Template)
, but applies to aTemplateConfiguration
.get(Configuration cfg)
Gets the value of aConfiguration
-scope attribute from the givenConfiguration
.void
Same asset(Object, Environment)
, but usesEnvironment.getCurrentEnvironment()
to fill the 2nd argument.void
set(Object value, Environment env)
Sets the attribute inside the appropriate scope that's accessible through the specifiedEnvironment
.void
set(Object value, TemplateConfiguration templateConfiguration)
Same asset(Object, Template)
, but applicable to aTemplateConfiguration
.void
set(Object value, Configuration cfg)
Sets the value of aConfiguration
-scope attribute in the givenConfiguration
.void
-
Field Details
-
SCOPE_ENVIRONMENT
public static final int SCOPE_ENVIRONMENTConstant used in the constructor specifying that this attribute isEnvironment
-scoped.- See Also:
- Constant Field Values
-
SCOPE_TEMPLATE
public static final int SCOPE_TEMPLATEConstant used in the constructor specifying that this attribute isTemplate
-scoped.- See Also:
- Constant Field Values
-
SCOPE_CONFIGURATION
public static final int SCOPE_CONFIGURATIONConstant used in the constructor specifying that this attribute isConfiguration
-scoped.- See Also:
- Constant Field Values
-
-
Constructor Details
-
CustomAttribute
public CustomAttribute(int scope)Creates a new custom attribute with the specified scope- Parameters:
scope
- one ofSCOPE_
constants.
-
-
Method Details
-
create
This method is invoked whenget()
is invoked withoutset(Object)
being invoked before it to define the value in the current scope. Override it to create the attribute value on-demand.- Returns:
- the initial value for the custom attribute. By default returns null.
-
get
Gets the attribute from the appropriate scope that's accessible through the specifiedEnvironment
. If the attribute hasSCOPE_ENVIRONMENT
scope, it will be get from the givenEnvironment
directly. If the attribute hasSCOPE_TEMPLATE
scope, it will be get from the parent of the givenEnvironment
(that is, inConfigurable.getParent()
) directly). If the attribute hasSCOPE_CONFIGURATION
scope, it will be get fromEnvironment.getConfiguration()
.- Returns:
- The new value of the attribute (possibly
null
), ornull
if the attribute doesn't exist. - Throws:
NullPointerException
- Ifenv
is null- Since:
- 2.3.22
-
get
Same asget(Environment)
, but usesEnvironment.getCurrentEnvironment()
to fill the 2nd argument.- Throws:
IllegalStateException
- If there is no currentEnvironment
, which is usually the case when the current thread isn't processing a template.
-
get
- Throws:
UnsupportedOperationException
- If this custom attribute has different scope thanSCOPE_TEMPLATE
.NullPointerException
- Iftemplate
is null
-
get
Same asget(Template)
, but applies to aTemplateConfiguration
.- Since:
- 2.3.24
-
get
Gets the value of aConfiguration
-scope attribute from the givenConfiguration
.- Throws:
UnsupportedOperationException
- If this custom attribute has different scope thanSCOPE_CONFIGURATION
.NullPointerException
- Ifcfg
is null- Since:
- 2.3.22
-
set
Sets the attribute inside the appropriate scope that's accessible through the specifiedEnvironment
. If the attribute hasSCOPE_ENVIRONMENT
scope, it will be set in the givenEnvironment
directly. If the attribute hasSCOPE_TEMPLATE
scope, it will be set in the parent of the givenEnvironment
(that is, inConfigurable.getParent()
) directly). If the attribute hasSCOPE_CONFIGURATION
scope, it will be set inEnvironment.getConfiguration()
.- Parameters:
value
- The new value of the attribute. Can benull
.- Throws:
NullPointerException
- Ifenv
is null- Since:
- 2.3.22
-
set
Same asset(Object, Environment)
, but usesEnvironment.getCurrentEnvironment()
to fill the 2nd argument.- Throws:
IllegalStateException
- If there is no currentEnvironment
, which is usually the case when the current thread isn't processing a template.
-
set
- Parameters:
value
- The new value of the attribute. Can benull
.- Throws:
UnsupportedOperationException
- If this custom attribute has different scope thanSCOPE_TEMPLATE
.NullPointerException
- Iftemplate
is null
-
set
Same asset(Object, Template)
, but applicable to aTemplateConfiguration
.- Since:
- 2.3.24
-
set
Sets the value of aConfiguration
-scope attribute in the givenConfiguration
.- Parameters:
value
- The new value of the attribute. Can benull
.- Throws:
UnsupportedOperationException
- If this custom attribute has different scope thanSCOPE_CONFIGURATION
.NullPointerException
- Ifcfg
is null- Since:
- 2.3.22
-