|
![]() |
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectsunlabs.brazil.template.Template
sunlabs.brazil.template.BSLTemplate (view source)
public class BSLTemplate
The BSLTemplate
takes an HTML document with embedded "BSL"
markup tags in it and evaluates those special tags to produce a
standard HTML document.
BSL stands for Brazil Scripting Language. BSL can be used to substitute
data from the request properties into the resultant document. However,
rather than simple property substitution as is provided by the
SetTemplate
, this class provides the ability to iterate
over and choose amongst the values substituted with a set of simple
flow-control constructs.
BSL uses the following special tags as its language constructs:
<if>
<foreach>
<abort>
<break>
<continue>
<extract>
This template recursively evalutes the bodies/clauses of the BSL commands, meaning that they may contain nested BSL and/or other tags defined by other templates.
The following configuration parameter is used to initialize this template.
debug
<if>
tag evaluates one of its clauses dependant
upon the value of the provided conditions. The other clauses are not
evaluated and do not appear in the resultant HTML document.
The general format of the <if>
tag is as follows:
<if [not] condition> clause <elseif [not] condition> clause <else> clause </if>The
<elseif>
and <else>
tags are
optional, and multiple <elseif>
tags may be present.
<elseif>
may also be spelled <elif>
or <else if>
. The optional parameter not
reverses the sense of the specified condition.
Following are the formats of the condition:
<if name=var>
<if name=var value=string>
<if name=pattern any>
glob
pattern.
Note: This may be expensive if there are large numbers of
properties.
<if name=var glob=pattern>
glob
pattern.
<if name=var match=pattern>
regular expression
pattern.
if the attribute nocase
is present, then a case
insensitive match is performed.
<if expr=numeric expression>
Calculator
to evaluate the expression.
Any vaiable that is defined, but not "0"m "off" or "no" is considered
to have a value of "1" for the purposes of the expression evaluation.
This allows (as an example) the expression
"x && y && ! z"
to evaluate to "true" only if the variables "x" and "y", but not
"z" are defined *(and not "0", "no" or "false".
<foreach>
tag repeatedly evaluates its body a
selected number of times. Each time the body is evaluated, the provided
named property is set to the next word in the provided list of words.
The body is terminated by the </foreach>
tag.
This tag is especially useful for dynamically producing lists and tables.
<foreach name=var list="value1 value2 ..." [delim="chars index=i"]> <get var> body </foreach>Iterate over the set of values "value1 value2 ...". The named property var is assigned each value in turn.
If the optional parameter delim
specifies, the
delimiter for splitting the list into elements.
delim
is
a single character, then that character is used as the delimiter.
delim
is not
specified or is the empty string "", the delimiter is whitespace.
index
is supplied, then at each
iteration, the value "name.count" counts the items, starting with the
supplied index value (or 1).
<foreach name=var property=property [delim="chars" index="i"]> <get var> body </foreach>Iterate over the values in the other property. The value of the other property is broken into elements and each element is assigned to the named property var in turn. This form is equivalent to
<foreach name=var list=${property}>
.
If the optional parameter delim
is specified, the characters
are delimiters for splitting the list into elements using the
StringTokenizer
rules. If delim
is not
specified or is the empty string "", the delimiter is whitespace.
<foreach name=var glob=pattern [index=i]> <get var.name> <get var.value> <get var.name.1> <get var.name.2> body </foreach>Iterate over all the properties whose name matches the
glob
pattern. In turn, the
following properties are set:
var.name
is the name of the property.
var.value
is the value of the property.
var.name.1
, var.name.2
, ...
are the substrings matching the wildcard characters in the pattern, if any.
<foreach name=var match=pattern [index=i]> <get var.name> <get var.value> <get var.name.0> <get var.name.1> <get var.name.2> body </foreach>Iterate over all the properties whose name matches the
regular expression
pattern. In turn, the following properties are set:
var.name
is the name of the property.
var.value
is the value of the property.
var.name.0
is the substring that matched the whole
pattern.
var.name.1
, var.name.2
, ...
are the substrings matching the parenthesized subexpressions, if any.
index
is supplied, then
var.count
counts the items, starting with the supplied index value (or 1).
NOTE: In the current implementation, when there are large numbers of
property values, using glob
is (potentially) much more
efficient than match
for locating names.
<foreach>
.
if the attribute nocase
is present, then a case
insensitive match is performed.
When either glob
or match
is specified, then
the "namespace" attribute may be used to restrict the name lookups to start
with that namespace. However, the values aren't restricted to being in the
specified namespace.
BUG: You must only specify a "namespace" if it has already been imported.
foreach
<server>
directive.
The four additional parameters used to control sorting are:
reverse
sort[=key]
getProperty
). A
sample use of the sort key would be:
<foreach name=id property=employee.ids sort="${employee.${id}.last}, ${employee.${id}.first}">This option can be tricky to use correctly. The following example will not sort employees by last name:
1. <foreach name=id property=employee.ids sort="employee.${id}.last">Why? Because another level of ${...} needs to be inserted in the sort key:
2. <foreach name=id property=employee.ids sort="${employee.${id}.last}">Example (1) will just sort the literal strings "employee.1234.last", "employee.5678.last", etc. while example (2) will do the correct thing and sort the values "Stevens" and "Johnson". Remember that BSL sorts based on exactly what you pass it and does not know that the provided string should be treated as another variable itself.
numeric
sort
parameter, it
causes the items to be interpreted as numbers (or zero if the item
doesn't look like a number).
nocase
sort
parameter, it
causes the items to be sorted in a case-insensitive fashion.
Note that when
used with "glob=..."
or "match=..."
, it
does NOT cause the glob or regular expression to be case
insensitive. It causes the results of the glob or regexp to be
sorted in a case-insensitive fashion. There is currently no way
to specify a case-insensitive glob or regular expression.
<abort>
tag terminates processing of the current
HTML page at the point it is evaluated. All HTML on the page after the
<abort>
tag is discarded, and the HTML processed up to
that point is returned. This tag can be placed anywhere on a page,
including within a <foreach>
or <if>
construct.
The following is an example usage of this tag:
<foreach name=x list="0 1 2 3"> <if name=x value=3> <abort> </if> <get name=x> </foreach> TestingThis example produces the output:
0 1 2
<break>
tag terminates processing within a
<foreach>
construct. The processing of HTML
continues immediately after the </foreach>
. This
tag can only be used inside of a <foreach>
tag.
The following is an example usage of this tag:
<foreach name=x list="0 1 2 3"> <if name=x value=3> <break> </if> <get name=x> </foreach> TestingThis example produces the output:
0 1 2 Testing
<continue>
tag continues processing at the top
of a <foreach>
construct. This skips any HTML
after the <continue>
tag and before the
</foreach>
. This tag can only be used inside of a
<foreach>
tag.
The following is an example usage of this tag:
<foreach name=x list="0 1 2 3"> <if name=x value=2> <continue> </if> <get name=x> </foreach> TestingThis example produces the output:
0 1 3 Testing
<extract>
tag permits portions of a property's
value to be extracted into additional properties, based on either glob
or regular expression patterns. The extract tag takes the following
tag parameters:
name=var
prepend=base
glob=pattern
glob
pattern to
match against. In turn, the following properties are set:
base.1
, base.2
, ...
are the substrings that matched the wildcard characters in
the pattern.
match=pattern
regular expression
pattern to match against. In turn, the following
properties are set:
base.0
is the substring that matched the
whole pattern.
base.1
, base.2
, ...
are the substrings that matched the parenthesized subexpressions
in pattern, if any.
If the attribute all
is present, then all matches and
submatches are extracted into properties. The properties
base.0
, base.1
,
etc., are set to the 1st matched expression, the 2nd matched
expression, etc. The properties
base.0.0
, base.0.1
...
are set to the sub-matches of the first full match, and so forth.
If any matches are found the following additional
properties are set:
basematches
basesubmatches
.
basematchelist
.
replace=substitution
substitution
).
The resultant substituted value is placed in the property:
prefix.replace.
map
tag_extract(sunlabs.brazil.template.RewriteContext)
for more detail.
glob
or match
must be specified.
In addition, the property base.matches
is
set to a value indicating the number of matches and submatches stored.
This property can be examined and compared with 0
to
determine if the <extract>
tag matched at all. If
there was no match, the numbered properties base.N
are not set or changed from their previous value.
getProperty
may be used.
Any time a boolean parameter (XXX) is allowed (nocase, not, numeric, or reverse) it is considered false if it takes any of the forms: XXX=0, XXX=no, XXX=false XXX="". If it takes the forms XXX or XXX="anything else", the value is true.
see a sample HTML page that contains some BSL markup.
SetTemplate
,
Serialized FormField Summary | |
---|---|
static int |
ABORT
|
static int |
BREAK
|
static int |
CONTINUE
|
Fields inherited from class sunlabs.brazil.template.Template |
---|
debug |
Constructor Summary | |
---|---|
BSLTemplate()
|
Method Summary | |
---|---|
boolean |
init(RewriteContext hr)
Called before this template processes any tags. |
void |
tag_abort(RewriteContext hr)
Handles the "abort" tag. |
void |
tag_break(RewriteContext hr)
Handles the "break" tag. |
void |
tag_continue(RewriteContext hr)
Handles the "continue" tag. |
void |
tag_extract(RewriteContext hr)
Handle the [experimental] "extract" tag. |
void |
tag_foreach(RewriteContext hr)
Handles the "foreach" tag. |
void |
tag_if(RewriteContext hr)
Handles the "if" tag. |
Methods inherited from class sunlabs.brazil.template.Template |
---|
done |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int ABORT
public static final int BREAK
public static final int CONTINUE
Constructor Detail |
---|
public BSLTemplate()
Method Detail |
---|
public boolean init(RewriteContext hr)
Template
init
in interface TemplateInterface
init
in class Template
public void tag_abort(RewriteContext hr)
public void tag_break(RewriteContext hr)
public void tag_continue(RewriteContext hr)
public void tag_foreach(RewriteContext hr)
public void tag_if(RewriteContext hr)
public void tag_extract(RewriteContext hr)
<set name=entry value="joe:211A:x3321"> <extract name=entry glob=*:*:* map="name room phone">Will return the values:
entry.name=joe entry.room=211A entry.phone=x3321In "glob" extraction, each wildcard in the glob pattern is assigned the next token in "map". in Regular expression extractions, when "all" is specified, the map names are used to name the sub-expressions. Without "all" the names are assigned like "glob", only the first name gets the entire match (e.g. you need one more name for "match" than for "glob".
NOTE: The namespace will be accessable by any other templates associated with the same TemplateRunner, using the default sessionTable (see SetTemplate).
|
Version Kenai-svn-r24, Generated 08/18/09 Copyright (c) 2001-2009, Sun Microsystems. |
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |