Show Table of Contents
11.4. Configuring Smooks to Capture Multiple NodeModels
- To configure Smooks to capture multiple NodeModels for use by the FreeMarker templates, you should configure the DomModelCreator visitor. It should be targeted at the root node of each model. Note again that Smooks also makes this available to SAX filtering (the key to processing huge messages).This is The Smooks configuration for creating the NodeModels for the message:
<?xml version="1.0"?> <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.3.xsd" xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd"> <!-- Filter the message using the SAX Filter (i.e. not DOM, so no intermediate DOM for the "complete" message - there are "mini" DOMs for the NodeModels below).... --> <core:filterSettings type="SAX" defaultSerialization="false" /> <!-- Create 2 NodeModels. One high level model for the "order" (header etc) and then one for the "order-item" elements... --> <resource-config selector="order,order-item"> <resource>org.milyn.delivery.DomModelCreator</resource> </resource-config> <!-- FreeMarker templating configs to be added below... --> - Next, apply the following FreeMarker templates:
- A template to output the order
headerdetails, up to but not including the order items. - A template for each of the order items, to generate the item elements in the salesorder.
- A template to close out the message.
With Smooks, you can implement this by defining two FreeMarker templates. One to cover points one and three (combined) above, and a second to cover the item elements. - Applt the first FreeMarker template. It is targeted at the order-items element and looks like this:
<ftl:freemarker applyOnElement="order-items"> <ftl:template><!--<salesorder> <details> <orderid>${order.@id}</orderid> <customer> <id>${order.header.customer.@number}</id> <name>${order.header.customer}</name> </customer> </details> <itemList> <?TEMPLATE-SPLIT-PI?> </itemList> </salesorder>--> </ftl:template> </ftl:freemarker>The?TEMPLATE-SPLIT-PI?processing instruction tells Smooks where to split the template, outputting the first part of the template at the start of the order-items element, and the other part at the end of the order-items element. The item element template (the second template) will be output in between. - Apply the second FreeMarker template. This outputs the item elements at the end of every order-item element in the source message:
<ftl:freemarker applyOnElement="order-item"> <ftl:template><!-- <item> <id>${.vars["order-item"].@id}</id> <productId>${.vars["order-item"].product}</productId> <quantity>${.vars["order-item"].quantity}</quantity> <price>${.vars["order-item"].price}</price> </item>--> </ftl:template> </ftl:freemarker> </smooks-resource-list>Because the second template fires on the end of the order-item elements, it effectively generates output into the location of the?TEMPLATE-SPLIT-PI?processing instruction in the first template. Note that the second template could have also referenced data in theorderNodeModel. - Apply a closing template of your choice.NoteThis approach to performing a one-to-one transformation of a huge message works because the only objects in memory at any one time are the order header details and the current order-item details (in the Virtual Object Model). Obviously it can' work if the transformation is so obscure as to always require full access to all the data in the source message, for example if the messages needs to have all the order items reversed in order (or sorted). In such a case however, you do have the option of routing the order details and items to a database and then using the database's storage, query and paging features to perform the transformation.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.