View Javadoc

1   /*
2    * $Id: XmlToOutputHandler.java 12316 2008-07-12 20:24:30Z dandiep $
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  package org.mule.module.xml.transformer;
11  
12  import org.mule.api.MuleEvent;
13  import org.mule.api.transformer.DiscoverableTransformer;
14  import org.mule.api.transformer.TransformerException;
15  import org.mule.api.transport.OutputHandler;
16  import org.mule.module.xml.util.XMLUtils;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
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(String.class);
38          registerSourceType(byte[].class);
39          registerSourceType(Source.class);
40          registerSourceType(Document.class);
41          registerSourceType(org.w3c.dom.Document.class);
42          registerSourceType(org.w3c.dom.Element.class);
43          registerSourceType(InputStream.class);
44          registerSourceType(OutputHandler.class);
45          registerSourceType(XMLStreamReader.class);
46          registerSourceType(DelayedResult.class);
47          setReturnClass(OutputHandler.class);
48      }
49  
50      public Object doTransform(final Object src, final String encoding) throws TransformerException
51      {
52          return new OutputHandler()
53          {
54              public void write(MuleEvent event, OutputStream out) throws IOException
55              {
56                  writeXml(src, encoding, out);
57              }
58          };
59      }
60  
61      protected void writeXml(final Object src, final String encoding, OutputStream out)
62          throws TransformerFactoryConfigurationError, IOException
63      {
64          try
65          {
66              if (src instanceof XMLStreamReader)
67              {
68                  // Unfortunately, the StAX source doesn't copy/serialize correctly so 
69                  // we have to do this little hack.
70                  XMLStreamReader reader = (XMLStreamReader)src;
71                  XMLStreamWriter writer = getXMLOutputFactory().createXMLStreamWriter(out);
72                  
73                  try {
74                      writer.writeStartDocument();
75                      XMLUtils.copy(reader, writer);
76                      writer.writeEndDocument();
77                  } finally {
78                      writer.close();
79                      reader.close();
80                  }
81              }
82              else if (src instanceof DelayedResult)
83              {
84                  DelayedResult result = (DelayedResult) src;
85                  
86                  StreamResult streamResult = new StreamResult(out);
87                  result.write(streamResult);
88              }
89              else
90              {
91                  writeToStream(src, encoding, out);
92              }
93          }
94          catch (Exception e)
95          {
96              IOException ioe = new IOException(e.toString());
97              ioe.initCause(e);
98              throw ioe;
99          }
100     }
101 
102     public int getPriorityWeighting()
103     {
104         return priorityWeighting;
105     }
106 
107     public void setPriorityWeighting(int priorityWeighting)
108     {
109         this.priorityWeighting = priorityWeighting;
110     }
111     
112 }