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