« Return to Thread: Jaxb Transformers

Jaxb Transformers

by James Richardson-3 :: Rate this Message:

Reply to Author | View in Thread

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