« Return to Thread: Jaxb Transformers

Re: Jaxb Transformers

by mschiffedb :: Rate this Message:

Reply to Author | View in Thread

Hi,

since JAXB-based unmarshalling of XML messages received via XML is one of the first requirements for my mule prototype / showcase, I would like to follow up on the JIRA that has been created for this work in the meantime: http://mule.mulesource.org/jira/browse/MULE-1208

As far as my understanding reaches JAXB unmarshalling is not yet available as part of mule's XML modules?

In order to get something like the custom transformer workaround working (see below) it looks like one has to include some special mule module in one's maven config / pom.xml config file in order to (indirectly) have JAXB jars available as part of one's project (this is what comments on the JIRA ticket say).

==> Could somebody advise on how exactly I can get the custom transformer / the previously posted codebase to work?  How can I ensure that I have JAXB jars available + that there are no JAR version clashes (which mule module to load/to add to pom.xml)?

Also...

==> Could I use Spring XOM (part of spring-ws) as the basis for a custom transformer and how would I bring this into the mule XML config? Which modules would I have to load for this via pom.xml?

Thanks + regards, Mike

In addition, ...
...

James Richardson-3 wrote:
Not sure if these already exist, but might be useful to somebody....

Configure like this:

  <transformer name="JaxbXmlToObject" className="JaxbXmlToObject">
            <properties>
                <property name="jaxbContextFactoryClass"
value="YourJaxbContextFactory "/>
            </properties>
        </transformer>

public class YourJaxbContextFactory {
    public static JAXBContext getJaxbContext() throws JAXBException {
        return JAXBContext.newInstance(SomeJaxb.class,AnotherJaxb.class);
    }
}

Best Regards,

James


public abstract class AbstractJaxbTransformer extends
AbstractEventAwareTransformer {
    private String jaxbContextFactoryClass;
    protected JAXBContext jaxbContext;

    @Override
    public void initialise() throws InitialisationException {

        try {
            Class clazz = Class.forName(jaxbContextFactoryClass);
            Method method = clazz.getMethod("getJaxbContext");
            jaxbContext = (JAXBContext) method.invoke(clazz);
        }
        catch (ClassNotFoundException e) {
            throw new InitialisationException(e, this);
        }
        catch (NoSuchMethodException e) {
            throw new InitialisationException(e, this);
        }
        catch (IllegalAccessException e) {
            throw new InitialisationException(e, this);
        }
        catch (InvocationTargetException e) {
            throw new InitialisationException(e, this);
        }
    }

    public String getJaxbContextFactoryClass() {
        return jaxbContextFactoryClass;
    }

    public void setJaxbContextFactoryClass(String jaxbContextFactoryClass) {
        this.jaxbContextFactoryClass = jaxbContextFactoryClass;
    }

    public JAXBContext getJaxbContext() {
        return jaxbContext;
    }

    public void setJaxbContext(JAXBContext jaxbContext) {
        this.jaxbContext = jaxbContext;
    }
}




import org.mule.umo.UMOEventContext;
import org.mule.umo.transformer.TransformerException;

import javax.xml.bind.Unmarshaller;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringReader;
import java.io.StringWriter;

public class JaxbObjectToXml extends AbstractJaxbTransformer {

    public JaxbObjectToXml() {
        registerSourceType(Object.class);
        setReturnClass(String.class);
    }

    public Object transform(Object src, String encoding, UMOEventContext
context) throws TransformerException {
          try {
            Marshaller unmarshaller = jaxbContext.createMarshaller();
              StringWriter writer = new StringWriter();
            unmarshaller.marshal(src,writer);
              return writer.toString();
        }
        catch (JAXBException e) {
            throw new TransformerException(this, e);
        }


    }
}



import org.mule.umo.UMOEventContext;
import org.mule.umo.transformer.TransformerException;

import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;

public class JaxbXmlToObject extends AbstractJaxbTransformer {

    public JaxbXmlToObject() {
        registerSourceType(String.class);
        setReturnClass(Object.class);
    }

    public Object transform(Object src, String encoding, UMOEventContext
context) throws TransformerException {
        try {
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            return unmarshaller.unmarshal(new StringReader((String) src));
        }
        catch (JAXBException e) {
            throw new TransformerException(this, e);
        }
    }

}




---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

 « Return to Thread: Jaxb Transformers