View Javadoc

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