KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > dq > transformers > DQMessageToXml


1 /*
2  * $Id: DQMessageToXml.java 3982 2006-11-22 14:28:01Z lajos $
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.providers.dq.transformers;
12
13 import java.util.Iterator JavaDoc;
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 /**
22  * <code>DQMessageToXml</code> Will convert a DQMessage to an xml string by
23  * extracting the message payload. The xml exemple:
24  *
25  * <pre>
26  *
27  *
28  * &amp;ltDQMessage&amp;gt
29  * &amp;ltentry name=&quot;the name&quot;&amp;gt The value &amp;lt/entry&amp;gt
30  * ....
31  * &amp;lt/DQMessage&amp;gt
32  *
33  *
34  * </pre>
35  */

36 public class DQMessageToXml extends AbstractTransformer
37 {
38     /**
39      * Serial version
40      */

41     private static final long serialVersionUID = 7383276830788153575L;
42
43     /**
44      * Constructor
45      */

46     public DQMessageToXml()
47     {
48         registerSourceType(DQMessage.class);
49         setReturnClass(String JavaDoc.class);
50     }
51
52     /**
53      * @see org.mule.transformers.AbstractTransformer#doTransform(Object, String)
54      */

55     public final Object JavaDoc doTransform(final Object JavaDoc src, String JavaDoc 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 JavaDoc it = msg.getEntryNames().iterator();
65
66             while (it.hasNext())
67             {
68                 String JavaDoc name = (String JavaDoc)it.next();
69                 Object JavaDoc field = msg.getEntry(name);
70
71                 if (field instanceof String JavaDoc)
72                 {
73                     String JavaDoc f = ((String JavaDoc)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 JavaDoc e)
88         {
89             throw new TransformerException(this, e);
90         }
91     }
92
93 }
94
Popular Tags