sunlabs.brazil.sunlabs
Class MiscTemplate

java.lang.Object
  extended by sunlabs.brazil.template.Template
      extended by sunlabs.brazil.sunlabs.MiscTemplate (view source)
All Implemented Interfaces:
TemplateInterface

public class MiscTemplate
extends Template

Template for misc string manipulation functions.

<append name="..." value="..." [delim=".." namespace="..." prepend=true|false]>
Append a value to an existing variable, using an (optional) delimiter to separate values. If prepend is true, the value is prepended instead.
<push name="..." value="..." namespace="...">
Push a variable value on a stack, and replace the value with a new one.
<pop name="..." namespace="...">
Pop a previously pushed value from the stack.
<increment name="..." [incr=n namespace="..."]>
Increment the value of a variable.
<sequence [name=".."] count=".." [start=".." incr=".." delim=".." min=".." max=".."]>
Generate a sequence of values.
<expr value="..." [name="..." namespace="..." format="..."]>
Do an arithmetic expression evaluation on "value".
<inline name="..." [eval="true|false]> ... </inline>
Place all the text between the "inline" and "/inline" tags into the variable indicated by "name".
<eval markup="..." [atEnd=true|false]>
Evaluate a variable as if the value was in-line markup. If "atEnd" is specified, the markup is interpolated at the end of the file.
<stringop name="..." [newname="..." range="start,end" trim="left|right|both" case="upper|lower|title" strip="re_char_class" convert="url|html|..." >
Perform misc. string functions on the value of a named variable.
<mapnames src=src_pattern dst=dst_pattern [namespace=dest_namespace] [range="start,end", trim="left,right", case="upper|lower|title" convert=url|html]>
Map a set of variables from one namespace to another, and optionally change the names and process the values. The "src" parameter is a glob pattern that is expected to match the names of one of more properties. The "dst" parameter (which defaults to the name actually matched) is the new name of the variable that is copied into the specified "namespace" (which defaults to "local"). ${...} substitutions are performed on the value of "dst" separately for each matched variable. The variables ${1}, ${2} ... ${i} ... ${9} take on the value of the "i"th wildcard match, as in Glob.match(). ${0} refers to the actual name of the matched variable, and ${mapCount} is the index of the current variable being mapped, starting with "1".

If "remove" is specified, then the source variables are removed.

All the conversion options provided by <stringop> are also available.

Example:

 <mapnames src=query.pref_* dst="pref_${1}" namepace=${SessionID}.pref>
 
Will take the query parameters from the current form that refer to user preferences, and save them in a preferences namespace associated with the current session.
<stringlength name="xxx" value="${...}">
Compute the length of the string specified in "value" and place it in the property "name".
<random name=xxx [count=xxx start=xxx type=int|hex|alpha>
Generate a random value. if "type=int", the default, then count (defaults to 2) specifies the range of integers from which the value is taken, and "start" (defaults to 1) specifies the minimum value.

When "type=hex", then "start" is taken to be a hex value (or 0 if start is not a valid hex value).

When "type=alpha", the "start" must contain only a-z, and the result is the alpabetic result, in radix 26.

<subst name=result value="... ${...} .." [recurse|backslashes]>
Do ${..} substitutions to a value.


Nested Class Summary
static interface MiscTemplate.Convert
          Interface for dynamically loaded String converters.
static class MiscTemplate.GlobProperties
          Special version of a properties that uses the sub expresions of the supplied glob pattern and name to define the keys 1-9.
 
Field Summary
 
Fields inherited from class sunlabs.brazil.template.Template
debug
 
Constructor Summary
MiscTemplate()
           
 
Method Summary
static int fromAlpha(String s)
          Convert an alpha-only string to an integer.
 boolean init(RewriteContext hr)
          Called before this template processes any tags.
 void tag_append(RewriteContext hr)
          Append a value to a property, with an optional delimeter.
 void tag_eval(RewriteContext hr)
          Evaluate the contents of a variable as a template, as if it was "inline" here.
 void tag_expr(RewriteContext hr)
          Set a variable to the result of an arithmetic expression <expr [name=nnn] value="expr" [format="..." ] [namespace="..."]> sets name to "NaN" if the expression was invalid.
 void tag_increment(RewriteContext hr)
          Increment the value of a variable.
 void tag_inline(RewriteContext hr)
          Set a variable to all the markup 'till the /inline tag.
 void tag_mapnames(RewriteContext hr)
          Map a set of properties from one namespace to another, and (optionally) change their names and values.
 void tag_pop(RewriteContext hr)
          Treat a variable as a stack, and pop a value from it.
 void tag_push(RewriteContext hr)
          Treat a variable as a stack, and push a value onto it.
 void tag_random(RewriteContext hr)
           
 void tag_replace(RewriteContext hr)
          Regular expression replacement.
 void tag_sequence(RewriteContext hr)
          Generate a (mostly) numeric sequence.
 void tag_stringlength(RewriteContext hr)
          Compute string length from "value" , return in "name"
 void tag_stringop(RewriteContext hr)
          String manipulation functions.
 void tag_subst(RewriteContext hr)
          Do ${...} substitutions in the current context.
static String toAlpha(int i)
           
 
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
 

Constructor Detail

MiscTemplate

public MiscTemplate()
Method Detail

tag_append

public void tag_append(RewriteContext hr)
Append a value to a property, with an optional delimeter. <append name="..." value="..." [delim="..." namespace="..."]> If "name" is undefined or empty, it is set to "value". The result is placed in the local namespace, unless the "namespace" is used to override it. Use namespace=${SessionID} to put the result into the current session. The current value is looked up in the request properties, and (if not found), the namespace specified by the "namespace" parameter, if any. Use the ListTemplate for more generic list manipulation.


tag_increment

public void tag_increment(RewriteContext hr)
Increment the value of a variable. <incr name=variable [incr=n namespace="..."]> -If undefined or empty, assume it was '0'.
-If the value was non numeric, append or modify any numeric suffix.
The result is placed in the local namespace, unless the "namespace" is used to override it. Use namespace=${SessionID} to put the result into the current session.


tag_sequence

public void tag_sequence(RewriteContext hr)
Generate a (mostly) numeric sequence. <sequence name="..." count="..." [start="..." incr="..." delim=".." min=".." max=".."]>
name
Where to put the result. Defaults to the local namespace, but may be overridden with "namespace=".
count
the number of values to generate
start
The start value. Defaults to "1". If non numeric, any trailing digits are incremented, if present, or appended otherwise.
The increment value. Defaults to "1" if the value is not an integer.
start
The starting value
delim
The delimiter between values. defaults to " ".
min
max
If supplied this limits "count" to be at least "min" and no more than "max".


tag_expr

public void tag_expr(RewriteContext hr)
Set a variable to the result of an arithmetic expression <expr [name=nnn] value="expr" [format="..." ] [namespace="..."]> sets name to "NaN" if the expression was invalid. The result is placed in the local namespace, unless the "namespace" is used to override it. Use namespace=${SessionID} to put the result into the current session.

If "name" is omitted, the result is placed in the document. If "format" is used, it is java.text.DecimalFormat format specifier. Briefly:

$
currency
%
percent
.
decimal point
,
group separator, as in: 1,000
#
a digit, if required, blank otherwise
0
a digit, if required, 0 otherwise.
Example: format="$#,##0.00" would be typical for expressing monetary values.


tag_eval

public void tag_eval(RewriteContext hr)
Evaluate the contents of a variable as a template, as if it was "inline" here. <eval markup="..." [atEnd=true|false]>


tag_subst

public void tag_subst(RewriteContext hr)
Do ${...} substitutions in the current context. This should be part of the SetTemplate.

See also the TemplateTemplate for processing markup instead of just ${...} constructs.


init

public boolean init(RewriteContext hr)
Description copied from class: Template
Called before this template processes any tags.

Specified by:
init in interface TemplateInterface
Overrides:
init in class Template

tag_push

public void tag_push(RewriteContext hr)
Treat a variable as a stack, and push a value onto it. <push name="..." value="..." [clear=true|false namespace="..."]> For more generic list manipulation, use the ListTemplate.

The result is placed in the local namespace, unless the "namespace" is used to override it. Use namespace=${SessionID} to put the result into the current session.


tag_pop

public void tag_pop(RewriteContext hr)
Treat a variable as a stack, and pop a value from it. <pop name="..." [clear=true|false namespace="..."]> The result is placed in the local namespace, unless the "namespace" is used to override it. Use namespace=${SessionID} to put the result into the current session.


tag_inline

public void tag_inline(RewriteContext hr)
Set a variable to all the markup 'till the /inline tag. <inline name="..." [eval=true|false] [append=true|false]> ... </inline>

If "eval" is true, then ${..} substitutions are performed before assigning the markup the the named variable. If "esc" is also true, the \X sequences will be replaced as well. If "append" is true and "name" is specified, then the markup is appended to the current contents of "name". If no "name" is specified, the markup is output as-is, after ${..} substitutions (e.g. eval=true is implied).


tag_stringop

public void tag_stringop(RewriteContext hr)
String manipulation functions. Any of the following options are supported. If more than one of "range", "trim", "case", "strip", or "convert" is specified, they are performed in the order listed below. The value named by the "name" attribute is modified, and the result is placed in the local namespace, using the same name, unless "newname" is specified, in which case "newname" is used instead.

The "namespace" attribute can be used to alter the namespace to put the result into, in which case the "sessionTable" configuration parameter can be used to alter the namespace class, which defaults to the template handler's (or filter's) prefix.

<stringop name=xxx range=x,y [newname=xxx namespace=xxx]>
<stringop name=xxx range=x [newname=xxx namespace=xxx]>
Do a substring from character position 'x' (starting at 0) up to but not including character position 'y'. Negative values count from the end of the string. If no "y" is specified, then the end of the string is assumed.
<stringop name=xxx trim=left|right|both [newname=xxx namespace=xxx]>
Trim whitespace from the string. "trim=true" is equivalent to "both".
<stringop name=xxx case=lower|upper|title [newname=xxx namespace=xxx]>
Do case conversions. "title" causes the entire string to
<stringop name=xxx strip=list [newname=xxx namespace=xxx]>
Remove all the characters from the string contained in "list", which is a regular expression character class. The characters '^', '-', or ']' are treated special in regular expression character classes. See Regexp.
<stringop name=xxx convert=html|url|other [newname=xxx namespace=xxx]>
The string is converted exactly the same as the "convert" option of the "get" tag in the SetTemplate() for "html" or "url". In addition "hidetags" can be used to escape '>' and '<' only with &lt; and &gt; respectively.

For any other string, a String converter is dynamically loaded to do the conversion, by looking first for a "other.class" attribute, then an "other.class" property of this template. That class is expected to implemnt the "Convert" interface. Class loading is done on first reference to a converter. See JSONConverter for a sample converter plug-in.


tag_stringlength

public void tag_stringlength(RewriteContext hr)
Compute string length from "value" , return in "name"


tag_mapnames

public void tag_mapnames(RewriteContext hr)
Map a set of properties from one namespace to another, and (optionally) change their names and values. <mapnames src=glob-pattern namespace=dst_space dst=dst-pattern [stringops options] [remove=true|false]>


tag_random

public void tag_random(RewriteContext hr)

tag_replace

public void tag_replace(RewriteContext hr)
Regular expression replacement. This is a subset of the <extract> tag in the BSLTemplate Usage: <replace name=... match= ... replace=... nocase=true|false all=true|false>


toAlpha

public static String toAlpha(int i)

fromAlpha

public static int fromAlpha(String s)
Convert an alpha-only string to an integer. no error checking!


Version Kenai-svn-r24, Generated 08/18/09
Copyright (c) 2001-2009, Sun Microsystems.