View Javadoc

1   /*
2    * $Id: JAXBUnmarshallerTransformer.java 22556 2011-07-25 21:01:33Z 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.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
37   * the returnType for this transformer is {@link Object}. If a specific returnType is
38   * set and no external {@link javax.xml.bind.JAXBContext} is set on the transformer,
39   * then a {@link javax.xml.bind.JAXBContext} will be created using the returnType.
40   *
41   * @since 3.0
42   */
43  public class JAXBUnmarshallerTransformer extends AbstractTransformer
44  {
45      protected JAXBContext jaxbContext;
46  
47      public JAXBUnmarshallerTransformer()
48      {
49          registerSourceType(DataTypeFactory.STRING);
50          registerSourceType(DataTypeFactory.create(Writer.class));
51          registerSourceType(DataTypeFactory.create(File.class));
52          registerSourceType(DataTypeFactory.create(URL.class));
53          registerSourceType(DataTypeFactory.create(Node.class));
54          registerSourceType(DataTypeFactory.INPUT_STREAM);
55          registerSourceType(DataTypeFactory.create(Source.class));
56          registerSourceType(DataTypeFactory.create(XMLStreamReader.class));
57          registerSourceType(DataTypeFactory.create(XMLEventReader.class));
58      }
59  
60      public JAXBUnmarshallerTransformer(JAXBContext jaxbContext, DataType<?> returnType)
61      {
62          this();
63          this.jaxbContext = jaxbContext;
64          setReturnDataType(returnType);
65      }
66  
67      @Override
68      public void initialise() throws InitialisationException
69      {
70          super.initialise();
71          if (jaxbContext == null)
72          {
73              if(Object.class.equals(getReturnDataType().getType()))
74              {
75                  throw new InitialisationException(CoreMessages.objectIsNull("jaxbContext"), this);
76              }
77              else
78              {
79                  try
80                  {
81                      jaxbContext = JAXBContext.newInstance(getReturnDataType().getType());
82                  }
83                  catch (JAXBException e)
84                  {
85                      throw new InitialisationException(e, this);
86                  }
87              }
88          }
89      }
90  
91      @Override
92      protected Object doTransform(Object src, String outputEncoding) throws TransformerException
93      {
94          try
95          {
96              final Unmarshaller u = jaxbContext.createUnmarshaller();
97              Object result = null;
98              if (src instanceof String)
99              {
100                 result = u.unmarshal(new StringReader((String) src));
101             }
102             else if (src instanceof File)
103             {
104                 result = u.unmarshal((File) src);
105             }
106             else if (src instanceof URL)
107             {
108                 result = u.unmarshal((URL) src);
109             }
110             else if (src instanceof InputStream)
111             {
112                 result = u.unmarshal((InputStream) src);
113             }
114             else if (src instanceof Node)
115             {
116                 result = u.unmarshal((Node) src, getReturnDataType().getType());
117             }
118             else if (src instanceof Source)
119             {
120                 result = u.unmarshal((Source) src, getReturnDataType().getType());
121             }
122             else if (src instanceof XMLStreamReader)
123             {
124                 result = u.unmarshal((XMLStreamReader) src, getReturnDataType().getType());
125             }
126             else if (src instanceof XMLEventReader)
127             {
128                 result = u.unmarshal((XMLEventReader) src, getReturnDataType().getType());
129             }
130             if (result != null)
131             {
132                 // If we get a JAXB element, return its contents
133                 if (result instanceof JAXBElement)
134                 {
135                     result = ((JAXBElement)result).getValue();
136                 }
137             }
138             return result;
139         }
140         catch (Exception e)
141         {
142             throw new TransformerException(this, e);
143         }
144     }
145 
146     public JAXBContext getJaxbContext()
147     {
148         return jaxbContext;
149     }
150 
151     public void setJaxbContext(JAXBContext jaxbContext)
152     {
153         this.jaxbContext = jaxbContext;
154     }
155 }