1 10 11 package org.mule.providers.dq.transformers; 12 13 import java.util.Iterator ; 14 15 import org.dom4j.DocumentHelper; 16 import org.dom4j.Element; 17 import org.mule.providers.dq.DQMessage; 18 import org.mule.transformers.AbstractTransformer; 19 import org.mule.umo.transformer.TransformerException; 20 21 36 public class DQMessageToXml extends AbstractTransformer 37 { 38 41 private static final long serialVersionUID = 7383276830788153575L; 42 43 46 public DQMessageToXml() 47 { 48 registerSourceType(DQMessage.class); 49 setReturnClass(String .class); 50 } 51 52 55 public final Object doTransform(final Object src, String encoding) throws TransformerException 56 { 57 DQMessage msg = (DQMessage)src; 58 59 try 60 { 61 org.dom4j.Document document = DocumentHelper.createDocument(); 62 Element root = document.addElement(DQMessage.XML_ROOT); 63 64 Iterator it = msg.getEntryNames().iterator(); 65 66 while (it.hasNext()) 67 { 68 String name = (String )it.next(); 69 Object field = msg.getEntry(name); 70 71 if (field instanceof String ) 72 { 73 String f = ((String )field).trim(); 74 field = (f.length() == 0) ? null : f; 75 } 76 77 if (field != null) 78 { 79 root.addElement(DQMessage.XML_ENTRY).addAttribute(DQMessage.XML_NAME, name).addText( 80 field.toString()); 81 } 82 } 83 84 return document.asXML(); 85 86 } 87 catch (Exception e) 88 { 89 throw new TransformerException(this, e); 90 } 91 } 92 93 } 94 | Popular Tags |