Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ObjectToXml |
|
| 0.0;0 |
1 | /* | |
2 | * $Id: ObjectToXml.java 19250 2010-08-30 16:53:14Z dirk.olmes $ | |
3 | * -------------------------------------------------------------------------------------- | |
4 | * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com | |
5 | * | |
6 | * The software in this package is published under the terms of the CPAL v1.0 | |
7 | * license, a copy of which has been included with this distribution in the | |
8 | * LICENSE.txt file. | |
9 | */ | |
10 | ||
11 | package org.mule.module.xml.transformer; | |
12 | ||
13 | import org.mule.api.MuleMessage; | |
14 | import org.mule.api.transformer.TransformerException; | |
15 | import org.mule.transformer.types.DataTypeFactory; | |
16 | ||
17 | /** | |
18 | * <code>ObjectToXml</code> converts any object to XML using Xstream. Xstream uses | |
19 | * some clever tricks so objects that get marshalled to XML do not need to implement | |
20 | * any interfaces including Serializable and you don't even need to specify a default | |
21 | * constructor. If <code>MuleMessage</code> is configured as a source type on this | |
22 | * transformer by calling <code>setAcceptMuleMessage(true)</code> then the MuleMessage | |
23 | * will be serialised. This is useful for transports such as TCP where the message | |
24 | * headers would normally be lost. | |
25 | */ | |
26 | ||
27 | public class ObjectToXml extends AbstractXStreamTransformer | |
28 | { | |
29 | ||
30 | public ObjectToXml() | |
31 | 0 | { |
32 | 0 | this.registerSourceType(DataTypeFactory.OBJECT); |
33 | 0 | this.setReturnDataType(DataTypeFactory.STRING); |
34 | 0 | } |
35 | ||
36 | public boolean isAcceptMuleMessage() | |
37 | { | |
38 | 0 | return this.sourceTypes.contains(MULE_MESSAGE_DATA_TYPE); |
39 | } | |
40 | ||
41 | public void setAcceptMuleMessage(boolean value) | |
42 | { | |
43 | 0 | if (value) |
44 | { | |
45 | 0 | this.registerSourceType(DataTypeFactory.MULE_MESSAGE); |
46 | } | |
47 | else | |
48 | { | |
49 | 0 | this.unregisterSourceType(DataTypeFactory.MULE_MESSAGE); |
50 | } | |
51 | 0 | } |
52 | ||
53 | @Override | |
54 | public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException | |
55 | { | |
56 | 0 | Object src = message.getPayload(); |
57 | /* | |
58 | * If the MuleMessage source type has been registered that we can assume that | |
59 | * the whole message is to be serialised to Xml, not just the payload. This | |
60 | * can be useful for protocols such as tcp where the protocol does not | |
61 | * support headers, thus the whole messgae needs to be serialized | |
62 | */ | |
63 | 0 | if (this.isAcceptMuleMessage()) |
64 | { | |
65 | 0 | src = message; |
66 | } | |
67 | 0 | return this.getXStream().toXML(src); |
68 | } | |
69 | } |