KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: XmlToObject.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 java.io.ByteArrayInputStream JavaDoc;
14
15 import org.mule.umo.UMOEventContext;
16 import org.mule.umo.transformer.TransformerException;
17
18 /**
19  * <code>XmlToObject</code> converts xml created by the ObjectToXml transformer in
20  * to a java object graph. This transformer uses XStream. Xstream uses some cleaver
21  * tricks so objects that get marshalled to xml do not need to implement any
22  * interfaces including Serializable and you don't even need to specify a default
23  * constructor.
24  *
25  * @see org.mule.transformers.xml.ObjectToXml
26  */

27
28 public class XmlToObject extends AbstractXStreamTransformer
29 {
30     /**
31      * Serial version
32      */

33     private static final long serialVersionUID = -2804761502812600068L;
34
35     private final DomDocumentToXml domTransformer = new DomDocumentToXml();
36
37     public XmlToObject()
38     {
39         registerSourceType(String JavaDoc.class);
40         registerSourceType(byte[].class);
41         registerSourceType(org.w3c.dom.Document JavaDoc.class);
42         registerSourceType(org.dom4j.Document.class);
43     }
44
45     public Object JavaDoc transform(Object JavaDoc src, String JavaDoc encoding, UMOEventContext context) throws TransformerException
46     {
47
48         if (src instanceof byte[])
49         {
50             return getXStream().fromXML(new ByteArrayInputStream JavaDoc((byte[])src));
51         }
52         else if (src instanceof String JavaDoc)
53         {
54             return getXStream().fromXML(src.toString());
55         }
56         else
57         {
58             return getXStream().fromXML((String JavaDoc)domTransformer.transform(src));
59         }
60     }
61 }
62
Popular Tags