org.mule.module.xml.transformer.ObjectToXml Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mule-module-xml Show documentation
Show all versions of mule-module-xml Show documentation
Functionality for working with XML
The newest version!
/*
* Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
* The software in this package is published under the terms of the CPAL v1.0
* license, a copy of which has been included with this distribution in the
* LICENSE.txt file.
*/
package org.mule.module.xml.transformer;
import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.types.DataTypeFactory;
/**
* ObjectToXml
converts any object to XML using Xstream. Xstream uses
* some clever tricks so objects that get marshalled to XML do not need to implement
* any interfaces including Serializable and you don't even need to specify a default
* constructor. If MuleMessage
is configured as a source type on this
* transformer by calling setAcceptMuleMessage(true)
then the MuleMessage
* will be serialised. This is useful for transports such as TCP where the message
* headers would normally be lost.
*/
public class ObjectToXml extends AbstractXStreamTransformer
{
public ObjectToXml()
{
this.registerSourceType(DataTypeFactory.OBJECT);
this.setReturnDataType(DataTypeFactory.XML_STRING);
}
public boolean isAcceptMuleMessage()
{
return this.sourceTypes.contains(MULE_MESSAGE_DATA_TYPE);
}
public void setAcceptMuleMessage(boolean value)
{
if (value)
{
this.registerSourceType(DataTypeFactory.MULE_MESSAGE);
}
else
{
this.unregisterSourceType(DataTypeFactory.MULE_MESSAGE);
}
}
@Override
public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
{
Object src = message.getPayload();
/*
* If the MuleMessage source type has been registered that we can assume that
* the whole message is to be serialised to Xml, not just the payload. This
* can be useful for protocols such as tcp where the protocol does not
* support headers, thus the whole messgae needs to be serialized
*/
if (this.isAcceptMuleMessage())
{
src = message;
}
return this.getXStream().toXML(src);
}
}