KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > transformers > xml > AbstractXmlTransformer


1 /*
2  * $Id: AbstractXmlTransformer.java 3937 2006-11-20 16:04:25Z 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.transformers.xml;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import org.apache.commons.io.output.ByteArrayOutputStream;
15 import java.io.StringReader JavaDoc;
16 import java.io.StringWriter JavaDoc;
17
18 import javax.xml.transform.OutputKeys JavaDoc;
19 import javax.xml.transform.Result JavaDoc;
20 import javax.xml.transform.Source JavaDoc;
21 import javax.xml.transform.Transformer JavaDoc;
22 import javax.xml.transform.TransformerFactory JavaDoc;
23 import javax.xml.transform.TransformerFactoryConfigurationError JavaDoc;
24 import javax.xml.transform.dom.DOMResult JavaDoc;
25 import javax.xml.transform.dom.DOMSource JavaDoc;
26 import javax.xml.transform.stream.StreamResult JavaDoc;
27 import javax.xml.transform.stream.StreamSource JavaDoc;
28
29 import org.dom4j.Document;
30 import org.dom4j.io.DocumentResult;
31 import org.dom4j.io.DocumentSource;
32 import org.mule.transformers.AbstractTransformer;
33
34 /**
35  * <code>AbstractXmlTransformer</code> offers some XSLT transform on a DOM (or
36  * other XML-ish) object.
37  */

38
39 public abstract class AbstractXmlTransformer extends AbstractTransformer
40 {
41
42     public AbstractXmlTransformer()
43     {
44         registerSourceType(String JavaDoc.class);
45         registerSourceType(byte[].class);
46         registerSourceType(DocumentSource.class);
47         registerSourceType(Document.class);
48         registerSourceType(org.w3c.dom.Document JavaDoc.class);
49         registerSourceType(org.w3c.dom.Element JavaDoc.class);
50     }
51
52     public Source JavaDoc getXmlSource(Object JavaDoc src)
53     {
54         if (src instanceof byte[])
55         {
56             return new StreamSource JavaDoc(new ByteArrayInputStream JavaDoc((byte[])src));
57         }
58         else if (src instanceof String JavaDoc)
59         {
60             return new StreamSource JavaDoc(new StringReader JavaDoc((String JavaDoc)src));
61         }
62         else if (src instanceof DocumentSource)
63         {
64             return (Source JavaDoc)src;
65         }
66         else if (src instanceof Document)
67         {
68             return new DocumentSource((Document)src);
69         }
70         else if (src instanceof org.w3c.dom.Document JavaDoc)
71         {
72             return new DOMSource JavaDoc((org.w3c.dom.Document JavaDoc)src);
73         }
74         else
75             return null;
76     }
77
78     /**
79      * Result callback interface used when processing XML through JAXP
80      */

81     protected static interface ResultHolder
82     {
83
84         /**
85          * @return A Result to use in a transformation (e.g. writing a DOM to a
86          * stream)
87          */

88         Result JavaDoc getResult();
89
90         /**
91          * @return The actual result as produced after the call to 'transform'.
92          */

93         Object JavaDoc getResultObject();
94     }
95
96     /**
97      * @param desiredClass Java class representing the desired format
98      * @return Callback interface representing the desiredClass - or null if the
99      * return class isn't supported (or is null).
100      */

101     protected static ResultHolder getResultHolder(Class JavaDoc desiredClass)
102     {
103         if (desiredClass == null) return null;
104         if (byte[].class.equals(desiredClass))
105         {
106             return new ResultHolder()
107             {
108                 ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
109                 StreamResult JavaDoc result = new StreamResult JavaDoc(resultStream);
110
111                 public Result JavaDoc getResult()
112                 {
113                     return result;
114                 }
115
116                 public Object JavaDoc getResultObject()
117                 {
118                     return resultStream.toByteArray();
119                 }
120             };
121         }
122         else if (String JavaDoc.class.equals(desiredClass))
123         {
124             return new ResultHolder()
125             {
126                 StringWriter JavaDoc writer = new StringWriter JavaDoc();
127                 StreamResult JavaDoc result = new StreamResult JavaDoc(writer);
128
129                 public Result JavaDoc getResult()
130                 {
131                     return result;
132                 }
133
134                 public Object JavaDoc getResultObject()
135                 {
136                     return writer.getBuffer().toString();
137                 }
138             };
139         }
140         else if (org.w3c.dom.Document JavaDoc.class.isAssignableFrom(desiredClass))
141         {
142             return new ResultHolder()
143             {
144                 DOMResult JavaDoc result = new DOMResult JavaDoc();
145
146                 public Result JavaDoc getResult()
147                 {
148                     return result;
149                 }
150
151                 public Object JavaDoc getResultObject()
152                 {
153                     return result.getNode();
154                 }
155             };
156         }
157         else if (org.dom4j.io.DocumentResult.class.isAssignableFrom(desiredClass))
158         {
159             return new ResultHolder()
160             {
161                 DocumentResult result = new DocumentResult();
162
163                 public Result JavaDoc getResult()
164                 {
165                     return result;
166                 }
167
168                 public Object JavaDoc getResultObject()
169                 {
170                     return result;
171                 }
172             };
173         }
174         else if (org.dom4j.Document.class.isAssignableFrom(desiredClass))
175         {
176             return new ResultHolder()
177             {
178                 DocumentResult result = new DocumentResult();
179
180                 public Result JavaDoc getResult()
181                 {
182                     return result;
183                 }
184
185                 public Object JavaDoc getResultObject()
186                 {
187                     return result.getDocument();
188                 }
189             };
190         }
191         return null;
192     }
193
194     protected String JavaDoc convertToText(Object JavaDoc obj)
195         throws TransformerFactoryConfigurationError JavaDoc, javax.xml.transform.TransformerException JavaDoc
196     {
197         // Catch the direct translations
198
if (obj instanceof String JavaDoc)
199         {
200             return (String JavaDoc)obj;
201         }
202         else if (obj instanceof Document)
203         {
204             return ((Document)obj).asXML();
205         }
206         // No easy fix, so use the transformer.
207
Source JavaDoc src = getXmlSource(obj);
208         if (src == null) return null;
209
210         StringWriter JavaDoc writer = new StringWriter JavaDoc();
211         StreamResult JavaDoc result = new StreamResult JavaDoc(writer);
212
213         TransformerFactory.newInstance().newTransformer().transform(src, result);
214         return writer.getBuffer().toString();
215     }
216
217     protected String JavaDoc convertToBytes(Object JavaDoc obj, String JavaDoc preferredEncoding)
218         throws TransformerFactoryConfigurationError JavaDoc, javax.xml.transform.TransformerException JavaDoc
219     {
220         // Always use the transformer, even for byte[] (to get the encoding right!)
221
Source JavaDoc src = getXmlSource(obj);
222         if (src == null) return null;
223
224         StringWriter JavaDoc writer = new StringWriter JavaDoc();
225         StreamResult JavaDoc result = new StreamResult JavaDoc(writer);
226
227         Transformer JavaDoc idTransformer = TransformerFactory.newInstance().newTransformer();
228         idTransformer.setOutputProperty(OutputKeys.ENCODING, preferredEncoding);
229         idTransformer.transform(src, result);
230         return writer.getBuffer().toString();
231     }
232
233 }
234
Popular Tags