CyLog Software Homepage BeanMaker v2.10
Copyright 2000-2006 CyLog Software
Introduction
Quick Guide
How the engine works
Writing Templates
   Standard Tags
   Property Loop Tags
   Switch Tags
   Include File Tag
   Regular Expression Tag
   String Function Tags
   Numeric Function Tags
   
Tutorials
Registration
License Agreement

Writing Templates - Switch Tags

Switch tags, a new BeanMaker feature, let you define some conditional functionality in your templates. A switch definition defines a switch domain, i.e. a collection of key-value pairs. Each switch domain can have an optional default value which will be used in place of an unknown key. Switches are mostly usefull when you want to generate different code based on property types.

Switch Domain definition

$switchdef('domain')$ start the domain definition (all lines until the closing $switchdef$ tag are removed
    key = valueKey value pairs
    ......
    [$default$] = default valueDefault value for unknown keys(optional)
$switchdef$end of the domain definition

Switch tag - (Using the domain definitions)

An switch tag is the means of retrieving the values defined in a switch domain definition as described above. It's syntax is shown below:

$switch('domain', 'key')$

The tag searches the definition of the key in the domain definition key-value list and generates a result. If the key is found then it's value is used. If the key is not found then either the default value (if specified) or an error code like the one shown below will be used:

SWITCH[domain]_DEFAULT_VALUE_UNDEFINED

Examples 1. Simple definition

$switchdef('NUMBERS')$
   one=1
   two=2
   three=3
   $default$=UNKNOWN_NUMBER
$switchdef$

$switch('NUMBERS','one')$
$switch('NUMBERS','two')$
$switch('NUMBERS','fourteen')$

...produces this output:

1
2
UNKNOWN_NUMBER

Examples 2. Real-world Situation
Convert a java primitive to an Object.

The objective here is to add all of a JavaBean's properties to a List. Java 1.4.2 and previous versions do not support automatic boxing, so we must convert the primitive member variables to Objects. Based on the following Bean Property definition...

int          an_integer
String       a_string
boolean      a_boolean
CustomObject a_customobject

...and using the following BeanMaker template...

$switchdef('OBJECT')$
   int           = new Integer($property$)
   boolean       = new Boolean($property$)
   long          = new Long($property$)
   $default$     = $property$
$switchdef$

   List list = new ArrayList();
$doproperty$
   list.add($switch('OBJECT','$propertytype$')$);
$loop$

...we can produce this output:

   List list = new ArrayList();
   list.add(new Integer(an_integer));
   list.add(a_string);
   list.add(new Boolean(a_boolean));
   list.add(a_customobject);
Copyright 2000-2006 CyLog Software    www.cylog.org