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("&lt;br/&gt;")
## <br/>

Replacement

$_EscapeTool.unescapeHtml("&lt;br/&gt;")
## <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}