View Javadoc

1   /*
2    * $Id: ObjectToString.java 9680 2007-11-12 10:26:06Z 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  
16  import java.util.Collection;
17  import java.util.Iterator;
18  import java.util.Map;
19  
20  /**
21   * <code>ObjectToString</code> transformer is useful for debugging. It will return
22   * human-readable output for various kinds of objects. Right now, it is just coded to
23   * handle Map and Collection objects. Others will be added.
24   */
25  public class ObjectToString extends AbstractTransformer
26  {
27      protected static final int DEFAULT_BUFFER_SIZE = 80;
28      protected static final String NULL_DESCRIPTION = "null";
29  
30      public ObjectToString()
31      {
32          registerSourceType(Object.class);
33          setReturnClass(String.class);
34      }
35  
36      public Object doTransform(Object src, String encoding) throws TransformerException
37      {
38          String output = "";
39  
40          if (src instanceof Map)
41          {
42              Iterator iter = ((Map) src).entrySet().iterator();
43              if (iter.hasNext())
44              {
45                  StringBuffer b = new StringBuffer(DEFAULT_BUFFER_SIZE);
46                  while (iter.hasNext())
47                  {
48                      Map.Entry e = (Map.Entry) iter.next();
49                      Object key = e.getKey();
50                      Object value = e.getValue();
51                      b.append(key.toString()).append(':');
52                      if (value != null)
53                      {
54                          b.append(value.toString());
55                      }
56                      else
57                      {
58                          b.append(NULL_DESCRIPTION);
59                      }
60                      if (iter.hasNext())
61                      {
62                          b.append('|');
63                      }
64                  }
65                  output = b.toString();
66              }
67          }
68          else if (src instanceof Collection)
69          {
70              Iterator iter = ((Collection) src).iterator();
71              if (iter.hasNext())
72              {
73                  StringBuffer b = new StringBuffer(DEFAULT_BUFFER_SIZE);
74                  while (iter.hasNext())
75                  {
76                      Object value = iter.next();
77                      if (value != null)
78                      {
79                          b.append(value.toString());   
80                      }
81                      else
82                      {
83                          b.append(NULL_DESCRIPTION);
84                      }
85                      
86                      if (iter.hasNext())
87                      {
88                          b.append('|');
89                      }
90                  }
91                  output = b.toString();
92              }
93          }
94          else
95          {
96              output = src.toString();
97          }
98  
99          return output;
100     }
101 
102 }