VelocityTemplateParserUnit

Class name

com.ebd.hub.datawizard.iu.VelocityTemplateParserUnit


This Integration Unit can be used to create a text file via a velocity template file.

An XML file is generated internally from the target tree. This XML file (passed with the prefix "xmlRoot", see example) is processed by the Velocity template file to generate the final text file. This text file can then be used in the Response as the output of the Integration Unit.

Parameters


Parameter

Allowed values

Default value

Description

create root element 'VelocityRoot'

true, false

false

If no valid root element is created (e.g. due to several top-level nodes), this parameter must be set to true. This creates a top-level element VelocityRoot in the XML file.

template-file



Path to template file with Velocity instructions . Example: ./conf/sample.vm

use Velocity XmlTool

true, false

false

If true, the variable useXmlTool is set to true (see example). This variable can be used to access the internally generated XML with the Apache Velocity XmlTool (Version 3.1) (see example). This simplifies the Velocity statements. If the XmlTool is not used, normal Java DOM instructions must be used (see example).

write created XML to log

true, false

false

The generated XML file is written into the log . Important note: This only works in a real profile run and not in a mapping test.

Example


In the following example, the profile reads the source data (input.xml) into the source structure. Via 1:1 mapping, these are transferred to the target structure. The internal XML file is generated from the target tree (i.e. the target structure filled with data). Please note that the source data does not have to be in XML format. The internal XML file is always created from the target tree. The Integration Unit then uses the internal XML file and the Velocity template to generate the final text file, which is then used in the Response. Note that the internal XML file is addressed in the Velocity template with the prefix xmlRoot. Run the profile with the values true and false for the Integration Unit parameter use Velocity XmlTool. In the job log you will find the internally generated XML file.


Profile: Profile-Test_Velocity_Unit.pak

Profile source data: input.xml

Velocity template file: sample.vm


sample.vm
## This is a comment (two #).
## The next line defines a macro that only works WITHOUT XmlTools. The macro outputs the XML with DOM.
#macro ( recursive $e $indent )
#if( $e.getChildren().size() > 0 )
$indent <$e.getName()>
#foreach ($child in $e.getChildren() )
#recursive( $child "$indent " )
#end
$indent </$e.getName()>
#else
$indent <$e.getName()>
$indent $e.getTextTrim()
$indent </$e.getName()>
#end
#end
#set($i = " ")
 
## The variable is set by the Integration Unit parameter "useXmlTool".
#if($useXmlTool)
With XmlTool, access by simplified 'XPath':
body : $xmlRoot.body.text
## "name" and "last" are functions of XmlTool, therefore the pure .dot notation does not work here.
last name: $xmlRoot.find('/document/properties/author/name/last/full').text
email: $xmlRoot.properties.author.email.text
## Without XmlTool.
#else
First, we print out the document tree with a recursive Velocimacro :
#recursive( $xmlRoot.getRootElement() $i )
 
Next, we access pieces of data directly :
email : $xmlRoot.getRootElement().getChild("properties").getChild("author").getChild("email").getText()
last name : $xmlRoot.getRootElement().getChild("properties").getChild("author").getChild("name").getChild("last").getChild("full").getText()
#end