Macro to encode high-characters

I found that one of the more annoying features of the Velocity Engine in 6.5.2 that I haven’t been able to work around in settings has been string encoding issues revolving around high-order characters (with an ASCII ordinal value greater than 127 (0x7F)). Here’s a macro that can be added to your code or “User Velocity Macros” file to make dealing with this a tad easier.

## macro to escape high-characters in html without encoding the normal characters
## Sam Rushing, October 14, 2009
#macro(htmlEncodeHighCharacters $var $string)##
#set( $builder = $user.psoStringTools.getStringBuilder() )##
#set( $normals = $string.split('[^\x00-\x7F]+') )##
#set( $specials = $string.split('[\x00-\x7F]+') )##
#set( $normalMax = $tools.math.add($tools.list.size($normals),-1) )##
#foreach( $i in [0..$normalMax] )##
#if ( $tools.list.size($specials) > $i )##
#set( $xxx = $builder.append($tools.esc.html($tools.list.get($specials,$i))) )##
#end##
#set( $xxx = $builder.append($tools.list.get($normals,$i)) )##
#end##
#set( $var = $builder.toString() )##
#end

to use:

#htmlEncodeHighCharacters( $myVar $sys.item.getProperty('rx:fieldname').string )
$myVar