View Javadoc

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      private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING;
34  
35      public XmlToOutputHandler()
36      {
37          registerSourceType(DataTypeFactory.STRING);
38          registerSourceType(DataTypeFactory.BYTE_ARRAY);
39          registerSourceType(DataTypeFactory.create(Source.class));
40          registerSourceType(DataTypeFactory.create(Document.class));
41          registerSourceType(DataTypeFactory.create(org.w3c.dom.Document.class));
42          registerSourceType(DataTypeFactory.create(org.w3c.dom.Element.class));
43          registerSourceType(DataTypeFactory.INPUT_STREAM);
44          registerSourceType(DataTypeFactory.create(OutputHandler.class));
45          registerSourceType(DataTypeFactory.create(XMLStreamReader.class));
46          registerSourceType(DataTypeFactory.create(DelayedResult.class));
47          setReturnDataType(DataTypeFactory.create(OutputHandler.class));
48      }
49  
50      @Override
51      public Object transformMessage(MuleMessage message, final String encoding)
52      {
53          final Object src = message.getPayload();
54          return new OutputHandler()
55          {
56              public void write(MuleEvent event, OutputStream out) throws IOException
57              {
58                  writeXml(src, encoding, out);
59              }
60          };
61      }
62  
63      protected void writeXml(final Object src, final String encoding, OutputStream out)
64          throws TransformerFactoryConfigurationError, IOException
65      {
66          try
67          {
68              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                  XMLStreamReader reader = (XMLStreamReader)src;
73                  XMLStreamWriter writer = getXMLOutputFactory().createXMLStreamWriter(out);
74                  
75                  try {
76                      writer.writeStartDocument();
77                      XMLUtils.copy(reader, writer);
78                      writer.writeEndDocument();
79                  } finally {
80                      writer.close();
81                      reader.close();
82                  }
83              }
84              else if (src instanceof DelayedResult)
85              {
86                  DelayedResult result = (DelayedResult) src;
87                  
88                  StreamResult streamResult = new StreamResult(out);
89                  result.write(streamResult);
90              }
91              else
92              {
93                  writeToStream(src, encoding, out);
94              }
95          }
96          catch (Exception e)
97          {
98              IOException ioe = new IOException(e.toString());
99              ioe.initCause(e);
100             throw ioe;
101         }
102     }
103 
104     public int getPriorityWeighting()
105     {
106         return priorityWeighting;
107     }
108 
109     public void setPriorityWeighting(int priorityWeighting)
110     {
111         this.priorityWeighting = priorityWeighting;
112     }
113     
114 }