KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > transformers > simple > ObjectToString


1 /*
2  * $Id: ObjectToString.java 4259 2006-12-14 03:12:07Z aperepel $
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 MuleSource MPL
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 java.util.Collection JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.mule.transformers.AbstractTransformer;
18 import org.mule.umo.transformer.TransformerException;
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  * @author <a HREF="mailto:lajos@galatea.com">Lajos Moczar</a>
26  * @version $Revision: 4259 $
27  */

28 public class ObjectToString extends AbstractTransformer
29 {
30     /**
31      * Serial version
32      */

33     private static final long serialVersionUID = 1784548784479765795L;
34
35     public ObjectToString()
36     {
37         registerSourceType(Object JavaDoc.class);
38         setReturnClass(String JavaDoc.class);
39     }
40
41     public Object JavaDoc doTransform(Object JavaDoc src, String JavaDoc encoding) throws TransformerException
42     {
43         String JavaDoc output = "";
44
45         if (src instanceof Map JavaDoc)
46         {
47             Iterator JavaDoc iter = ((Map JavaDoc)src).entrySet().iterator();
48             if (iter.hasNext())
49             {
50                 StringBuffer JavaDoc b = new StringBuffer JavaDoc(80);
51                 while (iter.hasNext())
52                 {
53                     Map.Entry JavaDoc e = (Map.Entry JavaDoc)iter.next();
54                     Object JavaDoc key = e.getKey();
55                     Object JavaDoc value = e.getValue();
56                     b.append(key.toString()).append(':').append(value.toString());
57                     if (iter.hasNext())
58                     {
59                         b.append('|');
60                     }
61                 }
62                 output = b.toString();
63             }
64         }
65         else if (src instanceof Collection JavaDoc)
66         {
67             Iterator JavaDoc iter = ((Collection JavaDoc)src).iterator();
68             if (iter.hasNext())
69             {
70                 StringBuffer JavaDoc b = new StringBuffer JavaDoc(80);
71                 while (iter.hasNext())
72                 {
73                     b.append(iter.next().toString());
74                     if (iter.hasNext())
75                     {
76                         b.append('|');
77                     }
78                 }
79                 output = b.toString();
80             }
81         }
82         else
83         {
84             output = src.toString();
85         }
86
87         return output;
88     }
89 }
90
Popular Tags