View Javadoc

1   /*
2    * $Id: ObjectArrayToString.java 7963 2007-08-21 08:53:15Z dirk.olmes $
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.transformers.simple;
12  
13  import org.mule.transformers.AbstractTransformer;
14  import org.mule.umo.transformer.TransformerException;
15  import org.mule.util.StringUtils;
16  
17  /**
18   * <code>ObjectArrayToString</code> transformer is the opposite of 
19   * StringToObjectArray - it simply converts Object[] to a String in which each
20   * element is separated by a configurable delimiter (default is a space).
21   */
22  
23  public class ObjectArrayToString extends AbstractTransformer
24  {
25      private String delimiter = null;
26      private static final String DEFAULT_DELIMITER = " ";
27  
28      public ObjectArrayToString()
29      {
30          registerSourceType(Object[].class);
31          setReturnClass(String.class);
32      }
33  
34      public Object doTransform(Object src, String encoding) throws TransformerException
35      {
36          if (src == null) return src;
37  
38          Object[] in = (Object[])src;
39          String out = StringUtils.join(in, getDelimiter());
40  
41          /*
42          for (int i = 0; i < in.length; i++)
43          {
44              if (in[i] != null)
45              {
46                  if (i > 0) out += getDelimiter();
47                  out += in[i].toString();
48              }
49          }
50          */
51  
52          return out;
53      }
54  
55      /**
56       * @return the delimiter
57       */
58      public String getDelimiter()
59      {
60          if (delimiter == null)
61              return DEFAULT_DELIMITER;
62          else
63              return delimiter;
64      }
65  
66      /**
67       * @param sets the delimiter
68       */
69      public void setDelimiter(String delimiter)
70      {
71          this.delimiter = delimiter;
72      }
73          
74  }