Skip to main content

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:

Velocity
$_.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:

Velocity
$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:

Velocity
#set($id = "video-${class.forName('java.util.UUID').randomUUID().toString()}")
$class.forName('java.util.Random').newInstance().nextInt(9999999)

New:

Velocity
<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:

Velocity
$class.forName('org.apache.commons.lang.StringEscapeUtils').unescapeHtml("&lt;br/&gt;")

New:

Velocity
$_EscapeTool.unescapeHtml("&lt;br/&gt;")

Regular expressions

java.util.regex.Pattern was used to compile regular expressions. Use the Regex Tool.

Old:

Velocity
#set($_pattern = $class.forName("java.util.regex.Pattern"))
#set($_ids = $_pattern.compile("#([\w_-]+)").matcher($_s))

New:

Velocity
#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:

Velocity
#set($sb = $class.forName('java.lang.StringBuilder').newInstance())
#set($sb = $sb.append('foo'))
${sb}

New:

Velocity
#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:

Velocity
#set ($chosenAsset = $currentPage.getStructuredDataNode("assetchooser").asset)
#if ($chosenAsset.class.simpleName == "PageAPIAdapter")
...
#end

New:

Velocity
#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:

Velocity
#set($map = {})
#if ($map.class.simpleName.contains("Map"))
...
#end

New:

Velocity
#set($map = {})
#if ($_PropertyTool.isMap($map))
...
#end

Checking whether an object is numeric

Old:

Velocity
#set($number = 3)
#if ($number.class.forName('java.lang.Number'))
...
#end

New, using the Number Tool:

Velocity
#set($number = 3)
#if ($_NumberTool.isNumeric($number))
...
#end

Converting a map to a JSON string

$_SerializerTool.toJson outputs a Map as JSON directly.

Old:

Velocity
#set($map = {"test": "testing", "array": [1, 2, 3]})
#foreach ($key in $map.keys)
  #set ($value = $map.get($key))
  ...
#end

New:

Velocity
#set($map = {"test": "testing", "array": [1, 2, 3]})
$_SerializerTool.toJson($map)