So, we'll base this tuorial on the Smooks In a Servlet Container turorial:
- For "message-target1", we will convert <aaa> elements to <zzz> elements.
- For "message-target1", we will convert <ccc> elements to <xxx> elements.
- For "message-target2", we will convert <aaa> elements to <zzz> elements.
So, when transforming the message "<aaa><bbb>888</bbb><ccc>999</ccc></aaa>", we want:
- message-target1 to receive "<zzz><bbb>888</bbb><xxx>999</xxx></zzz>", and
- message-target2 to receive "<zzz><bbb>888</bbb><ccc>999</ccc></zzz>".
Write the Transformation Unit
So, the transformation code to perform this task is very simple:
The constructor and "visit" method code is of most interest. In the constructor we capture a parameter from the transformation configuration (talked about below). In the "visit" method, we use this parameter value to perform the rename. So as you can see, we've actually implemented the transformation unit such that it can be reused for renaming any element.
Classes of interest here are:
Target the Transformation Unit
As you'll have already noticed, message-target1 and message-target2 transformations have something in common i.e. they both need <aaa> elements converted to <zzz> elements. We can use profiling to target this transformation at both message targets i.e. make both message targets members of the same profile. Using profiles here is a little silly, but it still makes the illustration.
The first piece of code creates the SmooksStandalone instance and initialises it with the useragents (and their profiles). This basically tells the SmooksStandalone instance that it will be transforming messages for these targets.
Next we create the resource configuration file and add it somewhere in the application classpath. This configuration tell the SmooksStandalone instance when to trigger the above RenameElementTrans ProcessingUnit i.e. on which target elements, and for which message targets. Note we can use the useragent name or a profile name for useragent targeting. Profiles simply allow us to reduce the number of configuration entities where two or more useragent targets require the same transformation.
The resource configuration is registered with the SmooksStandalone instance as follows:
Alternatively, we could have registered these resource configurations by constructing a set of SmooksResourceConfiguration instances, calling the registerResource method on the SmooksStandalone instance for each SmooksResourceConfiguration instances.
Execute the Transformation
Now, to transform a message stream for one of these message targets:
Or, if transforming a DOM message: