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.DiscoverableTransformer;
10  import org.mule.api.transformer.TransformerException;
11  import org.mule.transformer.AbstractTransformer;
12  import org.mule.transformer.types.DataTypeFactory;
13  import org.mule.util.StringUtils;
14  
15  /**
16   * <code>ObjectArrayToString</code> transformer is the opposite of
17   * StringToObjectArray - it simply converts Object[] to a String in which each
18   * element is separated by a configurable delimiter (default is a space).
19   */
20  
21  public class ObjectArrayToString extends AbstractTransformer implements DiscoverableTransformer
22  {
23      /** Give core transformers a slighty higher priority */
24      private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING + 1;
25  
26      private static final String DEFAULT_DELIMITER = " ";
27  
28      private String delimiter = null;
29  
30      public ObjectArrayToString()
31      {
32          registerSourceType(DataTypeFactory.create(Object[].class));
33          setReturnDataType(DataTypeFactory.TEXT_STRING);
34      }
35  
36      @Override
37      public Object doTransform(Object src, String encoding) throws TransformerException
38      {
39          if (src == null)
40          {
41              return src;
42          }
43  
44          Object[] in = (Object[]) src;
45          String out = StringUtils.join(in, getDelimiter());
46  
47          /*
48          for (int i = 0; i < in.length; i++)
49          {
50              if (in[i] != null)
51              {
52                  if (i > 0) out += getDelimiter();
53                  out += in[i].toString();
54              }
55          }
56          */
57  
58          return out;
59      }
60  
61      public String getDelimiter()
62      {
63          if (delimiter == null)
64          {
65              return DEFAULT_DELIMITER;
66          }
67          else
68          {
69              return delimiter;
70          }
71      }
72  
73      public void setDelimiter(String delimiter)
74      {
75          this.delimiter = delimiter;
76      }
77  
78      public int getPriorityWeighting()
79      {
80          return priorityWeighting;
81      }
82  
83      public void setPriorityWeighting(int priorityWeighting)
84      {
85          this.priorityWeighting = priorityWeighting;
86      }
87  }