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
FieldsModifier and TypeFieldDescriptionstatic intConstant used in the constructor specifying that this attribute isConfiguration-scoped.static intConstant used in the constructor specifying that this attribute isEnvironment-scoped.static intConstant used in the constructor specifying that this attribute isTemplate-scoped. -
Constructor Summary
ConstructorsConstructorDescriptionCustomAttribute(int scope)Creates a new custom attribute with the specified scope -
Method Summary
Modifier and TypeMethodDescriptionprotected Objectcreate()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.voidSame asset(Object, Environment), but usesEnvironment.getCurrentEnvironment()to fill the 2nd argument.voidset(Object value, Environment env)Sets the attribute inside the appropriate scope that's accessible through the specifiedEnvironment.voidset(Object value, TemplateConfiguration templateConfiguration)Same asset(Object, Template), but applicable to aTemplateConfiguration.voidset(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_ENVIRONMENTscope, it will be get from the givenEnvironmentdirectly. If the attribute hasSCOPE_TEMPLATEscope, it will be get from the parent of the givenEnvironment(that is, inConfigurable.getParent()) directly). If the attribute hasSCOPE_CONFIGURATIONscope, it will be get fromEnvironment.getConfiguration().- Returns:
- The new value of the attribute (possibly
null), ornullif the attribute doesn't exist. - Throws:
NullPointerException- Ifenvis 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- Iftemplateis 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- Ifcfgis null- Since:
- 2.3.22
-
set
Sets the attribute inside the appropriate scope that's accessible through the specifiedEnvironment. If the attribute hasSCOPE_ENVIRONMENTscope, it will be set in the givenEnvironmentdirectly. If the attribute hasSCOPE_TEMPLATEscope, it will be set in the parent of the givenEnvironment(that is, inConfigurable.getParent()) directly). If the attribute hasSCOPE_CONFIGURATIONscope, it will be set inEnvironment.getConfiguration().- Parameters:
value- The new value of the attribute. Can benull.- Throws:
NullPointerException- Ifenvis 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- Iftemplateis 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- Ifcfgis null- Since:
- 2.3.22
-