Combining Mule with Altova MapForce
- one is Altova generic, so will be reused for each mapping
- one is mapping specific so is the actual part that changes with the mapping (this package name is configurable)
We first jarred the Altova generic code and added it to our Maven repository (Artifactory) so we can include this in our Mule project as dependency. We removed the generated mapping-specific Java code and used the included Ant build script to create the jar file. Here is a screenshot of how it looks in Artifactory:

Now if we want to use Mapforce generated code, we add the following dependency to the pom:
<dependency> <groupId>com.altova</groupId> <artifactId>mapforce-core</artifactId> <version>2010R2</version> </dependency>
The next step is to add the generated mapping-specific code to the project. We do this by copying the generated mapping specific code to our Mule project (in MapForce you can configure the package name to be used for the generated code so that makes it easier fitting in with the rest of the code). Here is a screenshot of the code to copy in this case:

Here is an example of a Transformer which uses the generated code:
package net.pascalalma.transformers;
import com.altova.io.StreamInput;
import com.altova.io.StringInput;
import com.altova.io.StringOutput;
import java.io.InputStream;
import nl.redstream.mapping.MappingMapTocustomer2;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;
/**
*
* @author pascal
*/
public class CustomerCsvToXmlTransformer extends AbstractTransformer {
public CustomerCsvToXmlTransformer() {
registerSourceType(InputStream.class);
registerSourceType(String.class);
setReturnClass(String.class);
}
@Override
protected Object doTransform(Object payload, String encoding)
throws TransformerException {
MappingMapTocustomer2 mappingToXml = new MappingMapTocustomer2();
StringOutput output = new StringOutput();
if (payload instanceof String) {
StringInput input = new StringInput((String) payload);
try {
mappingToXml.run(input, output);
} catch (Exception ex) {
throw new TransformerException(this, ex);
}
} else if (payload instanceof InputStream) {
StreamInput sInput = new StreamInput((InputStream) payload);
try {
mappingToXml.run(sInput, output);
} catch (Exception ex) {
throw new TransformerException(this, ex);
}
}
return output.getString().toString();
}
}That’s it! Define the transformer as a ‘custom-transformer’ in your Mule
config and your done. You can now create and maintain all your mappings
visually with MapForce.We are now looking into the capabilities that SPL (Spy Programming Language) offer, because it looks like we can generate the Mule Transformer straight from MapForce, that would even boost the producivity more!
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





