1 10 11 package org.mule.transformers.simple; 12 13 import java.util.Collection ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 17 import org.mule.transformers.AbstractTransformer; 18 import org.mule.umo.transformer.TransformerException; 19 20 28 public class ObjectToString extends AbstractTransformer 29 { 30 33 private static final long serialVersionUID = 1784548784479765795L; 34 35 public ObjectToString() 36 { 37 registerSourceType(Object .class); 38 setReturnClass(String .class); 39 } 40 41 public Object doTransform(Object src, String encoding) throws TransformerException 42 { 43 String output = ""; 44 45 if (src instanceof Map ) 46 { 47 Iterator iter = ((Map )src).entrySet().iterator(); 48 if (iter.hasNext()) 49 { 50 StringBuffer b = new StringBuffer (80); 51 while (iter.hasNext()) 52 { 53 Map.Entry e = (Map.Entry )iter.next(); 54 Object key = e.getKey(); 55 Object 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 ) 66 { 67 Iterator iter = ((Collection )src).iterator(); 68 if (iter.hasNext()) 69 { 70 StringBuffer b = new StringBuffer (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 |