How to update deprecated Velocity code
$_FieldTool.in("com.hannonhill.cascade.model.dom.identifier.EntityTypes")
The $_FieldTool.in(String)
method was used to obtain reference to entity types in order to locate assets using the Locator Tool. New methods added to the Locator Tool allow for locating each type of asset.
Examples
$_.locate($currentPagePath, $_FieldTool.in("com.hannonhill.cascade.model.dom.identifier.EntityTypes").TYPE_PAGE, $currentPageSiteName)
$_.locate("path/to/format", $_FieldTool.in("com.hannonhill.cascade.model.dom.identifier.EntityTypes").TYPE_FORMAT, $currentPageSiteName)
$endCal.add( $_FieldTool.in("java.util.Calendar").YEAR, $numberOfYearsToDisplay )
#set ( $System = $String.class.forName('java.lang.System') )
#set ( $newLine = $System.getProperty('line.separator')
#set ( $title = $title.replaceAll($newLine, '') )
Replacements
$currentPage
$_.locateFormat("path/to/format")
$endCal.add( $_FieldTool.in($endCal).YEAR, $numberOfYearsToDisplay )
#set ( $title = $title.replaceAll($_EscapeTool.n, '') )
Generating a random content
The java.util.UUID
, org.apache.commons.codec.digest.DigestUtils
and java.util.Random
classes were often used to generate random strings or UUIDs. A common use case is to generate unique DOM element identifiers.
A new method, generateUUID(), added to the String Tool generates random UUID strings and there is an existing Math Tool method, random(), that can generate random numbers.
Examples
#set($id = "video-${class.forName('java.util.UUID').randomUUID().toString()}")
...
<div id="${id}">
...
</div>
$class.forName('java.util.Random').newInstance().nextInt(9999999)
Replacements
<div id="video-${_StringTool.generateUUID()}">
...
</div>
$_MathTool.random(0, 9999999)
Unescaping content
The org.apache.commons.lang.StringEscapeUtils
class was used as a way to unescape content that was previously escaped, such as HTML, XML, JavaScript, etc. New methods added to the Escape Tool provide ways to unescape content.
Example
$class.forName('org.apache.commons.lang.StringEscapeUtils').unescapeHtml("<br/>")
## <br/>
Replacement
$_EscapeTool.unescapeHtml("<br/>")
## <br/>
Regular expressions and pattern matching
The java.util.regex.Pattern
class was used to compile regular expressions for complex regular expression testing and group matching. A new Regex Tool provides a way to generate these pattern objects.
Example
#set($_pattern = $class.forName("java.util.regex.Pattern"))
#set( $_ids = $_pattern.compile("#([\w_-]+)").matcher($_s) )
Replacement
#set( $_ids = $_RegexTool.compile("#([\w_-]+)").matcher($_s) )
Concatenate strings using StringBuilder
The java.lang.StringBuilder
class was used as a way to concatenate strings together for outputting later, without introducing adding extra whitespace into the output. A new String Tool method provides access to a
StringBuilder
instance.
Example
#set($sb = $class.forName('java.lang.StringBuilder').newInstance())
#set ($sb = $sb.append('foo'))
...
${sb}
Replacement
#set($sb = $_StringTool.getStringBuilder())
#set ($sb = $sb.append('foo'))
...
${sb}
Checking Cascade asset type using getClass()
The getClass()
method was used as a way to access the underlying class of a given Cascade API object, such as combining with getSimpleName()
. The Cascade API provides a way to access the asset type using getAssetType()
.
Example
#set ($chosenAsset = $currentPage.getStructuredDataNode("assetchooser").asset)
#if ($chosenAsset.class.simpleName == "PageAPIAdapter")
...
#end
Replacement
#set ($chosenAsset = $currentPage.getStructuredDataNode("assetchooser").asset)
#if ($chosenAsset.assetType == "page")
...
#end
Checking data structure type using getClass()
The getClass()
method was used as a way to access the underlying class of a given object, such as combining with getSimpleName()
. Various methods have been added to the Property Tool to check data structure types: isString
, isMap
, isList
, isSet
, and isIterable
.
Example
#set($map = {})
#if ($map.class.simpleName.contains("Map"))
...
#end
Replacement
#set($map = {})
#if ($_PropertyTool.isMap($map))
...
#end
Checking if an object is numeric using getClass()
The getClass()
method was used as a way to check if a given object is numeric. A method has been added to the Number Tool to check if a given object is numeric.
Example
#set($number = 3)
#if ($number.class.forName('java.lang.Number'))
...
#end
Replacement
#set($number = 3)
#if ($_NumberTool.isNumeric($number))
...
#end
Converting a map to JSON string
A new $_StringTool.toJson method was added to allow for easily outputting a Map as a JSON string.
Example
#set($map = {"test": "testing", "array": [1, 2, 3]})
#foreach ($key in $map.keys)
#set ($value = $map.get($key))
...
#end
Replacement
#set($map = {"test": "testing", "array": [1, 2, 3]})
$_SerializerTool.toJson($map)