Coverage Report - org.mule.module.xml.transformer.jaxb.JAXBUnmarshallerTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
JAXBUnmarshallerTransformer
0%
0/52
0%
0/20
0
 
 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  0
     {
 48  0
         registerSourceType(DataTypeFactory.STRING);
 49  0
         registerSourceType(DataTypeFactory.create(Writer.class));
 50  0
         registerSourceType(DataTypeFactory.create(File.class));
 51  0
         registerSourceType(DataTypeFactory.create(URL.class));
 52  0
         registerSourceType(DataTypeFactory.create(Node.class));
 53  0
         registerSourceType(DataTypeFactory.INPUT_STREAM);
 54  0
         registerSourceType(DataTypeFactory.create(Source.class));
 55  0
         registerSourceType(DataTypeFactory.create(XMLStreamReader.class));
 56  0
         registerSourceType(DataTypeFactory.create(XMLEventReader.class));
 57  0
     }
 58  
 
 59  
     public JAXBUnmarshallerTransformer(JAXBContext jaxbContext, DataType returnType)
 60  
     {
 61  0
         this();
 62  0
         this.jaxbContext = jaxbContext;
 63  0
         setReturnDataType(returnType);
 64  0
     }
 65  
 
 66  
     @Override
 67  
     public void initialise() throws InitialisationException
 68  
     {
 69  0
         super.initialise();
 70  0
         if (jaxbContext == null)
 71  
         {
 72  0
             if(Object.class.equals(getReturnDataType().getType()))
 73  
             {
 74  0
                 throw new InitialisationException(CoreMessages.objectIsNull("jaxbContext"), this);
 75  
             }
 76  
             else
 77  
             {
 78  
                 try
 79  
                 {
 80  0
                     jaxbContext = JAXBContext.newInstance(getReturnDataType().getType());
 81  
                 }
 82  0
                 catch (JAXBException e)
 83  
                 {
 84  0
                     throw new InitialisationException(e, this);
 85  0
                 }
 86  
             }
 87  
         }
 88  0
     }
 89  
 
 90  
     @Override
 91  
     protected Object doTransform(Object src, String encoding) throws TransformerException
 92  
     {
 93  
         try
 94  
         {
 95  0
             final Unmarshaller u = jaxbContext.createUnmarshaller();
 96  0
             if (src instanceof String)
 97  
             {
 98  0
                 return u.unmarshal(new StringReader((String) src));
 99  
             }
 100  0
             else if (src instanceof File)
 101  
             {
 102  0
                 return u.unmarshal((File) src);
 103  
             }
 104  0
             else if (src instanceof URL)
 105  
             {
 106  0
                 return u.unmarshal((URL) src);
 107  
             }
 108  0
             else if (src instanceof InputStream)
 109  
             {
 110  0
                 return u.unmarshal((InputStream) src);
 111  
             }
 112  0
             else if (src instanceof Node)
 113  
             {
 114  0
                 JAXBElement e = u.unmarshal((Node) src, getReturnClass());
 115  0
                 return e.getValue();
 116  
             }
 117  0
             else if (src instanceof Source)
 118  
             {
 119  0
                 JAXBElement e = u.unmarshal((Source) src, getReturnClass());
 120  0
                 return e.getValue();
 121  
             }
 122  0
             else if (src instanceof XMLStreamReader)
 123  
             {
 124  0
                 JAXBElement e = u.unmarshal((XMLStreamReader) src, getReturnClass());
 125  0
                 return e.getValue();
 126  
             }
 127  0
             else if (src instanceof XMLEventReader)
 128  
             {
 129  0
                 JAXBElement e = u.unmarshal((XMLEventReader) src, getReturnClass());
 130  0
                 return e.getValue();
 131  
             }
 132  
 
 133  
         }
 134  0
         catch (Exception e)
 135  
         {
 136  0
             throw new TransformerException(this, e);
 137  0
         }
 138  0
         return null;
 139  
     }
 140  
 
 141  
     public JAXBContext getJaxbContext()
 142  
     {
 143  0
         return jaxbContext;
 144  
     }
 145  
 
 146  
     public void setJaxbContext(JAXBContext jaxbContext)
 147  
     {
 148  0
         this.jaxbContext = jaxbContext;
 149  0
     }
 150  
 }