Class RuntimeMacro
- java.lang.Object
-
- org.apache.velocity.runtime.directive.Directive
-
- org.apache.velocity.runtime.directive.RuntimeMacro
-
- All Implemented Interfaces:
java.lang.Cloneable,DirectiveConstants
public class RuntimeMacro extends Directive
This class acts as a proxy for potential macros. When the AST is built this class is inserted as a placeholder for the macro (whether or not the macro is actually defined). At render time we check whether there is a implementation for the macro call. If an implementation cannot be found the literal text is rendered.- Since:
- 1.6
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.StringbadArgsErrorMsgbadArgsErrorMsg will be non null if the arguments to this macro are deamed bad at init time, see the init method.private java.lang.StringliteralLiteral text of the macroprivate java.lang.StringmacroNameName of the macroprivate NodenodeNode of the macro callprotected booleanstrictRefIndicates if we are running in strict reference mode.-
Fields inherited from interface org.apache.velocity.runtime.directive.DirectiveConstants
BLOCK, LINE
-
-
Constructor Summary
Constructors Constructor Description RuntimeMacro(java.lang.String macroName)Create a RuntimeMacro instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.lang.StringgetLiteral()It is probably quite rare that we need to render the macro literal so do it only on-demand and then cache the value.java.lang.StringgetName()Return name of this Velocimacro.java.lang.StringgetScopeName()Override to always return "macro".intgetType()Velocimacros are always LINE type directives.voidinit(RuntimeServices rs, InternalContextAdapter context, Node node)Intialize the Runtime macro.booleanrender(InternalContextAdapter context, java.io.Writer writer, Node node)Velocimacro implementation is not known at the init time.booleanrender(InternalContextAdapter context, java.io.Writer writer, Node node, Renderable body)This method is used with BlockMacro when we want to render a macro with a body AST.-
Methods inherited from class org.apache.velocity.runtime.directive.Directive
getColumn, getLine, getTemplateName, isScopeProvided, makeScope, postRender, preRender, setLocation, setLocation
-
-
-
-
Field Detail
-
macroName
private java.lang.String macroName
Name of the macro
-
literal
private java.lang.String literal
Literal text of the macro
-
node
private Node node
Node of the macro call
-
strictRef
protected boolean strictRef
Indicates if we are running in strict reference mode.
-
badArgsErrorMsg
private java.lang.String badArgsErrorMsg
badArgsErrorMsg will be non null if the arguments to this macro are deamed bad at init time, see the init method. If his is non null, then this macro cannot be rendered, and if there is an attempt to render we throw an exception with this as the message.
-
-
Method Detail
-
getName
public java.lang.String getName()
Return name of this Velocimacro.
-
getScopeName
public java.lang.String getScopeName()
Override to always return "macro". We don't want to use the macro name here, since when writing VTL that uses the scope, we are within a #macro call. The macro name will instead be used as the scope name when defining the body of a BlockMacro.- Overrides:
getScopeNamein classDirective
-
getType
public int getType()
Velocimacros are always LINE type directives.
-
init
public void init(RuntimeServices rs, InternalContextAdapter context, Node node)
Intialize the Runtime macro. At the init time no implementation so we just save the values to use at the render time.
-
getLiteral
private java.lang.String getLiteral()
It is probably quite rare that we need to render the macro literal so do it only on-demand and then cache the value. This tactic helps to reduce memory usage a bit.
-
render
public boolean render(InternalContextAdapter context, java.io.Writer writer, Node node) throws java.io.IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException
Velocimacro implementation is not known at the init time. So look for a implementation in the macro libaries and if finds one renders it. The actual rendering is delegated to the VelocimacroProxy object. When looking for a macro we first loot at the template with has the macro call then we look at the macro lbraries in the order they appear in the list. If a macro has many definitions above look up will determine the precedence.- Specified by:
renderin classDirective- Parameters:
context-writer-node-- Returns:
- true if the rendering is successful
- Throws:
java.io.IOExceptionResourceNotFoundExceptionParseErrorExceptionMethodInvocationException
-
render
public boolean render(InternalContextAdapter context, java.io.Writer writer, Node node, Renderable body) throws java.io.IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException
This method is used with BlockMacro when we want to render a macro with a body AST.- Parameters:
context-writer-node-body- AST block that was enclosed in the macro body.- Returns:
- true if the rendering is successful
- Throws:
java.io.IOExceptionResourceNotFoundExceptionParseErrorExceptionMethodInvocationException
-
-