KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: XsltTransformer.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.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.StringReader JavaDoc;
16
17 import javax.xml.transform.ErrorListener JavaDoc;
18 import javax.xml.transform.OutputKeys JavaDoc;
19 import javax.xml.transform.Source JavaDoc;
20 import javax.xml.transform.Transformer JavaDoc;
21 import javax.xml.transform.TransformerFactory JavaDoc;
22 import javax.xml.transform.stream.StreamSource JavaDoc;
23
24 import org.apache.commons.pool.BasePoolableObjectFactory;
25 import org.apache.commons.pool.ObjectPool;
26 import org.apache.commons.pool.impl.StackObjectPool;
27 import org.mule.config.i18n.Message;
28 import org.mule.config.i18n.Messages;
29 import org.mule.umo.lifecycle.InitialisationException;
30 import org.mule.umo.transformer.TransformerException;
31 import org.mule.umo.transformer.UMOTransformer;
32 import org.mule.util.IOUtils;
33
34 /**
35  * <code>XsltTransformer</code> performs an XSLT transform on a DOM (or other
36  * XML-ish) object.
37  *
38  */

39
40 public class XsltTransformer extends AbstractXmlTransformer
41 {
42     /**
43      * Serial version
44      */

45     private static final long serialVersionUID = -6958917343589717387L;
46
47     // always keep at least 1 XSLT Transformer ready
48
private static final int MIN_IDLE = 1;
49
50     private ObjectPool transformerPool;
51     private int maxIdleTransformers = 2;
52     private String JavaDoc xslFile;
53     private String JavaDoc xslt;
54
55     public XsltTransformer()
56     {
57         super();
58     }
59
60     /**
61      * @see org.mule.umo.lifecycle.Initialisable#initialise()
62      */

63     public void initialise() throws InitialisationException
64     {
65         try
66         {
67             transformerPool = new StackObjectPool(new BasePoolableObjectFactory()
68             {
69                 public Object JavaDoc makeObject() throws Exception JavaDoc
70                 {
71                     StreamSource JavaDoc source = getStreamSource();
72                     TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
73                     return factory.newTransformer(source);
74                 }
75             }, Math.max(MIN_IDLE, maxIdleTransformers));
76             transformerPool.addObject();
77         }
78         catch (Throwable JavaDoc te)
79         {
80             throw new InitialisationException(te, this);
81         }
82     }
83
84     /**
85      * Transform, using XSLT, a XML String to another String.
86      *
87      * @param src The source XML (String, byte[], DOM, etc.)
88      * @return The result String (or DOM)
89      */

90     public Object JavaDoc doTransform(Object JavaDoc src, String JavaDoc encoding) throws TransformerException
91     {
92         try
93         {
94             Source JavaDoc sourceDoc = getXmlSource(src);
95             if (sourceDoc == null) return null;
96
97             ResultHolder holder = getResultHolder(returnClass);
98             if (holder == null) holder = getResultHolder(src.getClass());
99
100             DefaultErrorListener errorListener = new DefaultErrorListener(this);
101             Transformer JavaDoc transformer = null;
102             Object JavaDoc result;
103             try
104             {
105                 transformer = (Transformer JavaDoc)transformerPool.borrowObject();
106
107                 transformer.setErrorListener(errorListener);
108                 transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
109
110                 transformer.transform(sourceDoc, holder.getResult());
111                 result = holder.getResultObject();
112
113                 if (errorListener.isError())
114                 {
115                     throw errorListener.getException();
116                 }
117             }
118             finally
119             {
120                 if (transformer != null) transformerPool.returnObject(transformer);
121             }
122             return result;
123         }
124         catch (Exception JavaDoc e)
125         {
126             throw new TransformerException(this, e);
127         }
128     }
129
130     /**
131      * @return Returns the xslFile.
132      */

133     public String JavaDoc getXslFile()
134     {
135         return xslFile;
136     }
137
138     /**
139      * @param xslFile The xslFile to set.
140      */

141     public void setXslFile(String JavaDoc xslFile)
142     {
143         this.xslFile = xslFile;
144     }
145
146     /**
147      * Returns the StreamSource corresponding to xslFile
148      *
149      * @return The StreamSource
150      * @throws InitialisationException
151      */

152     private StreamSource JavaDoc getStreamSource() throws InitialisationException
153     {
154         if (xslt != null)
155         {
156             return new StreamSource JavaDoc(new StringReader JavaDoc(xslt));
157         }
158
159         if (xslFile == null)
160         {
161             throw new InitialisationException(new Message(Messages.X_IS_NULL, "xslFile"), this);
162         }
163
164         InputStream JavaDoc is;
165         try
166         {
167             is = IOUtils.getResourceAsStream(xslFile, getClass());
168         }
169         catch (IOException JavaDoc e)
170         {
171             throw new InitialisationException(e, this);
172         }
173         if (is != null)
174         {
175             return new StreamSource JavaDoc(is);
176         }
177         else
178         {
179             throw new InitialisationException(new Message(Messages.FAILED_LOAD_X, xslFile), this);
180         }
181     }
182
183     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
184     {
185         XsltTransformer x = (XsltTransformer)super.clone();
186         try
187         {
188             if (x.nextTransformer == null)
189             {
190                 x.initialise();
191             }
192         }
193         catch (Exception JavaDoc e)
194         {
195             throw new CloneNotSupportedException JavaDoc(e.getMessage());
196         }
197         return x;
198     }
199
200     private class DefaultErrorListener implements ErrorListener JavaDoc
201     {
202         private TransformerException e = null;
203
204         private UMOTransformer trans;
205
206         public DefaultErrorListener(UMOTransformer trans)
207         {
208             this.trans = trans;
209         }
210
211         public TransformerException getException()
212         {
213             return e;
214         }
215
216         public boolean isError()
217         {
218             return e != null;
219         }
220
221         public void error(javax.xml.transform.TransformerException JavaDoc exception)
222             throws javax.xml.transform.TransformerException JavaDoc
223         {
224             logger.error(exception.getMessage(), exception);
225             e = new TransformerException(trans, exception);
226         }
227
228         public void fatalError(javax.xml.transform.TransformerException JavaDoc exception)
229             throws javax.xml.transform.TransformerException JavaDoc
230         {
231             logger.fatal(exception.getMessage());
232             e = new TransformerException(trans, exception);
233         }
234
235         public void warning(javax.xml.transform.TransformerException JavaDoc exception)
236             throws javax.xml.transform.TransformerException JavaDoc
237         {
238             logger.warn(exception.getMessage());
239         }
240     }
241
242     /**
243      * @return The current maximum number of allowable idle transformer objects in
244      * the pool
245      */

246     public int getMaxIdleTransformers()
247     {
248         return maxIdleTransformers;
249     }
250
251     /**
252      * Sets the the current maximum number of idle transformer objects allowed in the
253      * pool
254      *
255      * @param maxIdleTransformers New maximum size to set
256      */

257     public void setMaxIdleTransformers(int maxIdleTransformers)
258     {
259         this.maxIdleTransformers = maxIdleTransformers;
260     }
261
262     public String JavaDoc getXslt()
263     {
264         return xslt;
265     }
266
267     public void setXslt(String JavaDoc xslt)
268     {
269         this.xslt = xslt;
270     }
271 }
272
Popular Tags