Coverage Report - org.mule.transformers.xml.ObjectToXml
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectToXml
100%
12/12
81%
13/16
1.5
 
 1  
 /*
 2  
  * $Id: ObjectToXml.java 10086 2007-12-14 10:25:20Z holger $
 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 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.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 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>UMOMessage</code> is configured as a source type on this
 22  
  * transformer by calling <code>setAcceptUMOMessage(true)</code> then the UMOMessage
 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  20
     {
 32  26
         this.registerSourceType(Object.class);
 33  20
         this.setReturnClass(String.class);
 34  20
     }
 35  
 
 36  
     public boolean isAcceptUMOMessage()
 37  
     {
 38  16
         return this.isSourceTypeSupported(UMOMessage.class, true);
 39  
     }
 40  
 
 41  
     public void setAcceptUMOMessage(boolean value)
 42  
     {
 43  12
         if (value)
 44  
         {
 45  10
             this.registerSourceType(UMOMessage.class);
 46  
         }
 47  
         else
 48  
         {
 49  2
             this.unregisterSourceType(UMOMessage.class);
 50  
         }
 51  12
     }
 52  
 
 53  
     public Object transform(Object src, String encoding, UMOEventContext context) throws TransformerException
 54  
     {
 55  
         /*
 56  
          * If the UMOMessage source type has been registered that we can assume that
 57  
          * the whole message is to be serialised to Xml, not just the payload. This
 58  
          * can be useful for protocols such as tcp where the protocol does not
 59  
          * support headers, thus the whole messgae needs to be serialized
 60  
          */
 61  12
         if (this.isAcceptUMOMessage() && context != null)
 62  
         {
 63  6
             src = context.getMessage();
 64  
         }
 65  
 
 66  12
         return this.getXStream().toXML(src);
 67  
     }
 68  
 }