Class JinjavaInterpreter

java.lang.Object
com.hubspot.jinjava.interpret.JinjavaInterpreter
All Implemented Interfaces:
PyWrapper, PyishSerializable

public class JinjavaInterpreter extends Object implements PyishSerializable
  • Field Details

  • Constructor Details

  • Method Details

    • checkOutputSize

      public static void checkOutputSize(String string)
    • isOutputTooLarge

      public static boolean isOutputTooLarge(String string)
    • getConfiguration

      @Deprecated public JinjavaConfig getConfiguration()
      Deprecated.
    • addExtendParentRoot

      public void addExtendParentRoot(Node root)
    • addBlock

      public void addBlock(String name, BlockInfo blockInfo)
    • enterScope

      Creates a new variable scope, extending from the current scope. Allows you to create a nested contextual scope which can override variables from higher levels.

      Should be used in a try/finally context, similar to lock-use patterns:

      interpreter.enterScope(); try (interpreter.enterScope()) { // ... }

    • enterScope

    • enterNonStackingScope

      public JinjavaInterpreter.InterpreterScopeClosable enterNonStackingScope()
    • leaveScope

      public void leaveScope()
    • getRandom

      public Random getRandom()
    • isValidationMode

      public boolean isValidationMode()
    • getRevertibleObjects

      public Map<String,RevertibleObject> getRevertibleObjects()
    • parse

      public Node parse(String template)
    • renderFlat

      public String renderFlat(String template)
      Parse the given string into a root Node, and then render it without processing any extend parents. This method should be used when the template is known to not have any extends or block tags.
      Parameters:
      template - string to parse
      Returns:
      rendered result
    • renderFlat

      public String renderFlat(String template, long renderLimit)
      Parse the given string into a root Node, and then render it without processing any extend parents. This method should be used when the template is known to not have any extends or block tags.
      Parameters:
      template - string to parse
      renderLimit - stop rendering once this output length is reached
      Returns:
      rendered result
    • render

      public String render(String template)
      Parse the given string into a root Node, and then renders it processing extend parents.
      Parameters:
      template - string to parse
      Returns:
      rendered result
    • render

      public String render(String template, long renderLimit)
      Parse the given string into a root Node, and then renders it processing extend parents.
      Parameters:
      template - string to parse
      renderLimit - stop rendering once this output length is reached
      Returns:
      rendered result
    • render

      public String render(Node root)
      Render the given root node, processing extend parents. Equivalent to render(root, true)
      Parameters:
      root - node to render
      Returns:
      rendered result
    • render

      public String render(Node root, boolean processExtendRoots)
      Render the given root node with an option to process extend parents. Equivalent to render(root, processExtendRoots).
      Parameters:
      root - node to render
      processExtendRoots -
      Returns:
    • retraceVariable

      public Object retraceVariable(String variable, int lineNumber, int startPosition)
      Resolve a variable from the interpreter context, returning null if not found. This method updates the template error accumulators when a variable is not found.
      Parameters:
      variable - name of variable in context
      lineNumber - current line number, for error reporting
      startPosition - current line position, for error reporting
      Returns:
      resolved value for variable
    • retraceVariable

      public Object retraceVariable(String variable, int lineNumber)
    • resolveObject

      public Object resolveObject(String variable, int lineNumber, int startPosition)
      Resolve a variable into an object value. If given a string literal (e.g. 'foo' or "foo"), this method returns the literal unquoted. If the variable is undefined in the context, this method returns the given variable string.
      Parameters:
      variable - name of variable in context
      lineNumber - current line number, for error reporting
      startPosition - current line position, for error reporting
      Returns:
      resolved value for variable
    • resolveObject

      public Object resolveObject(String variable, int lineNumber)
    • resolveString

      public String resolveString(String variable, int lineNumber, int startPosition)
      Resolve a variable into a string value. If given a string literal (e.g. 'foo' or "foo"), this method returns the literal unquoted. If the variable is undefined in the context, this method returns the given variable string.
      Parameters:
      variable - name of variable in context
      lineNumber - current line number, for error reporting
      startPosition - current line position, for error reporting
      Returns:
      resolved value for variable
    • getAsString

      public String getAsString(Object object)
    • resolveString

      public String resolveString(String variable, int lineNumber)
    • getContext

      public Context getContext()
    • resolveResourceLocation

      public String resolveResourceLocation(String location)
    • getResource

      public String getResource(String resource) throws IOException
      Throws:
      IOException
    • getConfig

      public JinjavaConfig getConfig()
    • resolveELExpressionSilently

      public Object resolveELExpressionSilently(String expression)
      Resolve expression against current context, but does not add the expression to the set of resolved expressions.
      Parameters:
      expression - Jinja expression.
      Returns:
      Value of expression.
    • resolveELExpression

      public Object resolveELExpression(String expression, int lineNumber)
      Resolve expression against current context.
      Parameters:
      expression - Jinja expression.
      lineNumber - Line number of expression.
      Returns:
      Value of expression.
    • resolveELExpression

      public Object resolveELExpression(String expression, int lineNumber, int position)
      Resolve expression against current context. Also set the interpreter's position, useful for nodes that resolve multiple expressions such as a node using an IfTag and ElseTags.
      Parameters:
      expression - Jinja expression.
      lineNumber - Line number of expression.
      position - Start position of expression.
      Returns:
      Value of expression.
    • resolveProperty

      public Object resolveProperty(Object object, String propertyName)
      Resolve property of bean.
      Parameters:
      object - Bean.
      propertyName - Name of property to resolve.
      Returns:
      Value of property.
    • resolveProperty

      public Object resolveProperty(Object object, List<String> propertyNames)
      Resolve property of bean.
      Parameters:
      object - Bean.
      propertyNames - Names of properties to resolve recursively.
      Returns:
      Value of property.
    • wrap

      public Object wrap(Object object)
      Wrap an object in it's PyIsh equivalent
      Parameters:
      object - Bean.
      Returns:
      Wrapped bean.
    • getLineNumber

      public int getLineNumber()
    • setLineNumber

      public void setLineNumber(int lineNumber)
    • getPosition

      public int getPosition()
    • setPosition

      public void setPosition(int position)
    • getCurrentBlock

      public BlockInfo getCurrentBlock()
    • addError

      public void addError(TemplateError templateError)
    • removeLastError

      public void removeLastError()
    • getLastError

      public Optional<TemplateError> getLastError()
    • getScopeDepth

      public int getScopeDepth()
    • addAllErrors

      @Deprecated public void addAllErrors(Collection<TemplateError> other)
      Deprecated.
      Use addAllChildErrors(String, Collection) instead to fix error line numbers
    • addAllChildErrors

      public void addAllChildErrors(String childTemplateName, Collection<TemplateError> childErrors)
    • getErrors

      public List<TemplateError> getErrors()
    • getErrorsCopy

      public List<TemplateError> getErrorsCopy()
    • getCurrent

      public static JinjavaInterpreter getCurrent()
    • getCurrentMaybe

      public static Optional<JinjavaInterpreter> getCurrentMaybe()
    • pushCurrent

      public static void pushCurrent(JinjavaInterpreter interpreter)
    • popCurrent

      public static void popCurrent()
    • startRender

      public void startRender(String name)
    • endRender

      public void endRender(String name)
    • endRender

      public void endRender(String name, Map<String,Object> data)
    • appendPyishString

      public <T extends Appendable & CharSequence> T appendPyishString(T appendable) throws IOException
      Description copied from interface: PyishSerializable
      Allows for a class to append the custom string representation in Jinjava. This method will be used by PyishSerializable.writePyishSelf(JsonGenerator, SerializerProvider) to specify what will be written to the json generator.

      Specified by:
      appendPyishString in interface PyishSerializable
      Parameters:
      appendable - Appendable to append the pyish string representation to.
      Returns:
      The same appendable with an appended result
      Throws:
      IOException