Package freemarker.core
Class CustomAttribute
- java.lang.Object
-
- freemarker.core.CustomAttribute
-
public class CustomAttribute extends java.lang.ObjectA 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 intSCOPE_CONFIGURATIONConstant used in the constructor specifying that this attribute isConfiguration-scoped.static intSCOPE_ENVIRONMENTConstant used in the constructor specifying that this attribute isEnvironment-scoped.static intSCOPE_TEMPLATEConstant 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.Objectcreate()This method is invoked whenget()is invoked withoutset(Object)being invoked before it to define the value in the current scope.java.lang.Objectget()Same asget(Environment), but usesEnvironment.getCurrentEnvironment()to fill the 2nd argument.java.lang.Objectget(Environment env)Gets the attribute from the appropriate scope that's accessible through the specifiedEnvironment.java.lang.Objectget(TemplateConfiguration templateConfiguration)Same asget(Template), but applies to aTemplateConfiguration.java.lang.Objectget(Configuration cfg)Gets the value of aConfiguration-scope attribute from the givenConfiguration.java.lang.Objectget(Template template)voidset(java.lang.Object value)Same asset(Object, Environment), but usesEnvironment.getCurrentEnvironment()to fill the 2nd argument.voidset(java.lang.Object value, Environment env)Sets the attribute inside the appropriate scope that's accessible through the specifiedEnvironment.voidset(java.lang.Object value, TemplateConfiguration templateConfiguration)Same asset(Object, Template), but applicable to aTemplateConfiguration.voidset(java.lang.Object value, Configuration cfg)Sets the value of aConfiguration-scope attribute in the givenConfiguration.voidset(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_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:
java.lang.NullPointerException- Ifenvis 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- Iftemplateis 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- Ifcfgis 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_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:
java.lang.NullPointerException- Ifenvis 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- Iftemplateis 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- Ifcfgis null- Since:
- 2.3.22
-
-