View Javadoc

1   /*
2    * $Id: JAXBUnmarshallerTransformer.java 20321 2010-11-24 15:21:24Z dfeist $
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.lifecycle.InitialisationException;
13  import org.mule.api.transformer.DataType;
14  import org.mule.api.transformer.TransformerException;
15  import org.mule.config.i18n.CoreMessages;
16  import org.mule.transformer.AbstractTransformer;
17  import org.mule.transformer.types.DataTypeFactory;
18  
19  import java.io.File;
20  import java.io.InputStream;
21  import java.io.StringReader;
22  import java.io.Writer;
23  import java.net.URL;
24  
25  import javax.xml.bind.JAXBContext;
26  import javax.xml.bind.JAXBElement;
27  import javax.xml.bind.JAXBException;
28  import javax.xml.bind.Unmarshaller;
29  import javax.xml.stream.XMLEventReader;
30  import javax.xml.stream.XMLStreamReader;
31  import javax.xml.transform.Source;
32  
33  import org.w3c.dom.Node;
34  
35  /**
36   * Allows un-marshaling of XML generated by JAXB to a Java object graph. By default the returnType for this transformer is {@link Object}
37   * If a specific returnType is set and ff no external {@link javax.xml.bind.JAXBContext} is set on the transformer, then a
38   * {@link javax.xml.bind.JAXBContext} will be created using the returnType.
39   *
40   * @since 3.0
41   */
42  public class JAXBUnmarshallerTransformer extends AbstractTransformer
43  {
44      protected JAXBContext jaxbContext;
45  
46      public JAXBUnmarshallerTransformer()
47      {
48          registerSourceType(DataTypeFactory.STRING);
49          registerSourceType(DataTypeFactory.create(Writer.class));
50          registerSourceType(DataTypeFactory.create(File.class));
51          registerSourceType(DataTypeFactory.create(URL.class));
52          registerSourceType(DataTypeFactory.create(Node.class));
53          registerSourceType(DataTypeFactory.INPUT_STREAM);
54          registerSourceType(DataTypeFactory.create(Source.class));
55          registerSourceType(DataTypeFactory.create(XMLStreamReader.class));
56          registerSourceType(DataTypeFactory.create(XMLEventReader.class));
57      }
58  
59      public JAXBUnmarshallerTransformer(JAXBContext jaxbContext, DataType returnType)
60      {
61          this();
62          this.jaxbContext = jaxbContext;
63          setReturnDataType(returnType);
64      }
65  
66      @Override
67      public void initialise() throws InitialisationException
68      {
69          super.initialise();
70          if (jaxbContext == null)
71          {
72              if(Object.class.equals(getReturnDataType().getType()))
73              {
74                  throw new InitialisationException(CoreMessages.objectIsNull("jaxbContext"), this);
75              }
76              else
77              {
78                  try
79                  {
80                      jaxbContext = JAXBContext.newInstance(getReturnDataType().getType());
81                  }
82                  catch (JAXBException e)
83                  {
84                      throw new InitialisationException(e, this);
85                  }
86              }
87          }
88      }
89  
90      @Override
91      protected Object doTransform(Object src, String encoding) throws TransformerException
92      {
93          try
94          {
95              final Unmarshaller u = jaxbContext.createUnmarshaller();
96              if (src instanceof String)
97              {
98                  return u.unmarshal(new StringReader((String) src));
99              }
100             else if (src instanceof File)
101             {
102                 return u.unmarshal((File) src);
103             }
104             else if (src instanceof URL)
105             {
106                 return u.unmarshal((URL) src);
107             }
108             else if (src instanceof InputStream)
109             {
110                 return u.unmarshal((InputStream) src);
111             }
112             else if (src instanceof Node)
113             {
114                 JAXBElement e = u.unmarshal((Node) src, getReturnClass());
115                 return e.getValue();
116             }
117             else if (src instanceof Source)
118             {
119                 JAXBElement e = u.unmarshal((Source) src, getReturnClass());
120                 return e.getValue();
121             }
122             else if (src instanceof XMLStreamReader)
123             {
124                 JAXBElement e = u.unmarshal((XMLStreamReader) src, getReturnClass());
125                 return e.getValue();
126             }
127             else if (src instanceof XMLEventReader)
128             {
129                 JAXBElement e = u.unmarshal((XMLEventReader) src, getReturnClass());
130                 return e.getValue();
131             }
132 
133         }
134         catch (Exception e)
135         {
136             throw new TransformerException(this, e);
137         }
138         return null;
139     }
140 
141     public JAXBContext getJaxbContext()
142     {
143         return jaxbContext;
144     }
145 
146     public void setJaxbContext(JAXBContext jaxbContext)
147     {
148         this.jaxbContext = jaxbContext;
149     }
150 }