Class Context


  • public class Context
    extends java.lang.Object
    Mustache/Handlebars are contextual template engines. This class represent the 'context stack' of a template.
    • Objects and hashes should be pushed onto the context stack.
    • All elements on the context stack should be accessible.
    • Multiple sections per template should be permitted.
    • Failed context lookups should be considered falsy.
    • Dotted names should be valid for Section tags.
    • Dotted names that cannot be resolved should be considered falsy.
    • Dotted Names - Context Precedence: Dotted names should be resolved against former resolutions.
    Since:
    0.1.0
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Context.Builder
      A context builder.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String CALLEE
      Last callee of a partial block.
      static java.lang.String INLINE_PARTIALS
      Inline partials.
      static java.lang.String INVOCATION_STACK
      The qualified name for partials.
      static java.lang.String PARAM_SIZE
      Number of parameters of a helper.
      static java.lang.String PARTIALS
      The qualified name for partials.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Context combine​(java.lang.String name, java.lang.Object model)
      Insert a new attribute in the context-stack.
      Context combine​(java.util.Map<java.lang.String,​?> model)
      Insert all the attributes in the context-stack.
      static Context copy​(Context context, java.lang.Object model)
      Creates a new context but keep the data attribute.
      <T> T data​(java.lang.String name)
      Read the attribute from the data storage.
      Context data​(java.lang.String name, java.lang.Object value)
      Set an attribute in the data storage.
      Context data​(java.util.Map<java.lang.String,​?> attributes)
      Store the map in the data storage.
      void destroy()
      Destroy this context by cleaning up instance attributes.
      java.lang.Object get​(java.lang.String key)
      Lookup the given key inside the context stack.
      java.lang.Object get​(java.lang.String key, boolean parentScopeResolution)
      Lookup the given key inside the context stack.
      java.lang.Object get​(java.util.List<PathExpression> path)
      Lookup the given key inside the context stack.
      boolean isBlockParams()  
      java.lang.Object model()
      Resolved as '.' or 'this' inside templates.
      static Context newBlockParamContext​(Context parent, java.util.List<java.lang.String> names, java.util.List<java.lang.Object> values)
      Creates a new block param context.
      static Context.Builder newBuilder​(Context parent, java.lang.Object model)
      Start a new context builder.
      static Context.Builder newBuilder​(java.lang.Object model)
      Start a new context builder.
      static Context newContext​(Context parent, java.lang.Object model)
      Creates a new child context.
      static Context newContext​(java.lang.Object model)
      Creates a new root context.
      static Context newPartialContext​(Context ctx, java.lang.String scope, java.util.Map<java.lang.String,​java.lang.Object> hash)
      Creates a new partial context.
      Context parent()
      The parent context or null.
      java.util.Set<java.util.Map.Entry<java.lang.String,​java.lang.Object>> propertySet()
      List all the properties and values of model().
      java.util.Set<java.util.Map.Entry<java.lang.String,​java.lang.Object>> propertySet​(java.lang.Object context)
      List all the properties and values for the given object.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • PARTIALS

        public static final java.lang.String PARTIALS
        The qualified name for partials. Internal use.
      • INLINE_PARTIALS

        public static final java.lang.String INLINE_PARTIALS
        Inline partials.
        See Also:
        Constant Field Values
      • INVOCATION_STACK

        public static final java.lang.String INVOCATION_STACK
        The qualified name for partials. Internal use.
      • PARAM_SIZE

        public static final java.lang.String PARAM_SIZE
        Number of parameters of a helper. Internal use.
      • CALLEE

        public static final java.lang.String CALLEE
        Last callee of a partial block. Internal use.
    • Method Detail

      • combine

        public Context combine​(java.lang.String name,
                               java.lang.Object model)
        Insert a new attribute in the context-stack.
        Parameters:
        name - The attribute's name. Required.
        model - The model data.
        Returns:
        This context.
      • combine

        public Context combine​(java.util.Map<java.lang.String,​?> model)
        Insert all the attributes in the context-stack.
        Parameters:
        model - The model attributes.
        Returns:
        This context.
      • data

        public <T> T data​(java.lang.String name)
        Read the attribute from the data storage.
        Type Parameters:
        T - Data type.
        Parameters:
        name - The attribute's name.
        Returns:
        The attribute value or null.
      • data

        public Context data​(java.lang.String name,
                            java.lang.Object value)
        Set an attribute in the data storage.
        Parameters:
        name - The attribute's name. Required.
        value - The attribute's value. Required.
        Returns:
        This context.
      • data

        public Context data​(java.util.Map<java.lang.String,​?> attributes)
        Store the map in the data storage.
        Parameters:
        attributes - The attributes to add. Required.
        Returns:
        This context.
      • model

        public final java.lang.Object model()
        Resolved as '.' or 'this' inside templates.
        Returns:
        The model or data.
      • parent

        public final Context parent()
        The parent context or null.
        Returns:
        The parent context or null.
      • propertySet

        public java.util.Set<java.util.Map.Entry<java.lang.String,​java.lang.Object>> propertySet​(java.lang.Object context)
        List all the properties and values for the given object.
        Parameters:
        context - The context object.
        Returns:
        All the properties and values for the given object.
      • propertySet

        public java.util.Set<java.util.Map.Entry<java.lang.String,​java.lang.Object>> propertySet()
        List all the properties and values of model().
        Returns:
        All the properties and values of model().
      • isBlockParams

        public boolean isBlockParams()
        Returns:
        True, if this context is a block param context.
      • get

        public java.lang.Object get​(java.util.List<PathExpression> path)
        Lookup the given key inside the context stack.
        • Objects and hashes should be pushed onto the context stack.
        • All elements on the context stack should be accessible.
        • Multiple sections per template should be permitted.
        • Failed context lookups should be considered falsey.
        • Dotted names should be valid for Section tags.
        • Dotted names that cannot be resolved should be considered falsey.
        • Dotted Names - Context Precedence: Dotted names should be resolved against former resolutions.
        Parameters:
        path - The object path.
        Returns:
        The value associated to the given key or null if no value is found.
      • get

        public java.lang.Object get​(java.lang.String key)
        Lookup the given key inside the context stack.
        • Objects and hashes should be pushed onto the context stack.
        • All elements on the context stack should be accessible.
        • Multiple sections per template should be permitted.
        • Failed context lookups should be considered falsey.
        • Dotted names should be valid for Section tags.
        • Dotted names that cannot be resolved should be considered falsey.
        • Dotted Names - Context Precedence: Dotted names should be resolved against former resolutions.
        Parameters:
        key - The object key.
        Returns:
        The value associated to the given key or null if no value is found.
      • get

        public java.lang.Object get​(java.lang.String key,
                                    boolean parentScopeResolution)
        Lookup the given key inside the context stack.
        • Objects and hashes should be pushed onto the context stack.
        • All elements on the context stack should be accessible.
        • Multiple sections per template should be permitted.
        • Failed context lookups should be considered falsey.
        • Dotted names should be valid for Section tags.
        • Dotted names that cannot be resolved should be considered falsey.
        • Dotted Names - Context Precedence: Dotted names should be resolved against former resolutions.
        Parameters:
        key - The object key.
        parentScopeResolution - False, if we want to restrict lookup to current scope.
        Returns:
        The value associated to the given key or null if no value is found.
      • destroy

        public void destroy()
        Destroy this context by cleaning up instance attributes.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • newBuilder

        public static Context.Builder newBuilder​(Context parent,
                                                 java.lang.Object model)
        Start a new context builder.
        Parameters:
        parent - The parent context. Required.
        model - The model data.
        Returns:
        A new context builder.
      • newBuilder

        public static Context.Builder newBuilder​(java.lang.Object model)
        Start a new context builder.
        Parameters:
        model - The model data.
        Returns:
        A new context builder.
      • newContext

        public static Context newContext​(Context parent,
                                         java.lang.Object model)
        Creates a new child context.
        Parameters:
        parent - The parent context. Required.
        model - The model data.
        Returns:
        A new child context.
      • newBlockParamContext

        public static Context newBlockParamContext​(Context parent,
                                                   java.util.List<java.lang.String> names,
                                                   java.util.List<java.lang.Object> values)
        Creates a new block param context.
        Parameters:
        parent - The parent context. Required.
        names - A list of names to set in the block param context.
        values - A list of values to set in the block param context.
        Returns:
        A new block param context.
      • newPartialContext

        public static Context newPartialContext​(Context ctx,
                                                java.lang.String scope,
                                                java.util.Map<java.lang.String,​java.lang.Object> hash)
        Creates a new partial context.
        Parameters:
        ctx - Current scope.
        scope - Scope switch.
        hash - Partial hash.
        Returns:
        A new context.
      • newContext

        public static Context newContext​(java.lang.Object model)
        Creates a new root context.
        Parameters:
        model - The model data.
        Returns:
        A new root context.
      • copy

        public static Context copy​(Context context,
                                   java.lang.Object model)
        Creates a new context but keep the data attribute.
        Parameters:
        context - Context to extract the data attribute.
        model - A model/data.
        Returns:
        A new context.