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 classJinjavaInterpreter.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 voidaddAllChildErrors(String childTemplateName, Collection<TemplateError> childErrors)voidaddAllErrors(Collection<TemplateError> other)Deprecated.voidaddBlock(String name, BlockInfo blockInfo)voidaddError(TemplateError templateError)voidaddExtendParentRoot(Node root)voidendRender(String name)voidendRender(String name, Map<String,Object> data)JinjavaInterpreter.InterpreterScopeClosableenterNonStackingScope()JinjavaInterpreter.InterpreterScopeClosableenterScope()Creates a new variable scope, extending from the current scope.JinjavaInterpreter.InterpreterScopeClosableenterScope(Map<Context.Library,Set<String>> disabled)StringgetAsString(Object object)JinjavaConfiggetConfig()JinjavaConfiggetConfiguration()Deprecated.use {getConfig()}ContextgetContext()static JinjavaInterpretergetCurrent()static Optional<JinjavaInterpreter>getCurrentMaybe()List<TemplateError>getErrors()List<TemplateError>getErrorsCopy()intgetLineNumber()intgetPosition()RandomgetRandom()StringgetResource(String resource)intgetScopeDepth()booleanisValidationMode()voidleaveScope()Nodeparse(String template)static voidpopCurrent()static voidpushCurrent(JinjavaInterpreter interpreter)Stringrender(Node root)Render the given root node, processing extend parents.Stringrender(Node root, boolean processExtendRoots)Render the given root node using this interpreter's current contextStringrender(String template)Parse the given string into a root Node, and then renders it processing extend parents.StringrenderFlat(String template)Parse the given string into a root Node, and then render it without processing any extend parents.ObjectresolveELExpression(String expression, int lineNumber)Resolve expression against current context.ObjectresolveObject(String variable, int lineNumber)ObjectresolveObject(String variable, int lineNumber, int startPosition)Resolve a variable into an object value.ObjectresolveProperty(Object object, String propertyName)Resolve property of bean.ObjectresolveProperty(Object object, List<String> propertyNames)Resolve property of bean.StringresolveResourceLocation(String location)StringresolveString(String variable, int lineNumber)StringresolveString(String variable, int lineNumber, int startPosition)Resolve a variable into a string value.ObjectretraceVariable(String variable, int lineNumber)ObjectretraceVariable(String variable, int lineNumber, int startPosition)Resolve a variable from the interpreter context, returning null if not found.voidsetLineNumber(int lineNumber)voidsetPosition(int position)voidstartRender(String name)StringtoPyishString()Allows for a class to specify a custom string representation in Jinjava.Objectwrap(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:PyishSerializableAllows 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:
toPyishStringin interfacePyishSerializable- Returns:
- A pyish/json string representation of the object
-
-