@Override
public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
{
try
{
XQPreparedExpression transformer = null;
try
{
transformer = (XQPreparedExpression) transformerPool.borrowObject();
bindParameters(transformer, message);
bindDocument(message.getPayload(), transformer);
XQResultSequence result = transformer.executeQuery();
//No support for return Arrays yet
List results = new ArrayList();
while (result.next())
{
XQItem item = result.getItem();
Class type = returnType.getType();
if (Node.class.isAssignableFrom(type) || Node[].class.isAssignableFrom(type))
{
results.add(item.getNode());
}
else if (String.class.isAssignableFrom(type) || String[].class.isAssignableFrom(type))
{
results.add(item.getItemAsString());
}
else if (XMLStreamReader.class.isAssignableFrom(type) || XMLStreamReader[].class.isAssignableFrom(type))
{
try
{
results.add(item.getItemAsStream());
}
catch (XQException e)
{
throw new TransformerException(XmlMessages.streamNotAvailble(getName()));
}
}
else
{
//This can be a JAXB bound object instance depending on whether the CommonHandler has been set
try
{
results.add(item.getObject());
}
catch (XQException e)
{
throw new TransformerException(XmlMessages.objectNotAvailble(getName()));
}
}
if (!type.isArray())
{
break;
}
}
if (returnType.getType().isArray())
{
return results.toArray();
}
if (results.size() == 1)
{
return results.get(0);
}
else if (results.size() == 0)
{
return null;
}
else
{
return results.toArray();
}
}
finally
{
if (transformer != null)
{
if (transformer.getWarnings() != null)
{
logger.warn(transformer.getWarnings().getMessage(), transformer.getWarnings().fillInStackTrace());
}
// clear transformation parameters before returning transformer to the
// pool
//TODO find out what the scope is for bound variables, there doesn't seem to be a way to unbind them