Introducing Altova Mapforce

21 May

Although we see many advantages in using (free) open source tools or frameworks we keep our eyes open to check whether the chosen open source solution is best for our customer.

We recently had a situation where we preferred a commercial solution over an open source one.
For a specific interface, we had to map a CSV file to an XML file and this transformation should be processed by the Mule ESB. At first we looked at Smooks to tackle this issue, as I described here. It resulted in a workable solution, but as you can read in post, the transformation had to take place in two steps: first from CSV to simple XML format and secondly, a transformation from the simple XML to a more advanced XML schema. For this second transformation we made use of an XSL stylesheet. We created the XSL file used for the transformation by making use of Altova MapForce.
However, MapForce can do a lot more then this simple XML to XML mapping. One of the things it can do is map CSV directly to (advanced) XML. This can’t be done with just a XSL translation anymore, but MapForce can generate Java code to perform the transformation at runtime. In this post I will show how you can use MapForce to generate the code. In a next post I will show you how to combine this generated code with the Mule ESB as a transformer.

The CSV file for this example looks like:

firstname|lastname|city|country
Steve|Jobs|Cupertino|USA
Bill|Gates|Washington|USA
Pascal|Alma|Papendrecht|Netherlands

As you can see a pipe-separated file with labels at the first row. When we add it to MapForce as an input source we configure it like:

The XSD to which the CSV file has to be mapped looks like:

In MapForce this target is configured like this:

The mapping in MapForce is quite simple and looks like this:

The output of the mapping can be viewed in MapForce (by pressing the “Output” tab) and looks like this (for this particular input file):

To perform this transformation in a Java environment we’ll generate Java code, after setting the package name that is generated by MapForce:

The code that is generated by MapForce looks like this:

As you can see, two packages are generated:

  • com.altova
  • The ‘com.altova’ package contains ‘generic’ code that is necessary for each MapForce mapping in Java. It contains global types, functions and so on.

  • nl.redstream
  • The ‘nl.redstream’ package is the package name we configured. This one contains the code that is specific for our mapping.

Beside these packages an Eclipse project file is generated and a Ant build script. To build this Java code you can run Ant with the following command:

To run the code after you build it you can use the following command:
java nl.redstream.CustomerMappingApplication

You will get a Frame like this:

Unfortunately by using the generated Main classes, the transformation can only be done on the files that where used as input for the mapping in this code. In the next post I will show how to use this generated code and use it as a Transformer in our Mule application.

For a lot more info about the Java generation possiblities of MapForce look here.

Follow

Get every new post delivered to your Inbox.

Join 101 other followers

%d bloggers like this: