How to update deprecated Velocity code
Older Velocity code often reached into Java classes directly through reflection. Those patterns are deprecated, and each has a Velocity Tool replacement. This article pairs the old form with the new one.
FieldTool.in for entity types
The $_FieldTool.in(String) method was used to reference entity types for the Locator Tool. New Locator Tool methods locate each asset type directly.
Old:
$_.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)
New:
$currentPage
$_.locateFormat("path/to/format")
$endCal.add($_FieldTool.in($endCal).YEAR, $numberOfYearsToDisplay)
Generating random content
java.util.UUID, DigestUtils, and java.util.Random were used to generate random strings and UUIDs, commonly for unique DOM element ids. Use generateUUID() and random() instead.
Old:
#set($id = "video-${class.forName('java.util.UUID').randomUUID().toString()}")
$class.forName('java.util.Random').newInstance().nextInt(9999999)
New:
<div id="video-${_StringTool.generateUUID()}">
$_MathTool.random(0, 9999999)
Unescaping content
StringEscapeUtils was used to unescape previously escaped content. The Escape Tool now covers this.
Old:
$class.forName('org.apache.commons.lang.StringEscapeUtils').unescapeHtml("<br/>")
New:
$_EscapeTool.unescapeHtml("<br/>")
Regular expressions
java.util.regex.Pattern was used to compile regular expressions. Use the Regex Tool.
Old:
#set($_pattern = $class.forName("java.util.regex.Pattern"))
#set($_ids = $_pattern.compile("#([\w_-]+)").matcher($_s))
New:
#set($_ids = $_RegexTool.compile("#([\w_-]+)").matcher($_s))
Concatenating with StringBuilder
java.lang.StringBuilder was used to concatenate strings without introducing whitespace. A String Tool method now provides an instance.
Old:
#set($sb = $class.forName('java.lang.StringBuilder').newInstance())
#set($sb = $sb.append('foo'))
${sb}
New:
#set($sb = $_StringTool.getStringBuilder())
#set($sb = $sb.append('foo'))
${sb}
Checking an asset's type
getClass() combined with getSimpleName() was used to check an API object's type. Use getAssetType().
Old:
#set ($chosenAsset = $currentPage.getStructuredDataNode("assetchooser").asset)
#if ($chosenAsset.class.simpleName == "PageAPIAdapter")
...
#end
New:
#set ($chosenAsset = $currentPage.getStructuredDataNode("assetchooser").asset)
#if ($chosenAsset.assetType == "page")
...
#end
Checking a data structure's type
The Property Tool gained isString, isMap, isList, isSet, and isIterable.
Old:
#set($map = {})
#if ($map.class.simpleName.contains("Map"))
...
#end
New:
#set($map = {})
#if ($_PropertyTool.isMap($map))
...
#end
Checking whether an object is numeric
Old:
#set($number = 3)
#if ($number.class.forName('java.lang.Number'))
...
#end
New, using the Number Tool:
#set($number = 3)
#if ($_NumberTool.isNumeric($number))
...
#end
Converting a map to a JSON string
$_SerializerTool.toJson outputs a Map as JSON directly.
Old:
#set($map = {"test": "testing", "array": [1, 2, 3]})
#foreach ($key in $map.keys)
#set ($value = $map.get($key))
...
#end
New:
#set($map = {"test": "testing", "array": [1, 2, 3]})
$_SerializerTool.toJson($map)