Package freemarker.core
Class CustomAttribute
- java.lang.Object
-
- freemarker.core.CustomAttribute
-
public class CustomAttribute extends java.lang.Object
A class that allows one to associate custom data with aConfiguration
, aTemplate
, orEnvironment
.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
Fields Modifier and Type Field Description static int
SCOPE_CONFIGURATION
Constant used in the constructor specifying that this attribute isConfiguration
-scoped.static int
SCOPE_ENVIRONMENT
Constant used in the constructor specifying that this attribute isEnvironment
-scoped.static int
SCOPE_TEMPLATE
Constant used in the constructor specifying that this attribute isTemplate
-scoped.
-
Constructor Summary
Constructors Constructor Description CustomAttribute(int scope)
Creates a new custom attribute with the specified scope
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.lang.Object
create()
This method is invoked whenget()
is invoked withoutset(Object)
being invoked before it to define the value in the current scope.java.lang.Object
get()
Same asget(Environment)
, but usesEnvironment.getCurrentEnvironment()
to fill the 2nd argument.java.lang.Object
get(Environment env)
Gets the attribute from the appropriate scope that's accessible through the specifiedEnvironment
.java.lang.Object
get(TemplateConfiguration templateConfiguration)
Same asget(Template)
, but applies to aTemplateConfiguration
.java.lang.Object
get(Configuration cfg)
Gets the value of aConfiguration
-scope attribute from the givenConfiguration
.java.lang.Object
get(Template template)
void
set(java.lang.Object value)
Same asset(Object, Environment)
, but usesEnvironment.getCurrentEnvironment()
to fill the 2nd argument.void
set(java.lang.Object value, Environment env)
Sets the attribute inside the appropriate scope that's accessible through the specifiedEnvironment
.void
set(java.lang.Object value, TemplateConfiguration templateConfiguration)
Same asset(Object, Template)
, but applicable to aTemplateConfiguration
.void
set(java.lang.Object value, Configuration cfg)
Sets the value of aConfiguration
-scope attribute in the givenConfiguration
.void
set(java.lang.Object value, Template template)
-
-
-
Field Detail
-
SCOPE_ENVIRONMENT
public static final int SCOPE_ENVIRONMENT
Constant used in the constructor specifying that this attribute isEnvironment
-scoped.- See Also:
- Constant Field Values
-
SCOPE_TEMPLATE
public static final int SCOPE_TEMPLATE
Constant used in the constructor specifying that this attribute isTemplate
-scoped.- See Also:
- Constant Field Values
-
SCOPE_CONFIGURATION
public static final int SCOPE_CONFIGURATION
Constant used in the constructor specifying that this attribute isConfiguration
-scoped.- See Also:
- Constant Field Values
-
-
Method Detail
-
create
protected java.lang.Object 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
public final java.lang.Object get(Environment env)
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:
java.lang.NullPointerException
- Ifenv
is null- Since:
- 2.3.22
-
get
public final java.lang.Object get()
Same asget(Environment)
, but usesEnvironment.getCurrentEnvironment()
to fill the 2nd argument.- Throws:
java.lang.IllegalStateException
- If there is no currentEnvironment
, which is usually the case when the current thread isn't processing a template.
-
get
public final java.lang.Object get(Template template)
- Throws:
java.lang.UnsupportedOperationException
- If this custom attribute has different scope thanSCOPE_TEMPLATE
.java.lang.NullPointerException
- Iftemplate
is null
-
get
public java.lang.Object get(TemplateConfiguration templateConfiguration)
Same asget(Template)
, but applies to aTemplateConfiguration
.- Since:
- 2.3.24
-
get
public final java.lang.Object get(Configuration cfg)
Gets the value of aConfiguration
-scope attribute from the givenConfiguration
.- Throws:
java.lang.UnsupportedOperationException
- If this custom attribute has different scope thanSCOPE_CONFIGURATION
.java.lang.NullPointerException
- Ifcfg
is null- Since:
- 2.3.22
-
set
public final void set(java.lang.Object value, Environment env)
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:
java.lang.NullPointerException
- Ifenv
is null- Since:
- 2.3.22
-
set
public final void set(java.lang.Object value)
Same asset(Object, Environment)
, but usesEnvironment.getCurrentEnvironment()
to fill the 2nd argument.- Throws:
java.lang.IllegalStateException
- If there is no currentEnvironment
, which is usually the case when the current thread isn't processing a template.
-
set
public final void set(java.lang.Object value, Template template)
- Parameters:
value
- The new value of the attribute. Can benull
.- Throws:
java.lang.UnsupportedOperationException
- If this custom attribute has different scope thanSCOPE_TEMPLATE
.java.lang.NullPointerException
- Iftemplate
is null
-
set
public final void set(java.lang.Object value, TemplateConfiguration templateConfiguration)
Same asset(Object, Template)
, but applicable to aTemplateConfiguration
.- Since:
- 2.3.24
-
set
public final void set(java.lang.Object value, Configuration cfg)
Sets the value of aConfiguration
-scope attribute in the givenConfiguration
.- Parameters:
value
- The new value of the attribute. Can benull
.- Throws:
java.lang.UnsupportedOperationException
- If this custom attribute has different scope thanSCOPE_CONFIGURATION
.java.lang.NullPointerException
- Ifcfg
is null- Since:
- 2.3.22
-
-