Class CustomAttribute

java.lang.Object
freemarker.core.CustomAttribute

public class CustomAttribute extends Object
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

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Constant used in the constructor specifying that this attribute is scoped by the configuration.
    static final int
    Constant used in the constructor specifying that this attribute is scoped by the environment.
    static final int
    Constant used in the constructor specifying that this attribute is scoped by the template.
  • Constructor Summary

    Constructors
    Constructor
    Description
    CustomAttribute(int scope)
    Creates a new custom attribute with the specified scope
  • Method Summary

    Modifier and Type
    Method
    Description
    protected Object
    This method is invoked when get() is invoked without set(Object) being invoked before it to define the value in the current scope.
    final Object
    get()
     
    final Object
     
    final void
    set(Object value)
    Sets the value of the attribute in the context of the current environment.
    final void
    set(Object value, Template t)
    Sets the value of a template-scope attribute in the context of the given template.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • SCOPE_ENVIRONMENT

      public static final int SCOPE_ENVIRONMENT
      Constant used in the constructor specifying that this attribute is scoped by the environment.
      See Also:
    • SCOPE_TEMPLATE

      public static final int SCOPE_TEMPLATE
      Constant used in the constructor specifying that this attribute is scoped by the template.
      See Also:
    • SCOPE_CONFIGURATION

      public static final int SCOPE_CONFIGURATION
      Constant 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

      protected Object create()
      This method is invoked when get() is invoked without set(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 Object 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

      public final Object get(Template t)
      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 attribute
      NullPointerException - if t is null
    • set

      public final void set(Object value)
      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

      public final void set(Object value, Template t)
      Sets the value of a template-scope attribute in the context of the given template.
      Parameters:
      value - the new value of the attribute
      t - the template
      Throws:
      UnsupportedOperationException - if this custom attribute is not a template-scope attribute
      NullPointerException - if t is null