Coverage Report - org.mule.transformer.simple.StringToObjectArray
 
Classes in this File Line Coverage Branch Coverage Complexity
StringToObjectArray
0%
0/23
0%
0/6
0
 
 1  
 /*
 2  
  * $Id: StringToObjectArray.java 19250 2010-08-30 16:53:14Z 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  
 
 21  
 /**
 22  
  * <code>StringToObjectArray</code> converts a String into an object array. This
 23  
  * is useful in certain situations, as when a string needs to be converted into
 24  
  * an Object[] in order to be passed to a SOAP service. The input String is parsed
 25  
  * into the array based on a configurable delimiter - default is a space.
 26  
  */
 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 encoding) throws TransformerException
 43  
     {
 44  
         String in;
 45  
 
 46  0
         if (src instanceof byte[])
 47  
         {
 48  0
             in = new String((byte[])src);
 49  
         }
 50  0
         else if (src instanceof InputStream)
 51  
         {
 52  0
             InputStream input = (InputStream) src;
 53  
             try
 54  
             {
 55  0
                 in = IOUtils.toString(input);
 56  
             }
 57  
             finally
 58  
             {
 59  0
                 IOUtils.closeQuietly(input);
 60  0
             }
 61  0
         }
 62  
         else
 63  
         {
 64  0
             in = (String)src;
 65  
         }
 66  
 
 67  0
         String[] out = StringUtils.splitAndTrim(in, getDelimiter());
 68  0
         return out;
 69  
     }
 70  
 
 71  
     /**
 72  
      * @return the delimiter
 73  
      */
 74  
     public String getDelimiter()
 75  
     {
 76  0
         if (delimiter == null)
 77  
         {
 78  0
             return DEFAULT_DELIMITER;
 79  
         }
 80  
         else
 81  
         {
 82  0
             return delimiter;
 83  
         }
 84  
     }
 85  
 
 86  
     /**
 87  
      * @param delimiter the delimiter
 88  
      */
 89  
     public void setDelimiter(String delimiter)
 90  
     {
 91  0
         this.delimiter = delimiter;
 92  0
     }
 93  
         
 94  
 }