info.magnolia.templating.freemarker
Class AbstractDirective<C extends TemplatingElement>

java.lang.Object
  extended by info.magnolia.templating.freemarker.AbstractDirective<C>
Type Parameters:
C - the templating element the directive is operating on
All Implemented Interfaces:
freemarker.template.TemplateDirectiveModel, freemarker.template.TemplateModel
Direct Known Subclasses:
AreaDirective, ComponentDirective, InitDirective, ReadOnlyComponentDirective

public abstract class AbstractDirective<C extends TemplatingElement>
extends Object
implements freemarker.template.TemplateDirectiveModel

A base class for freemarker directives used in Magnolia.

Version:
$Id$

Field Summary
static String CONTENT_ATTRIBUTE
           
static String PATH_ATTRIBUTE
           
static String UUID_ATTRIBUTE
           
static String WORKSPACE_ATTRIBUTE
           
 
Fields inherited from interface freemarker.template.TemplateModel
NOTHING
 
Constructor Summary
AbstractDirective()
           
 
Method Summary
protected
<MT extends freemarker.template.TemplateModel>
MT
_param(Map<String,freemarker.template.TemplateModel> params, String key, Class<MT> type, boolean isMandatory)
           
protected  Boolean bool(Map<String,freemarker.template.TemplateModel> params, String key, Boolean defaultValue)
           
protected  void checkBody(freemarker.template.TemplateDirectiveBody body, boolean needsBody)
          Utility method for directives who need to ensure they're used with or without a body.
protected  info.magnolia.cms.core.Content content(Map<String,freemarker.template.TemplateModel> params, String key, info.magnolia.cms.core.Content defaultValue)
          Deprecated. 
protected  C createTemplatingElement()
           
protected  void doBody(freemarker.core.Environment env, freemarker.template.TemplateDirectiveBody body)
           
 void execute(freemarker.core.Environment env, Map params, freemarker.template.TemplateModel[] loopVars, freemarker.template.TemplateDirectiveBody body)
           
protected  Class<C> getTemplatingElementClass()
           
protected  void initContentElement(Map<String,freemarker.template.TemplateModel> params, AbstractContentTemplatingElement component)
          Init attributes common to all AbstractContentTemplatingElement.
protected  boolean mandatoryBool(Map<String,freemarker.template.TemplateModel> params, String key)
           
protected  info.magnolia.cms.core.Content mandatoryContent(Map<String,freemarker.template.TemplateModel> params, String key)
          Deprecated. 
protected  Object mandatoryObject(Map<String,freemarker.template.TemplateModel> params, String key)
           
protected  String mandatoryString(Map<String,freemarker.template.TemplateModel> params, String key)
           
protected  List<String> mandatoryStringList(Map<String,freemarker.template.TemplateModel> params, String key)
           
protected  javax.jcr.Node node(Map<String,freemarker.template.TemplateModel> params, String key, javax.jcr.Node defaultValue)
           
protected  Object object(Map<String,freemarker.template.TemplateModel> params, String key)
           
protected abstract  void prepareTemplatingElement(C templatingElement, freemarker.core.Environment env, Map<String,freemarker.template.TemplateModel> params, freemarker.template.TemplateModel[] loopVars, freemarker.template.TemplateDirectiveBody body)
          Implementations of this method should prepare the TemplatingElement with the known parameters.
protected  String string(Map<String,freemarker.template.TemplateModel> params, String key, String defaultValue)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PATH_ATTRIBUTE

public static final String PATH_ATTRIBUTE
See Also:
Constant Field Values

UUID_ATTRIBUTE

public static final String UUID_ATTRIBUTE
See Also:
Constant Field Values

WORKSPACE_ATTRIBUTE

public static final String WORKSPACE_ATTRIBUTE
See Also:
Constant Field Values

CONTENT_ATTRIBUTE

public static final String CONTENT_ATTRIBUTE
See Also:
Constant Field Values
Constructor Detail

AbstractDirective

public AbstractDirective()
Method Detail

execute

public void execute(freemarker.core.Environment env,
                    Map params,
                    freemarker.template.TemplateModel[] loopVars,
                    freemarker.template.TemplateDirectiveBody body)
             throws freemarker.template.TemplateException,
                    IOException
Specified by:
execute in interface freemarker.template.TemplateDirectiveModel
Throws:
freemarker.template.TemplateException
IOException

createTemplatingElement

protected C createTemplatingElement()

getTemplatingElementClass

protected Class<C> getTemplatingElementClass()

prepareTemplatingElement

protected abstract void prepareTemplatingElement(C templatingElement,
                                                 freemarker.core.Environment env,
                                                 Map<String,freemarker.template.TemplateModel> params,
                                                 freemarker.template.TemplateModel[] loopVars,
                                                 freemarker.template.TemplateDirectiveBody body)
                                          throws freemarker.template.TemplateModelException,
                                                 IOException
