View Javadoc

1   /*
2    * $Id: JAXBMarshallerTransformer.java 21725 2011-04-26 01:52:03Z mike.schilling $
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.jaxb;
11  
12  import org.mule.api.MuleEvent;
13  import org.mule.api.lifecycle.InitialisationException;
14  import org.mule.api.transformer.DataType;
15  import org.mule.api.transformer.TransformerException;
16  import org.mule.api.transport.OutputHandler;
17  import org.mule.config.i18n.CoreMessages;
18  import org.mule.transformer.AbstractTransformer;
19  import org.mule.transformer.types.DataTypeFactory;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  import java.io.OutputStream;
24  import java.io.StringWriter;
25  import java.io.Writer;
26  
27  import javax.xml.bind.JAXBContext;
28  import javax.xml.bind.JAXBException;
29  import javax.xml.bind.Marshaller;
30  import javax.xml.parsers.DocumentBuilderFactory;
31  
32  import org.w3c.dom.Document;
33  
34  /**
35   * Allows marshaling of Java objects to XML using JAXB 2.  A specific sourceClass can be set on this transformer, this
36   * is the expected source object type.  If no external {@link javax.xml.bind.JAXBContext} is set on the transformer, but
37   * the 'sourceClass' is set, a {@link javax.xml.bind.JAXBContext} will be created using the sourceClass.
38   *
39   * @since 3.0
40   */
41  public class JAXBMarshallerTransformer extends AbstractTransformer
42  {
43      protected JAXBContext jaxbContext;
44  
45      protected Class<?> sourceClass;
46  
47      public JAXBMarshallerTransformer()
48      {
49          setReturnDataType(DataTypeFactory.create(OutputStream.class));
50          registerSourceType(DataTypeFactory.OBJECT);
51      }
52  
53      public JAXBMarshallerTransformer(JAXBContext jaxbContext, DataType returnType)
54      {
55          this();
56          this.jaxbContext = jaxbContext;
57          setReturnDataType(returnType);
58      }
59  
60      @Override
61      public void initialise() throws InitialisationException
62      {
63          super.initialise();
64          if (jaxbContext == null)
65          {
66              throw new InitialisationException(CoreMessages.objectIsNull("jaxbContext"), this);
67          }
68      }
69  
70      @Override
71      protected Object doTransform(final Object src, String encoding) throws TransformerException
72      {
73          try
74          {
75              final Marshaller m = jaxbContext.createMarshaller();
76              if (getReturnClass().equals(String.class))
77              {
78                  Writer w = new StringWriter();
79                  m.marshal(src, w);
80                  return w.toString();
81              }
82              else if (getReturnClass().isAssignableFrom(Writer.class))
83              {
84                  Writer w = new StringWriter();
85                  m.marshal(src, w);
86                  return w;
87              }
88              else if (Document.class.isAssignableFrom(getReturnClass()))
89              {
90                  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
91                  Document doc = factory.newDocumentBuilder().newDocument();
92                  m.marshal(src, doc);
93                  return doc;
94              }
95              else if (OutputStream.class.isAssignableFrom(getReturnClass()))
96              {
97                  ByteArrayOutputStream out = new ByteArrayOutputStream();
98                  m.marshal(src, out);
99                  return out;
100             }
101             else if (OutputHandler.class.equals(getReturnClass()))
102             {
103                 return new OutputHandler()
104                 {
105                     public void write(MuleEvent event, OutputStream out) throws IOException
106                     {
107                         try
108                         {
109                             m.marshal(src, out);
110                         }
111                         catch (JAXBException e)
112                         {
113                             IOException iox = new IOException("failed to mashal objec tto XML");
114                             iox.initCause(e);
115                             throw iox;
116                         }
117                     }
118                 };
119             }
120             else
121             {
122                 throw new TransformerException(CoreMessages.transformerInvalidReturnType(getReturnClass(), getName()));
123             }
124 
125         }
126         catch (Exception e)
127         {
128             throw new TransformerException(this, e);
129         }
130     }
131 
132     public JAXBContext getJaxbContext()
133     {
134         return jaxbContext;
135     }
136 
137     public void setJaxbContext(JAXBContext jaxbContext)
138     {
139         this.jaxbContext = jaxbContext;
140     }
141 
142     public Class<?> getSourceClass()
143     {
144         return sourceClass;
145     }
146 
147     public void setSourceClass(Class<?> sourceClass)
148     {
149         this.sourceClass = sourceClass;
150     }
151 }