org.webmacro.directive
Class TypeDirective
java.lang.Object
|
+--org.webmacro.directive.Directive
|
+--org.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
Inner classes inherited from class org.webmacro.directive.Directive |
Directive.ArgDescriptor, Directive.AssignmentArg, Directive.BlockArg, Directive.ConditionArg, Directive.KeywordArg, Directive.LiteralBlockArg, Directive.LValueArg, Directive.NotSimpleVariableBuildException, Directive.NotVariableBuildException, Directive.OptionalGroup, Directive.OptionalRepeatingSubdirective, Directive.OptionalSubdirective, Directive.OptionChoice, Directive.QuotedStringArg, Directive.RValueArg, Directive.SingleOptionChoice, Directive.StringArg, Directive.Subdirective |
Fields inherited from class org.webmacro.directive.Directive |
ArgType_ASSIGN, ArgType_BLOCK, ArgType_CHOICE, ArgType_CONDITION, ArgType_GROUP, ArgType_KEYWORD, ArgType_LITBLOCK, ArgType_LVALUE, ArgType_QUOTEDSTRING, ArgType_RVALUE, ArgType_STRING, ArgType_SUBDIRECTIVE |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
TYPE_REQUIRED
public static final int TYPE_REQUIRED
TYPE_OBJECT
public static final int TYPE_OBJECT
TYPE_CLASSNAME
public static final int TYPE_CLASSNAME
_args
public static final Directive.ArgDescriptor[] _args
_dd
public static final DirectiveDescriptor _dd
TypeDirective
public TypeDirective()
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'
- Overrides:
build
in class Directive
evaluate
public java.lang.Object evaluate(Context context)
throws PropertyException
- Ensure the class of the specified Variable reference
isAssignableFrom
from the classname arg.
- Overrides:
evaluate
in class Directive
- Returns:
null
, always- Throws:
PropertyException.NoSuchVariableException
- if the
specified variable arg evaluates to nullTypeDirective.InvalidTypeException
- if the
specified Variable's class is not assignable from the
specified classname arg
write
public void write(FastWriter fw,
Context context)
throws java.io.IOException,
PropertyException
- The #type directive does not produce output
- Following copied from interface:
org.webmacro.Macro
- Throws:
PropertyException
- if required data was missing from contextjava.io.IOException
- if we could not successfully write to out
accept
public void accept(TemplateVisitor v)
- Overrides:
accept
in class Directive