Red Hat Training

A Red Hat training course is available for Red Hat Fuse

10.7. Supported Transformations

10.7.1. Java

10.7.1.1. Java Transformer

You can create a Java-based transformer in SwitchYard using any of the following methods:
  • Implement the org.switchyard.transform.Transformer interface and add a <transform.java> definition to your switchyard.xml.
  • Annotate one or more methods on your Java class with @Transformer annotation.
When using the @Transformer annotation, the SwitchYard maven plug-in automatically generates the <transform.java> definition and adds them to the switchyard.xml packaged in your application. Here is an example of a Java class that produces the <transform.java> definition:
@Named("MyTransformerBean")
public class MyTransformer {
    @Transformer(from = "{urn:switchyard-quickstart-demo:orders:1.0}submitOrder")
    public Order transform(Element from) {
       // handle transformation here
    }
}
Here, the from and to elements of the @Transformer annotation are optional. You can use them to specify the qualified type name used during transformer registration. If not specified, the full class name of the method parameter is used as the from type and the full class name of the return type is used as the to type.
The CDI bean name specified by @Named annotation is used to resolve transformer class. If not specified, the class name of the transformer is used instead. Here is an example:
<transforms>
   <transform.java class="org.switchyard.quickstarts.demos.orders.MyTransformer"
                   from="{urn:switchyard-quickstart-demo:orders:1.0}submitOrder"
                   to="java:org.switchyard.quickstarts.demos.orders.Order"/>
</transforms>
Note
The bean attribute and class attribute are mutually exclusive.

10.7.2. JAXB

10.7.2.1. JAXB Transformer

SwitchYard automatically detects and adds the JAXB Transformations for your application deployment. For example, if you develop a CDI Bean Service and use JAXB generated types in the Service Interface, you do not need to configure any of the transformations. SwitchYard automatically detects their availability for your application and automatically applies them at the appropriate time during service invocation.
The JAXB transformer allows you to perform Java to XML and XML to Java transformations using JAXB (XML marshalling and unmarshalling). The JAXB Transformer is similar to the JSON Transformer configuration. It also requires a to and from configuration with one Java type and one QNamed XML type.
For example in the source file ObjectFactory.java, the factory methods represent the available marshallings/unmarshallings as shown below:
@XmlElementDecl(namespace = "http://com.acme/orders", name = "create")
public JAXBElement<CreateOrder> createOrder(CreateOrder value) {
    return new JAXBElement<Order>(_CreateOrder_QNAME, CreateOrder.class, null, value);
}
Here, the @XmlElementDecl annotation implies that the XML QName associated with the com.acme.orders.CreateOrder type is "{http://com.acme/orders}create". This means that you can have the following SwitchYard JAXB Transformer configurations:
<transform.jaxb from="{http://com.acme/orders}create" to="java:com.acme.orders.CreateOrder" />
<transform.jaxb from="java:com.acme.orders.CreateOrder" to="{http://com.acme/orders}create" />

10.7.3. JSON

10.7.3.1. JSON Transformer

The JSON transformer provides a basic mapping facility between POJOs and JSON (JSON marshalling and unmarshalling). Just like the JAXB Transformer, specification of the transformer requires a to and from specification with one Java type and one QNamed JSON type, depending on whether you are performing a Java to JSON or JSON to Java transformation.
Here is an example configuration of JSON to Java transformation:
<trfm:transform.json
            from="{urn:switchyard-quickstart:transform-json:1.0}order"
            to="java:org.switchyard.quickstarts.transform.json.Order"/>
Here is a sample configuration of Java to JSON transformation:
<trfm:transform.json
            from="java:org.switchyard.quickstarts.transform.json.Order"
            to="{urn:switchyard-quickstart:transform-json:1.0}order"/>

10.7.4. Smooks

10.7.4.1. Smooks Transformer

SwitchYard provides three distinct transformation models for Smooks:
  • XML to Java : Based on a standard Smooks Java Binding configuration.
  • Java to XML: Based on a standard Smooks Java Binding configuration.
  • Smooks : This is a normal Smooks transformation in which you must define which Smooks filtering result is to be exported back to the SwitchYard Message as the transformation result.
You can declare a Smooks transformation by including a <transform.smooks> definition in your switchyard.xml. Here is an example:
<transform.smooks config="/smooks/OrderAck_XML.xml"
                  from="java:org.switchyard.quickstarts.transform.smooks.OrderAck"
                  to="{urn:switchyard-quickstart:transform-smooks:1.0}submitOrderResponse"
                  type="JAVA2XML"/>
The Smooks and XSLT translators require an external configuration file that tells the translator how to go about its actions. In the example above, the config attribute points to a Smooks resource containing the mapping definition. The type attribute can be one of SMOOKS, XML2JAVA, or JAVA2XML.
For more information on Smooks and XSLT configuration files, see Red Hat JBoss Fuse Development Guide Volume 2: Smooks.
For more information on the elements that extend the abstract transform, see the /docs/schema/soa/org/switchyard/transform/config/model/v1/transform_<latest_version>.xsd.

10.7.5. XSLT

10.7.5.1. XSLT Transformer

You can perform transformations with the XSLT transformer using an XSLT. You can configure it by specifying the to and from QNames, and the path to the XSLT. Here is an example of XSLT Transformer configuration:
<transform.xslt from="{http://acme/}A" to="{http://acme/}B" xsltFile="com/acme/xslt/A2B.xslt"/>