Implementations of this method should prepare the TemplatingElement with the known parameters. If parameters have been grabbed using the methods provided by this class, they should be removed from the map, thus leaving an empty map once the method returns. execute(freemarker.core.Environment, java.util.Map, freemarker.template.TemplateModel[], freemarker.template.TemplateDirectiveBody) will throw a TemplateModelException if there are leftover parameters.

note: The current FreeMarker implementation passes a "new" Map which we can safely manipulate. is thrown away after the execution of the directive. When no parameters are passed, the Map is readonly, but it is otherwise a regular HashMap which has been instantiated shortly before the execution of the directive. However, since this behavior is not mandated by their API, nor documented (at the time of writing, with FreeMarker 2.3.16), we should exert caution. Unit tests hopefully cover this, so we'll be safe when updating to newer FreeMarker versions.

Throws:
freemarker.template.TemplateModelException
IOException

doBody

protected void doBody(freemarker.core.Environment env,
                      freemarker.template.TemplateDirectiveBody body)
               throws freemarker.template.TemplateException,
                      IOException
Throws:
freemarker.template.TemplateException
IOException

checkBody

protected void checkBody(freemarker.template.TemplateDirectiveBody body,
                         boolean needsBody)
                  throws freemarker.template.TemplateModelException
Utility method for directives who need to ensure they're used with or without a body. If the body is *optional*, this method shouldn't be used.

Throws:
freemarker.template.TemplateModelException

mandatoryString

protected String mandatoryString(Map<String,freemarker.template.TemplateModel> params,
                                 String key)
                          throws freemarker.template.TemplateModelException
Throws:
freemarker.template.TemplateModelException

string

protected String string(Map<String,freemarker.template.TemplateModel> params,
                        String key,
                        String defaultValue)
                 throws freemarker.template.TemplateModelException
Throws:
freemarker.template.TemplateModelException

mandatoryBool

protected boolean mandatoryBool(Map<String,freemarker.template.TemplateModel> params,
                                String key)
                         throws freemarker.template.TemplateModelException
Throws:
freemarker.template.TemplateModelException

bool

protected Boolean bool(Map<String,freemarker.template.TemplateModel> params,
                       String key,
                       Boolean defaultValue)
                throws freemarker.template.TemplateModelException
Throws:
freemarker.template.TemplateModelException

mandatoryContent

@Deprecated
protected info.magnolia.cms.core.Content mandatoryContent(Map<String,freemarker.template.TemplateModel> params,
                                                                     String key)
                                                   throws freemarker.template.TemplateModelException
Deprecated. 

Throws:
freemarker.template.TemplateModelException

content

@Deprecated
protected info.magnolia.cms.core.Content content(Map<String,freemarker.template.TemplateModel> params,
                                                            String key,
                                                            info.magnolia.cms.core.Content defaultValue)
                                          throws freemarker.template.TemplateModelException
Deprecated. 

Throws:
freemarker.template.TemplateModelException

node

protected javax.jcr.Node node(Map<String,freemarker.template.TemplateModel> params,
                              String key,
                              javax.jcr.Node defaultValue)
                       throws freemarker.template.TemplateModelException
Throws:
freemarker.template.TemplateModelException

object

protected Object object(Map<String,freemarker.template.TemplateModel> params,
                        String key)
                 throws freemarker.template.TemplateModelException
Throws:
freemarker.template.TemplateModelException

mandatoryObject

protected Object mandatoryObject(Map<String,freemarker.template.TemplateModel> params,
                                 String key)
                          throws freemarker.template.TemplateModelException
Throws:
freemarker.template.TemplateModelException

mandatoryStringList

protected List<String> mandatoryStringList(Map<String,freemarker.template.TemplateModel> params,
                                           String key)
                                    throws freemarker.template.TemplateModelException
Throws:
freemarker.template.TemplateModelException

_param

protected <MT extends freemarker.template.TemplateModel> MT _param(Map<String,freemarker.template.TemplateModel> params,
                                                                   String key,
                                                                   Class<MT> type,
                                                                   boolean isMandatory)
                                                       throws freemarker.template.TemplateModelException
Throws:
freemarker.template.TemplateModelException

initContentElement

protected void initContentElement(Map<String,freemarker.template.TemplateModel> params,
                                  AbstractContentTemplatingElement component)
                           throws freemarker.template.TemplateModelException
Init attributes common to all AbstractContentTemplatingElement.

Throws:
freemarker.template.TemplateModelException


Copyright © 2003-2014 Magnolia International Ltd.. All Rights Reserved.