View Javadoc
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.MuleMessage;
10  import org.mule.api.transformer.DiscoverableTransformer;
11  import org.mule.api.transformer.TransformerException;
12  import org.mule.transformer.types.DataTypeFactory;
13  
14  /**
15   * <code>DomDocumentToXml</code> Transform a org.w3c.dom.Document to XML String
16   */
17  public class DomDocumentToXml extends AbstractXmlTransformer implements DiscoverableTransformer
18  {
19      private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING;
20  
21      public DomDocumentToXml()
22      {
23          setReturnDataType(DataTypeFactory.XML_STRING);
24      }
25  
26      @Override
27      public Object transformMessage(MuleMessage message, String encoding) throws TransformerException
28      {
29          Object src = message.getPayload();
30          try
31          {
32              // We now offer XML in byte OR String form.
33              // String remains the default like before.
34              if (byte[].class.equals(returnType))
35              {
36                  return convertToBytes(src, encoding);
37              }
38              else
39              {
40                  return convertToText(src, encoding);
41              }
42          }
43          catch (Exception e)
44          {
45              throw new TransformerException(this, e);
46          }
47      }
48  
49      public int getPriorityWeighting()
50      {
51          return priorityWeighting;
52      }
53  
54      public void setPriorityWeighting(int priorityWeighting)
55      {
56          this.priorityWeighting = priorityWeighting;
57      }
58  }