View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transformer.simple;
8   
9   import org.mule.api.transformer.TransformerException;
10  import org.mule.transformer.AbstractTransformer;
11  import org.mule.transformer.types.DataTypeFactory;
12  import org.mule.util.IOUtils;
13  import org.mule.util.StringUtils;
14  
15  import java.io.InputStream;
16  import java.io.UnsupportedEncodingException;
17  
18  /**
19   * <code>StringToObjectArray</code> converts a String into an object array. This
20   * is useful in certain situations, as when a string needs to be converted into
21   * an Object[] in order to be passed to a SOAP service. The input String is parsed
22   * into the array based on a configurable delimiter - default is a space.
23   */
24  public class StringToObjectArray extends AbstractTransformer
25  {
26      private String delimiter = null;
27      private static final String DEFAULT_DELIMITER = " ";
28  
29      public StringToObjectArray()
30      {
31          registerSourceType(DataTypeFactory.STRING);
32          registerSourceType(DataTypeFactory.BYTE_ARRAY);
33          registerSourceType(DataTypeFactory.INPUT_STREAM);
34          setReturnDataType(DataTypeFactory.create(Object[].class));
35      }
36  
37      @Override
38      public Object doTransform(Object src, String outputEncoding) throws TransformerException
39      {
40          String in;
41  
42          if (src instanceof byte[])
43          {
44              in = createStringFromByteArray((byte[]) src, outputEncoding);
45          }
46          else if (src instanceof InputStream)
47          {
48              in = createStringFromInputStream((InputStream) src);
49          }
50          else
51          {
52              in = (String) src;
53          }
54  
55          String[] out = StringUtils.splitAndTrim(in, getDelimiter());
56          return out;
57      }
58  
59      protected String createStringFromByteArray(byte[] bytes, String outputEncoding) throws TransformerException
60      {
61          try
62          {
63              return new String(bytes, outputEncoding);
64          }
65          catch (UnsupportedEncodingException uee)
66          {
67              throw new TransformerException(this, uee);
68          }
69      }
70  
71      protected String createStringFromInputStream(InputStream input)
72      {
73          try
74          {
75              return IOUtils.toString(input);
76          }
77          finally
78          {
79              IOUtils.closeQuietly(input);
80          }
81      }
82  
83      /**
84       * @return the delimiter
85       */
86      public String getDelimiter()
87      {
88          if (delimiter == null)
89          {
90              return DEFAULT_DELIMITER;
91          }
92          else
93          {
94              return delimiter;
95          }
96      }
97  
98      /**
99       * @param delimiter the delimiter
100      */
101     public void setDelimiter(String delimiter)
102     {
103         this.delimiter = delimiter;
104     }
105 
106 }