KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > transformers > xml > ObjectToXml


1 /*
2  * $Id: ObjectToXml.java 3937 2006-11-20 16:04:25Z lajos $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.transformers.xml;
12
13 import org.mule.umo.UMOEventContext;
14 import org.mule.umo.UMOMessage;
15 import org.mule.umo.transformer.TransformerException;
16
17 /**
18  * <code>ObjectToXml</code> converts any object to xml using Xstream. Xstream uses
19  * some cleaver 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>UMOMessage</code> is added as a source type on this
22  * transformer then the UMOMessage will be serialised. This is useful for transports
23  * such as tcp where the message headers would normally be lost.
24  */

25
26 public class ObjectToXml extends AbstractXStreamTransformer
27 {
28     /**
29      * Serial version
30      */

31     private static final long serialVersionUID = 2231326110980980434L;
32
33     public ObjectToXml()
34     {
35         registerSourceType(Object JavaDoc.class);
36     }
37
38     public Object JavaDoc transform(Object JavaDoc src, String JavaDoc encoding, UMOEventContext context) throws TransformerException
39     {
40         // If the UMOMessage source type has been registered that we can assume that
41
// the
42
// whole message is to be serialised to Xml, nit just the payload. This can
43
// be useful
44
// for protocols such as tcp where the protocol does not support headers,
45
// thus the whole messgae
46
// needs to be serialized
47
if (isSourceTypeSupported(UMOMessage.class, true) && context != null)
48         {
49             return getXStream().toXML(context.getMessage());
50         }
51         else
52         {
53             return getXStream().toXML(src);
54         }
55     }
56 }
57
Popular Tags