Coverage Report - org.mule.module.xml.transformer.XmlToOutputHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlToOutputHandler
0%
0/41
0%
0/4
0
XmlToOutputHandler$1
0%
0/3
N/A
0
 
 1  
 /*
 2  
  * $Id: XmlToOutputHandler.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  
 package org.mule.module.xml.transformer;
 11  
 
 12  
 import org.mule.api.MuleEvent;
 13  
 import org.mule.api.MuleMessage;
 14  
 import org.mule.api.transformer.DiscoverableTransformer;
 15  
 import org.mule.api.transport.OutputHandler;
 16  
 import org.mule.module.xml.util.XMLUtils;
 17  
 import org.mule.transformer.types.DataTypeFactory;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.OutputStream;
 21  
 
 22  
 import javax.xml.stream.XMLStreamReader;
 23  
 import javax.xml.stream.XMLStreamWriter;
 24  
 import javax.xml.transform.Source;
 25  
 import javax.xml.transform.TransformerFactoryConfigurationError;
 26  
 import javax.xml.transform.stream.StreamResult;
 27  
 
 28  
 import org.dom4j.Document;
 29  
 
 30  
 public class XmlToOutputHandler extends AbstractXmlTransformer implements DiscoverableTransformer
 31  
 {
 32  
 
 33  0
     private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING;
 34  
 
 35  
     public XmlToOutputHandler()
 36  0
     {
 37  0
         registerSourceType(DataTypeFactory.STRING);
 38  0
         registerSourceType(DataTypeFactory.BYTE_ARRAY);
 39  0
         registerSourceType(DataTypeFactory.create(Source.class));
 40  0
         registerSourceType(DataTypeFactory.create(Document.class));
 41  0
         registerSourceType(DataTypeFactory.create(org.w3c.dom.Document.class));
 42  0
         registerSourceType(DataTypeFactory.create(org.w3c.dom.Element.class));
 43  0
         registerSourceType(DataTypeFactory.INPUT_STREAM);
 44  0
         registerSourceType(DataTypeFactory.create(OutputHandler.class));
 45  0
         registerSourceType(DataTypeFactory.create(XMLStreamReader.class));
 46  0
         registerSourceType(DataTypeFactory.create(DelayedResult.class));
 47  0
         setReturnDataType(DataTypeFactory.create(OutputHandler.class));
 48  0
     }
 49  
 
 50  
     @Override
 51  
     public Object transformMessage(MuleMessage message, final String encoding)
 52  
     {
 53  0
         final Object src = message.getPayload();
 54  0
         return new OutputHandler()
 55  0
         {
 56  
             public void write(MuleEvent event, OutputStream out) throws IOException
 57  
             {
 58  0
                 writeXml(src, encoding, out);
 59  0
             }
 60  
         };
 61  
     }
 62  
 
 63  
     protected void writeXml(final Object src, final String encoding, OutputStream out)
 64  
         throws TransformerFactoryConfigurationError, IOException
 65  
     {
 66  
         try
 67  
         {
 68  0
             if (src instanceof XMLStreamReader)
 69  
             {
 70  
                 // Unfortunately, the StAX source doesn't copy/serialize correctly so
 71  
                 // we have to do this little hack.
 72  0
                 XMLStreamReader reader = (XMLStreamReader)src;
 73  0
                 XMLStreamWriter writer = getXMLOutputFactory().createXMLStreamWriter(out);
 74  
                 
 75  
                 try {
 76  0
                     writer.writeStartDocument();
 77  0
                     XMLUtils.copy(reader, writer);
 78  0
                     writer.writeEndDocument();
 79  
                 } finally {
 80  0
                     writer.close();
 81  0
                     reader.close();
 82  0
                 }
 83  0
             }
 84  0
             else if (src instanceof DelayedResult)
 85  
             {
 86  0
                 DelayedResult result = (DelayedResult) src;
 87  
                 
 88  0
                 StreamResult streamResult = new StreamResult(out);
 89  0
                 result.write(streamResult);
 90  0
             }
 91  
             else
 92  
             {
 93  0
                 writeToStream(src, encoding, out);
 94  
             }
 95  
         }
 96  0
         catch (Exception e)
 97  
         {
 98  0
             IOException ioe = new IOException(e.toString());
 99  0
             ioe.initCause(e);
 100  0
             throw ioe;
 101  0
         }
 102  0
     }
 103  
 
 104  
     public int getPriorityWeighting()
 105  
     {
 106  0
         return priorityWeighting;
 107  
     }
 108  
 
 109  
     public void setPriorityWeighting(int priorityWeighting)
 110  
     {
 111  0
         this.priorityWeighting = priorityWeighting;
 112  0
     }
 113  
     
 114  
 }