Class EagerReconstructionUtils

java.lang.Object
com.hubspot.jinjava.util.EagerReconstructionUtils

@Beta public class EagerReconstructionUtils extends Object
  • Constructor Details

    • EagerReconstructionUtils

      public EagerReconstructionUtils()
  • Method Details

    • executeInChildContext

      @Deprecated public static EagerExecutionResult executeInChildContext(Function<JinjavaInterpreter,EagerExpressionResolver.EagerExpressionResult> function, JinjavaInterpreter interpreter, boolean takeNewValue, boolean partialMacroEvaluation, boolean checkForContextChanges)
      Deprecated.
      Use EagerContextWatcher.executeInChildContext(Function, JinjavaInterpreter, EagerChildContextConfig) Execute the specified functions within a protected context. Additionally, if the execution causes existing values on the context to become deferred, then their previous values will wrapped in a set tag that gets prepended to the returned result. The function is run in deferredExecutionMode=true, where the context needs to be protected from having values updated or set, such as when evaluating both the positive and negative nodes in an if statement.
      Parameters:
      function - Function to run within a "protected" child context
      interpreter - JinjavaInterpreter to create a child from.
      takeNewValue - If a value is updated (not replaced) either take the new value or take the previous value and put it into the EagerExecutionResult.prefixToPreserveState.
      partialMacroEvaluation - Allow macro functions to be partially evaluated rather than needing an explicit result during this render.
      checkForContextChanges - Set this to be true if executing function could cause changes to the context. Otherwise, a false value will speed up execution.
      Returns:
      An EagerExecutionResult where: result is the string result of function. prefixToPreserveState is either blank or a set tag that preserves the state within the output for a second rendering pass.
    • executeInChildContext

    • reconstructFromContextBeforeDeferring

      @Deprecated public static String reconstructFromContextBeforeDeferring(Set<String> deferredWords, JinjavaInterpreter interpreter)
      Reconstruct the macro functions and variables from the context before they get deferred. Those macro functions and variables found within deferredWords are reconstructed with MacroTag(s) and SetTag(s), respectively to preserve the context within the Jinjava template itself.
      Parameters:
      deferredWords - set of words that will need to be deferred based on the previously performed operation.
      interpreter - the Jinjava interpreter.
      Returns:
      a Jinjava-syntax string of 0 or more macro tags and 0 or more set tags.
    • reconstructFromContextBeforeDeferringAsMap

      @Deprecated public static PrefixToPreserveState reconstructFromContextBeforeDeferringAsMap(Set<String> deferredWords, JinjavaInterpreter interpreter)
      Reconstruct the macro functions and variables from the context before they get deferred. Those macro functions and variables found within deferredWords are reconstructed with MacroTag(s) and SetTag(s), respectively to preserve the context within the Jinjava template itself.
      Parameters:
      deferredWords - set of words that will need to be deferred based on the previously performed operation.
      interpreter - the Jinjava interpreter.
      Returns:
      a PrefixToPreserveState map of 0 or more macro tags and 0 or more set tags.
    • hydrateReconstructionFromContextBeforeDeferring

      public static PrefixToPreserveState hydrateReconstructionFromContextBeforeDeferring(PrefixToPreserveState prefixToPreserveState, Set<String> deferredWords, JinjavaInterpreter interpreter)
      Reconstruct the macro functions and variables from the context before they get deferred. Those macro functions and variables found within deferredWords are reconstructed with MacroTag(s) and SetTag(s), respectively to preserve the context within the Jinjava template itself.
      Parameters:
      prefixToPreserveState - This PrefixToPreserveState will be hydrated with the Macro tag images and set tag images
      deferredWords - set of words that will need to be deferred based on the previously performed operation.
      interpreter - the Jinjava interpreter.
      Returns:
      The PrefixToPreserveState to allow method chaining
    • buildBlockOrInlineSetTag

      public static String buildBlockOrInlineSetTag(String name, Object value, JinjavaInterpreter interpreter)
    • buildBlockOrInlineSetTagAndRegisterDeferredToken

      public static String buildBlockOrInlineSetTagAndRegisterDeferredToken(String name, Object value, JinjavaInterpreter interpreter)
    • hydrateBlockOrInlineSetTagRecursively

      public static PrefixToPreserveState hydrateBlockOrInlineSetTagRecursively(PrefixToPreserveState prefixToPreserveState, String name, Object value, JinjavaInterpreter interpreter)
    • buildBlockOrInlineSetTag

      public static String buildBlockOrInlineSetTag(String name, Object value, JinjavaInterpreter interpreter, boolean registerDeferredToken)
    • buildSetTag

      public static String buildSetTag(Map<String,String> deferredValuesToSet, JinjavaInterpreter interpreter, boolean registerDeferredToken)
      Build the image for a SetTag which preserves the values of objects on the context for a later rendering pass. The set tag will set the keys to the values within the deferredValuesToSet Map.
      Parameters:
      deferredValuesToSet - Map that specifies what the context objects should be set to in the returned image.
      interpreter - The Jinjava interpreter.
      registerDeferredToken - Whether or not to register the returned SetTag image as an DeferredToken.
      Returns:
      A jinjava-syntax string that is the image of a set tag that will be executed at a later time.
    • buildBlockSetTag

      public static String buildBlockSetTag(String name, String value, JinjavaInterpreter interpreter, boolean registerDeferredToken)
      Build the image for a block SetTag and body to preserve the values of an object on the context for a later rendering pass.
      Parameters:
      name - The name of the variable to set.
      value - The string value, potentially containing jinja code to put in the set tag block.
      interpreter - The Jinjava interpreter.
      registerDeferredToken - Whether to register the returned SetTag token as an DeferredToken.
      Returns:
      A jinjava-syntax string that is the image of a block set tag that will be executed at a later time.
    • buildDoUpdateTag

      public static String buildDoUpdateTag(String name, String updateString, JinjavaInterpreter interpreter)
    • reconstructEnd

      public static String reconstructEnd(TagNode tagNode)
    • wrapInRawIfNeeded

      public static String wrapInRawIfNeeded(String output, JinjavaInterpreter interpreter)
    • wrapInAutoEscapeIfNeeded

      public static String wrapInAutoEscapeIfNeeded(String output, JinjavaInterpreter interpreter)
    • wrapInTag

      public static String wrapInTag(String body, String tagNameToWrap, JinjavaInterpreter interpreter, boolean registerDeferredToken)
      Wrap the string output in a specified block-type tag.
      Parameters:
      body - The string body to wrap.
      tagNameToWrap - The name of the tag which will wrap around the .
      interpreter - The Jinjava interpreter.
      registerDeferredToken - Whether to register the returned Tag token as an DeferredToken.
      Returns:
      A jinjava-syntax string that is the image of a block set tag that will be executed at a later time.
    • labelWithNotes

      public static String labelWithNotes(String body, String noteIdentifier, JinjavaInterpreter interpreter)
      Surround the with notes to provide identifying information on what is. If is foo and is {{ bar }}, the result will be:

      {# foo #}{{ bar }}{# endfoo #}

      Parameters:
      body - The string body to wrap.
      noteIdentifier - The identifier for the note.
      interpreter - The Jinjava interpreter.
      Returns:
      A block surrounded with labelled notes
    • getStartLabel

      public static String getStartLabel(String noteIdentifier, TokenScannerSymbols symbols)
    • getEndLabel

      public static String getEndLabel(String noteIdentifier, TokenScannerSymbols symbols)
    • wrapInChildScope

      public static String wrapInChildScope(String toWrap, JinjavaInterpreter interpreter)
    • isDeferredExecutionMode

      public static Boolean isDeferredExecutionMode()
    • deferWordsAndReconstructReferences

      public static PrefixToPreserveState deferWordsAndReconstructReferences(JinjavaInterpreter interpreter, Set<String> wordsToDefer)
    • handleDeferredTokenAndReconstructReferences

      public static Map<String,String> handleDeferredTokenAndReconstructReferences(JinjavaInterpreter interpreter, DeferredToken deferredToken)
    • reconstructDeferredReferences

      public static Map<String,String> reconstructDeferredReferences(JinjavaInterpreter interpreter, Set<String> usedDeferredWords)
    • resetAndDeferSpeculativeBindings

      public static PrefixToPreserveState resetAndDeferSpeculativeBindings(JinjavaInterpreter interpreter, EagerExecutionResult eagerExecutionResult)
      Reset variables to what they were before running the latest execution represented by . Then re-defer those variables and reconstruct deferred lazy references to them. This method is needed in 2 circumstances:

      * When doing some eager execution and then needing to repeat the same execution in deferred execution mode.

      * When rendering logic which takes place in its own child scope (for tag, macro function, set block) and there are speculative bindings. These must be deferred and the execution must run again, so they don't get reconstructed within the child scope, and can instead be reconstructed in their original scopes.

      Parameters:
      interpreter - The JinjavaInterpreter
      eagerExecutionResult - The execution result which contains information about which bindings were modified during the execution.
      Returns:
    • resetSpeculativeBindings

      public static Set<String> resetSpeculativeBindings(JinjavaInterpreter interpreter, EagerExecutionResult result)
    • commitSpeculativeBindings

      public static void commitSpeculativeBindings(JinjavaInterpreter interpreter, EagerExecutionResult result)
    • reconstructPathAroundBlock

      public static void reconstructPathAroundBlock(DynamicRenderedOutputNode prefix, OutputList blockValueBuilder, JinjavaInterpreter interpreter)
    • wrapPathAroundText

      public static String wrapPathAroundText(String text, String newPath, JinjavaInterpreter interpreter)