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. It works pretty much like
ThreadLocal, a
class that allows one to associate custom data with a thread.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intConstant used in the constructor specifying that this attribute is scoped by the configuration.static final intConstant used in the constructor specifying that this attribute is scoped by the environment.static final intConstant used in the constructor specifying that this attribute is scoped by the template. -
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.final Objectget()final Objectfinal voidSets the value of the attribute in the context of the current environment.final voidSets the value of a template-scope attribute in the context of the given template.
-
Field Details
-
SCOPE_ENVIRONMENT
public static final int SCOPE_ENVIRONMENTConstant used in the constructor specifying that this attribute is scoped by the environment.- See Also:
-
SCOPE_TEMPLATE
public static final int SCOPE_TEMPLATEConstant used in the constructor specifying that this attribute is scoped by the template.- See Also:
-
SCOPE_CONFIGURATION
public static final int SCOPE_CONFIGURATIONConstant used in the constructor specifying that this attribute is scoped by the configuration.- See Also:
-
-
Constructor Details
-
CustomAttribute
public CustomAttribute(int scope) Creates a new custom attribute with the specified scope- Parameters:
scope- one of SCOPE_ 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
- Returns:
- the value of the attribute in the context of the current environment.
- Throws:
IllegalStateException- if there is no current environment (and hence also no current template and configuration), therefore the attribute's current scope object can't be resolved.
-
get
- Returns:
- the value of a template-scope attribute in the context of a given template.
- Throws:
UnsupportedOperationException- if this custom attribute is not a template-scope attributeNullPointerException- if t is null
-
set
Sets the value of the attribute in the context of the current environment.- Parameters:
value- the new value of the attribute- Throws:
IllegalStateException- if there is no current environment (and hence also no current template and configuration), therefore the attribute's current scope object can't be resolved.
-
set
Sets the value of a template-scope attribute in the context of the given template.- Parameters:
value- the new value of the attributet- the template- Throws:
UnsupportedOperationException- if this custom attribute is not a template-scope attributeNullPointerException- if t is null
-