org.webmacro.directive
Class TypeDirective

java.lang.Object
  extended byorg.webmacro.directive.Directive
      extended byorg.webmacro.directive.TypeDirective
All Implemented Interfaces:
Macro, Visitable

public class TypeDirective
extends Directive

TypeDirective allows the programmer (or template designer) to ensure objects placed into the Context are of the required class type.

Syntax:

    #type [ required ] var-reference quoted-string
 
Examples:
    $MyName, if it exists in the context, must be a java.lang.String
    #type $MyName "java.lang.String"

    $Today must exist in the context and must be a java.util.Date
    #type required $Today "java.util.Date"

    $Addresses, if it exists in the context, must be an org.mycompany.til.Address array
    #type $Addresses "org.mycompany.util.Address[]"
 
TypeDirective simply ensures the class type of the $Variable isAssignableFrom the specified classname.

If the variable is not flagged as being required, then its class type is only checked if it exists in the context. By default, variables are not required to be in the context.

Special support for checking that a variable is an object array exists. Simply append matching square brackets to the end of the classname.

If a Variable is not of the specified type, TypeDirective will throw a org.webmacro.PropertyException.InvalidTypeException, which one can catch in their servlet code if necessary.

TypeDirective is enabled by default, however, it can be disabled via your custom WebMacro.properties file:

    TypeDirective.Enabled = true | false
 

Since:
post 0.97
Version:
1.0
Author:
Eric B. Ridge

Nested Class Summary
 
Nested classes inherited from class org.webmacro.directive.Directive
Directive.ArgDescriptor, Directive.AssignmentArg, Directive.BlockArg, Directive.ConditionArg, Directive.ExactlyOneChoice, Directive.FormalArgListArg, Directive.KeywordArg, Directive.LiteralBlockArg, Directive.LValueArg, Directive.NameArg, Directive.NotSimpleVariableBuildException, Directive.NotVariableBuildException, Directive.OptionalGroup, Directive.OptionalRepeatingSubdirective, Directive.OptionalSubdirective, Directive.OptionChoice, Directive.QuotedStringArg, Directive.RValueArg, Directive.SingleOptionChoice, Directive.StringArg, Directive.Subdirective
 
Field Summary
static Directive.ArgDescriptor[] _args
           
static DirectiveDescriptor _dd
           
static int TYPE_CLASSNAME
           
static int TYPE_OBJECT
           
static int TYPE_REQUIRED
           
 
Fields inherited from class org.webmacro.directive.Directive
ArgType_ARGLIST, ArgType_ASSIGN, ArgType_BLOCK, ArgType_CHOICE, ArgType_CONDITION, ArgType_GROUP, ArgType_KEYWORD, ArgType_LITBLOCK, ArgType_LVALUE, ArgType_NAME, ArgType_QUOTEDSTRING, ArgType_RVALUE, ArgType_STRING, ArgType_SUBDIRECTIVE
 
Constructor Summary
TypeDirective()
           
 
Method Summary
 void accept(TemplateVisitor v)
           
 java.lang.Object build(DirectiveBuilder builder, BuildContext bc)
          configure directive for this run and return 'this'
 java.lang.Object evaluate(Context context)
          Ensure the class of the specified Variable reference isAssignableFrom from the classname arg.
static DirectiveDescriptor getDescriptor()
          static method required by the WebMacro parser to provide a descriptor about this directive
 void write(FastWriter fw, Context context)
          The #type directive does not produce output
 
Methods inherited from class org.webmacro.directive.Directive
getWarningText, writeWarning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TYPE_REQUIRED

public static final int TYPE_REQUIRED
See Also:
Constant Field Values

TYPE_OBJECT

public static final int TYPE_OBJECT
See Also:
Constant Field Values

TYPE_CLASSNAME

public static final int TYPE_CLASSNAME
See Also:
Constant Field Values

_args

public static final Directive.ArgDescriptor[] _args

_dd

public static final DirectiveDescriptor _dd
Constructor Detail

TypeDirective

public TypeDirective()
Method Detail

getDescriptor

public static DirectiveDescriptor getDescriptor()
static method required by the WebMacro parser to provide a descriptor about this directive


build

public java.lang.Object build(DirectiveBuilder builder,
                              BuildContext bc)
                       throws BuildException
configure directive for this run and return 'this'

Specified by:
build in class Directive
Throws:
BuildException

evaluate

public java.lang.Object evaluate(Context context)
                          throws PropertyException
Ensure the class of the specified Variable reference isAssignableFrom from the classname arg.

Specified by:
evaluate in interface Macro
Overrides:
evaluate in class Directive
Returns:
null, always
Throws:
PropertyException.NoSuchVariableException - if the specified variable arg evaluates to null
PropertyException

write

public void write(FastWriter fw,
                  Context context)
           throws java.io.IOException,
                  PropertyException
The #type directive does not produce output

Throws:
PropertyException - if required data was missing from context
java.io.IOException - if we could not successfully write to out

accept

public void accept(TemplateVisitor v)
Specified by:
accept in interface Visitable
Overrides:
accept in class Directive