org.apache.velocity.runtime.directive
Class RuntimeMacro

java.lang.Object
  extended by org.apache.velocity.runtime.directive.Directive
      extended by org.apache.velocity.runtime.directive.RuntimeMacro
All Implemented Interfaces:
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
protected  boolean strictRef
          Indicates if we are running in strict reference mode.
 
Fields inherited from class org.apache.velocity.runtime.directive.Directive
rsvc
 
Fields inherited from interface org.apache.velocity.runtime.directive.DirectiveConstants
BLOCK, LINE
 
Constructor Summary
RuntimeMacro(String macroName)
          Create a RuntimeMacro instance.
 
Method Summary
 String getName()
          Return name of this Velocimacro.
 String getScopeName()
          Override to always return "macro".
 int getType()
          Velocimacros are always LINE type directives.
 void init(RuntimeServices rs, InternalContextAdapter context, Node node)
          Intialize the Runtime macro.
 boolean render(InternalContextAdapter context, Writer writer, Node node)
          Velocimacro implementation is not known at the init time.
 boolean render(InternalContextAdapter context, 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
checkArgs, getColumn, getLine, getTemplateName, isScopeProvided, makeScope, postRender, preRender, setLocation, setLocation
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

strictRef

protected boolean strictRef
Indicates if we are running in strict reference mode.

Constructor Detail

RuntimeMacro

public RuntimeMacro(String macroName)
Create a RuntimeMacro instance. Macro name and source template stored for later use.

Parameters:
macroName - name of the macro
Method Detail

getName

public String getName()
Return name of this Velocimacro.

Specified by:
getName in class Directive
Returns:
The name of this Velocimacro.

getScopeName

public 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:
getScopeName in class Directive
Returns:
the name to be used when a scope control is provided for this directive.

getType

public int getType()
Velocimacros are always LINE type directives.

Specified by:
getType in class Directive
Returns:
The type of this directive.

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.

Overrides:
init in class Directive
Parameters:
rs - runtime services
context - InternalContextAdapter
node - node containing the macro call

render

public boolean render(InternalContextAdapter context,
                      Writer writer,
                      Node node)
               throws 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:
render in class Directive
Parameters:
context -
writer -
node -
Returns:
true if the rendering is successful
Throws:
IOException
ResourceNotFoundException
ParseErrorException
MethodInvocationException

render

public boolean render(InternalContextAdapter context,
                      Writer writer,
                      Node node,
                      Renderable body)
               throws 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:
IOException
ResourceNotFoundException
ParseErrorException
MethodInvocationException


Copyright © 2000-2012 The Apache Software Foundation. All Rights Reserved.