Package com.hubspot.jinjava.interpret
Class JinjavaInterpreter
- java.lang.Object
-
- com.hubspot.jinjava.interpret.JinjavaInterpreter
-
- All Implemented Interfaces:
PyWrapper
,PyishSerializable
public class JinjavaInterpreter extends Object implements PyishSerializable
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
JinjavaInterpreter.InterpreterScopeClosable
-
Field Summary
-
Fields inherited from interface com.hubspot.jinjava.objects.serialization.PyishSerializable
SELF_WRITER
-
-
Constructor Summary
Constructors Constructor Description JinjavaInterpreter(JinjavaInterpreter orig)
JinjavaInterpreter(Jinjava application, Context context, JinjavaConfig renderConfig)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addAllChildErrors(String childTemplateName, Collection<TemplateError> childErrors)
void
addAllErrors(Collection<TemplateError> other)
Deprecated.void
addBlock(String name, BlockInfo blockInfo)
void
addError(TemplateError templateError)
void
addExtendParentRoot(Node root)
void
endRender(String name)
void
endRender(String name, Map<String,Object> data)
JinjavaInterpreter.InterpreterScopeClosable
enterNonStackingScope()
JinjavaInterpreter.InterpreterScopeClosable
enterScope()
Creates a new variable scope, extending from the current scope.JinjavaInterpreter.InterpreterScopeClosable
enterScope(Map<Context.Library,Set<String>> disabled)
String
getAsString(Object object)
JinjavaConfig
getConfig()
JinjavaConfig
getConfiguration()
Deprecated.use {getConfig()
}Context
getContext()
static JinjavaInterpreter
getCurrent()
static Optional<JinjavaInterpreter>
getCurrentMaybe()
List<TemplateError>
getErrors()
List<TemplateError>
getErrorsCopy()
int
getLineNumber()
int
getPosition()
Random
getRandom()
String
getResource(String resource)
int
getScopeDepth()
boolean
isValidationMode()
void
leaveScope()
Node
parse(String template)
static void
popCurrent()
static void
pushCurrent(JinjavaInterpreter interpreter)
String
render(Node root)
Render the given root node, processing extend parents.String
render(Node root, boolean processExtendRoots)
Render the given root node using this interpreter's current contextString
render(String template)
Parse the given string into a root Node, and then renders it processing extend parents.String
renderFlat(String template)
Parse the given string into a root Node, and then render it without processing any extend parents.Object
resolveELExpression(String expression, int lineNumber)
Resolve expression against current context.Object
resolveObject(String variable, int lineNumber)
Object
resolveObject(String variable, int lineNumber, int startPosition)
Resolve a variable into an object value.Object
resolveProperty(Object object, String propertyName)
Resolve property of bean.Object
resolveProperty(Object object, List<String> propertyNames)
Resolve property of bean.String
resolveResourceLocation(String location)
String
resolveString(String variable, int lineNumber)
String
resolveString(String variable, int lineNumber, int startPosition)
Resolve a variable into a string value.Object
retraceVariable(String variable, int lineNumber)
Object
retraceVariable(String variable, int lineNumber, int startPosition)
Resolve a variable from the interpreter context, returning null if not found.void
setLineNumber(int lineNumber)
void
setPosition(int position)
void
startRender(String name)
String
toPyishString()
Allows for a class to specify a custom string representation in Jinjava.Object
wrap(Object object)
Wrap an object in it's PyIsh equivalent
-
-
-
Constructor Detail
-
JinjavaInterpreter
public JinjavaInterpreter(Jinjava application, Context context, JinjavaConfig renderConfig)
-
JinjavaInterpreter
public JinjavaInterpreter(JinjavaInterpreter orig)
-
-
Method Detail
-
getConfiguration
@Deprecated public JinjavaConfig getConfiguration()
Deprecated.use {getConfig()
}
-
addExtendParentRoot
public void addExtendParentRoot(Node root)
-
enterScope
public JinjavaInterpreter.InterpreterScopeClosable 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
public JinjavaInterpreter.InterpreterScopeClosable enterScope(Map<Context.Library,Set<String>> disabled)
-
enterNonStackingScope
public JinjavaInterpreter.InterpreterScopeClosable enterNonStackingScope()
-
leaveScope
public void leaveScope()
-
getRandom
public Random getRandom()
-
isValidationMode
public boolean isValidationMode()
-
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
-
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(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 using this interpreter's current context- Parameters:
root
- node to renderprocessExtendRoots
- if true, also render all extend parents- Returns:
- rendered result
-
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 contextlineNumber
- current line number, for error reportingstartPosition
- current line position, for error reporting- Returns:
- resolved value for variable
-
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 contextlineNumber
- current line number, for error reportingstartPosition
- current line position, for error reporting- Returns:
- resolved value for variable
-
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 contextlineNumber
- current line number, for error reportingstartPosition
- current line position, for error reporting- Returns:
- resolved value for variable
-
getContext
public Context getContext()
-
getResource
public String getResource(String resource) throws IOException
- Throws:
IOException
-
getConfig
public JinjavaConfig getConfig()
-
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.
-
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)
-
addError
public void addError(TemplateError templateError)
-
getScopeDepth
public int getScopeDepth()
-
addAllErrors
@Deprecated public void addAllErrors(Collection<TemplateError> other)
Deprecated.UseaddAllChildErrors(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)
-
toPyishString
public String toPyishString()
Description copied from interface:PyishSerializable
Allows for a class to specify a custom string representation in Jinjava. By default, this will get a json representation of the object, but this method can be overridden to provide a custom representation. This should use double quotes to wrap json keys/values.- Specified by:
toPyishString
in interfacePyishSerializable
- Returns:
- A pyish/json string representation of the object
-
-