Coverage Report - org.mule.transformer.simple.StringToObjectArray
 
Classes in this File Line Coverage Branch Coverage Complexity
StringToObjectArray
0%
0/24
0%
0/6
0
 
 1  
 /*
 2  
  * $Id: StringToObjectArray.java 20377 2010-11-29 08:58:19Z dirk.olmes $
 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  
 
 11  
 package org.mule.transformer.simple;
 12  
 
 13  
 import org.mule.api.transformer.TransformerException;
 14  
 import org.mule.transformer.AbstractTransformer;
 15  
 import org.mule.transformer.types.DataTypeFactory;
 16  
 import org.mule.util.IOUtils;
 17  
 import org.mule.util.StringUtils;
 18  
 
 19  
 import java.io.InputStream;
 20  
 import java.io.UnsupportedEncodingException;
 21  
 
 22  
 /**
 23  
  * <code>StringToObjectArray</code> converts a String into an object array. This
 24  
  * is useful in certain situations, as when a string needs to be converted into
 25  
  * an Object[] in order to be passed to a SOAP service. The input String is parsed
 26  
  * into the array based on a configurable delimiter - default is a space.
 27  
  */
 28  
 public class StringToObjectArray extends AbstractTransformer
 29  
 {
 30  0
     private String delimiter = null;
 31  
     private static final String DEFAULT_DELIMITER = " ";
 32  
 
 33  
     public StringToObjectArray()
 34  0
     {
 35  0
         registerSourceType(DataTypeFactory.STRING);
 36  0
         registerSourceType(DataTypeFactory.BYTE_ARRAY);
 37  0
         registerSourceType(DataTypeFactory.INPUT_STREAM);
 38  0
         setReturnDataType(DataTypeFactory.create(Object[].class));
 39  0
     }
 40  
 
 41  
     @Override
 42  
     public Object doTransform(Object src, String outputEncoding) throws TransformerException
 43  
     {
 44  
         String in;
 45  
 
 46  0
         if (src instanceof byte[])
 47  
         {
 48  0
             in = createStringFromByteArray((byte[]) src, outputEncoding);
 49  
         }
 50  0
         else if (src instanceof InputStream)
 51  
         {
 52  0
             in = createStringFromInputStream((InputStream) src);
 53  
         }
 54  
         else
 55  
         {
 56  0
             in = (String) src;
 57  
         }
 58  
 
 59  0
         String[] out = StringUtils.splitAndTrim(in, getDelimiter());
 60  0
         return out;
 61  
     }
 62  
 
 63  
     protected String createStringFromByteArray(byte[] bytes, String outputEncoding) throws TransformerException
 64  
     {
 65  
         try
 66  
         {
 67  0
             return new String(bytes, outputEncoding);
 68  
         }
 69  0
         catch (UnsupportedEncodingException uee)
 70  
         {
 71  0
             throw new TransformerException(this, uee);
 72  
         }
 73  
     }
 74  
 
 75  
     protected String createStringFromInputStream(InputStream input)
 76  
     {
 77  
         try
 78  
         {
 79  0
             return IOUtils.toString(input);
 80  
         }
 81  
         finally
 82  
         {
 83  0
             IOUtils.closeQuietly(input);
 84  
         }
 85  
     }
 86  
 
 87  
     /**
 88  
      * @return the delimiter
 89  
      */
 90  
     public String getDelimiter()
 91  
     {
 92  0
         if (delimiter == null)
 93  
         {
 94  0
             return DEFAULT_DELIMITER;
 95  
         }
 96  
         else
 97  
         {
 98  0
             return delimiter;
 99  
         }
 100  
     }
 101  
 
 102  
     /**
 103  
      * @param delimiter the delimiter
 104  
      */
 105  
     public void setDelimiter(String delimiter)
 106  
     {
 107  0
         this.delimiter = delimiter;
 108  0
     }
 109  
 
 110  
 }