Coverage Report - org.mule.transformer.simple.ObjectToString
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectToString
51%
21/41
67%
4/6
3.5
 
 1  
 /*
 2  
  * $Id: ObjectToString.java 12269 2008-07-10 04:19:03Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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  
 
 11  
 package org.mule.transformer.simple;
 12  
 
 13  
 import org.mule.RequestContext;
 14  
 import org.mule.api.transformer.DiscoverableTransformer;
 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.util.IOUtils;
 20  
 import org.mule.util.StringMessageUtils;
 21  
 
 22  
 import java.io.ByteArrayOutputStream;
 23  
 import java.io.IOException;
 24  
 import java.io.InputStream;
 25  
 import java.io.UnsupportedEncodingException;
 26  
 
 27  
 /**
 28  
  * <code>ObjectToString</code> transformer is useful for debugging. It will return
 29  
  * human-readable output for various kinds of objects. Right now, it is just coded to
 30  
  * handle Map and Collection objects. Others will be added.
 31  
  */
 32  
 public class ObjectToString extends AbstractTransformer implements DiscoverableTransformer
 33  
 {
 34  
     protected static final int DEFAULT_BUFFER_SIZE = 80;
 35  
 
 36  
     /** Give core transformers a slighty higher priority */
 37  1164
     private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING + 1;
 38  
 
 39  
     public ObjectToString()
 40  1164
     {
 41  1164
         registerSourceType(Object.class);
 42  1164
         registerSourceType(byte[].class);
 43  1164
         registerSourceType(InputStream.class);
 44  1164
         registerSourceType(OutputHandler.class);
 45  1164
         setReturnClass(String.class);
 46  1164
     }
 47  
 
 48  
     public Object doTransform(Object src, String encoding) throws TransformerException
 49  
     {
 50  12
         String output = "";
 51  
 
 52  12
         if (src instanceof InputStream)
 53  
         {
 54  2
             InputStream is = (InputStream) src;
 55  
             try
 56  
             {
 57  2
                 ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
 58  2
                 IOUtils.copy(is, byteOut);
 59  2
                 output = new String(byteOut.toByteArray(), encoding);
 60  
             }
 61  0
             catch (IOException e)
 62  
             {
 63  0
                 throw new TransformerException(CoreMessages.errorReadingStream(), e);
 64  
             }
 65  
             finally
 66  
             {
 67  0
                 try
 68  
                 {
 69  2
                     is.close();
 70  
                 }
 71  0
                 catch (IOException e)
 72  
                 {
 73  0
                     logger.warn("Could not close stream", e);
 74  2
                 }
 75  0
             }
 76  2
         }
 77  10
         else if (src instanceof OutputHandler)
 78  
         {
 79  0
             ByteArrayOutputStream bytes = new ByteArrayOutputStream();
 80  
             
 81  
             try
 82  
             {
 83  0
                 ((OutputHandler) src).write(RequestContext.getEvent(), bytes);
 84  
                 
 85  0
                 output = new String(bytes.toByteArray(), encoding);
 86  
             }
 87  0
             catch (IOException e)
 88  
             {
 89  0
                 throw new TransformerException(this, e);
 90  0
             }
 91  
             
 92  
             
 93  0
         }
 94  10
         else if (src instanceof byte[])
 95  
         {
 96  
             try
 97  
             {
 98  0
                 output = new String((byte[]) src, encoding);
 99  
             }
 100  0
             catch (UnsupportedEncodingException e)
 101  
             {
 102  0
                 throw new TransformerException(this, e);
 103  0
             }
 104  
         }
 105  
         else
 106  
         {
 107  10
             output = StringMessageUtils.toString(src);
 108  
         }
 109  
 
 110  12
         return output;
 111  
     }
 112  
 
 113  
     public int getPriorityWeighting()
 114  
     {
 115  0
         return priorityWeighting;
 116  
     }
 117  
 
 118  
     public void setPriorityWeighting(int priorityWeighting)
 119  
     {
 120  0
         this.priorityWeighting = priorityWeighting;
 121  0
     }
 122  
 }