KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > xslt > XsltComponent


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.xslt;
18
19 import org.apache.servicemix.MessageExchangeListener;
20 import org.apache.servicemix.components.util.TransformComponentSupport;
21 import org.apache.servicemix.jbi.jaxp.BytesSource;
22 import org.apache.servicemix.jbi.jaxp.StringSource;
23 import org.springframework.core.io.Resource;
24 import org.w3c.dom.Document JavaDoc;
25 import org.w3c.dom.Node JavaDoc;
26
27 import javax.jbi.messaging.MessageExchange;
28 import javax.jbi.messaging.MessagingException;
29 import javax.jbi.messaging.NormalizedMessage;
30 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
32 import javax.xml.transform.Result JavaDoc;
33 import javax.xml.transform.Source JavaDoc;
34 import javax.xml.transform.Templates JavaDoc;
35 import javax.xml.transform.Transformer JavaDoc;
36 import javax.xml.transform.TransformerConfigurationException JavaDoc;
37 import javax.xml.transform.TransformerException JavaDoc;
38 import javax.xml.transform.TransformerFactory JavaDoc;
39 import javax.xml.transform.dom.DOMSource JavaDoc;
40 import javax.xml.transform.stream.StreamResult JavaDoc;
41 import javax.xml.transform.stream.StreamSource JavaDoc;
42
43 import java.io.ByteArrayOutputStream JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.io.StringWriter JavaDoc;
46 import java.net.URL JavaDoc;
47 import java.util.Iterator JavaDoc;
48
49 /**
50  * An <a HREF="http://www.w3.org/TR/xslt">XSLT</a> based JBI component using <a
51  * HREF="http://java.sun.com/xml/jaxp/">JAXP</a> to perform the XSLT
52  * transformation.
53  *
54  * @version $Revision: 426415 $
55  */

56 public class XsltComponent extends TransformComponentSupport implements MessageExchangeListener {
57
58     private TransformerFactory JavaDoc transformerFactory;
59     private Source xsltSource;
60     private Resource xsltResource;
61     private Templates JavaDoc templates;
62     private boolean disableOutput;
63     private boolean useStringBuffer = true;
64     private boolean forceDocIfDom = true;
65
66     /**
67      * @return the forceDocIfDom
68      */

69     public boolean isForceDocIfDom() {
70         return forceDocIfDom;
71     }
72
73     /**
74      * @param forceDocIfDom the forceDocIfDom to set
75      */

76     public void setForceDocIfDom(boolean forceDocIfDom) {
77         this.forceDocIfDom = forceDocIfDom;
78     }
79
80     // Properties
81
// -------------------------------------------------------------------------
82
public TransformerFactory JavaDoc getTransformerFactory() {
83         if (transformerFactory == null) {
84             transformerFactory = TransformerFactory.newInstance();
85         }
86         return transformerFactory;
87     }
88
89     public void setTransformerFactory(TransformerFactory JavaDoc transformerFactory) {
90         this.transformerFactory = transformerFactory;
91     }
92
93     public Source getXsltSource() throws IOException JavaDoc {
94         if (xsltSource == null) {
95             // lets create a new one each time
96
// as we can only read a stream once
97
return createXsltSource();
98         }
99         return xsltSource;
100     }
101
102     public void setXsltSource(Source xsltSource) {
103         this.xsltSource = xsltSource;
104     }
105
106     public Resource getXsltResource() {
107         return xsltResource;
108     }
109
110     public void setXsltResource(Resource xsltResource) {
111         this.xsltResource = xsltResource;
112     }
113
114     public boolean isDisableOutput() {
115         return disableOutput;
116     }
117
118     public void setDisableOutput(boolean disableOutput) {
119         this.disableOutput = disableOutput;
120     }
121
122     public boolean isUseStringBuffer() {
123         return useStringBuffer;
124     }
125
126     public void setUseStringBuffer(boolean useStringBuffer) {
127         this.useStringBuffer = useStringBuffer;
128     }
129
130     // Implementation methods
131
// -------------------------------------------------------------------------
132
protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out)
133             throws MessagingException {
134         try {
135             Transformer JavaDoc transformer = createTransformer(exchange, in);
136             configureTransformer(transformer, exchange, in);
137             copyPropertiesAndAttachments(exchange, in, out);
138             transformContent(transformer, exchange, in, out);
139             return shouldOutputResult(transformer);
140         }
141         catch (ParserConfigurationException JavaDoc e) {
142             e.printStackTrace();
143             throw new MessagingException("Failed to transform: " + e, e);
144         }
145         catch (TransformerException JavaDoc e) {
146             e.printStackTrace();
147             throw new MessagingException("Failed to transform: " + e, e);
148         }
149         catch (IOException JavaDoc e) {
150             throw new MessagingException("Failed to load transform: " + e, e);
151         }
152     }
153
154     protected void transformContent(Transformer JavaDoc transformer, MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws TransformerException JavaDoc, MessagingException, ParserConfigurationException JavaDoc {
155         Source src = in.getContent();
156         if (forceDocIfDom && src instanceof DOMSource JavaDoc) {
157             Node JavaDoc n = ((DOMSource JavaDoc) src).getNode();
158             if (n instanceof Document JavaDoc == false) {
159                 Document JavaDoc doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
160                 doc.appendChild(doc.importNode(n, true));
161                 src = new DOMSource JavaDoc(doc);
162             }
163         }
164         if (isUseStringBuffer()) {
165             StringWriter JavaDoc buffer = new StringWriter JavaDoc();
166             Result JavaDoc result = new StreamResult JavaDoc(buffer);
167             transformer.transform(src, result);
168             out.setContent(new StringSource(buffer.toString()));
169         }
170         else {
171             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
172             Result JavaDoc result = new StreamResult JavaDoc(buffer);
173             transformer.transform(src, result);
174             out.setContent(new BytesSource(buffer.toByteArray()));
175         }
176     }
177
178     /**
179      * Should we disable output of the result of the XSLT?
180      */

181     protected boolean shouldOutputResult(Transformer JavaDoc transformer) {
182         if (disableOutput) {
183             return false;
184         }
185         return true;
186         /**
187          * String value = transformer.getOutputProperty("disableOutput"); return
188          * value == null || !value.equals("true");
189          */

190     }
191
192     protected Source createXsltSource() throws IOException JavaDoc {
193         if (xsltResource != null) {
194             URL JavaDoc url = xsltResource.getURL();
195             if (url == null) {
196                 return new StreamSource JavaDoc(xsltResource.getInputStream());
197             }
198             else {
199                 return new StreamSource JavaDoc(xsltResource.getInputStream(), url.toExternalForm());
200             }
201         }
202         return null;
203     }
204
205     public Templates JavaDoc getTemplates() throws IOException JavaDoc, TransformerConfigurationException JavaDoc {
206         if (templates == null) {
207             templates = createTemplates();
208         }
209         return templates;
210     }
211
212     /**
213      * Factory method to create a new transformer instance
214      */

215     protected Templates JavaDoc createTemplates() throws TransformerConfigurationException JavaDoc, IOException JavaDoc {
216         Source source = getXsltSource();
217         return getTransformerFactory().newTemplates(source);
218     }
219
220     /**
221      * Factory method to create a new transformer instance
222      */

223     protected Transformer JavaDoc createTransformer(MessageExchange exchange, NormalizedMessage in)
224             throws TransformerConfigurationException JavaDoc, IOException JavaDoc {
225         Source source = getXsltSource();
226         if (source == null) {
227             return getTransformerFactory().newTransformer();
228         }
229         else {
230             return getTemplates().newTransformer();
231         }
232     }
233
234     /**
235      * A hook to allow the transformer to be configured from the current
236      * exchange and inbound message
237      */

238     protected void configureTransformer(Transformer JavaDoc transformer, MessageExchange exchange, NormalizedMessage in) {
239         for (Iterator JavaDoc iter = exchange.getPropertyNames().iterator(); iter.hasNext();) {
240             String JavaDoc name = (String JavaDoc) iter.next();
241             Object JavaDoc value = exchange.getProperty(name);
242             transformer.setParameter(name, value);
243         }
244         for (Iterator JavaDoc iter = in.getPropertyNames().iterator(); iter.hasNext();) {
245             String JavaDoc name = (String JavaDoc) iter.next();
246             Object JavaDoc value = in.getProperty(name);
247             transformer.setParameter(name, value);
248         }
249         transformer.setParameter("exchange", exchange);
250         transformer.setParameter("in", in);
251         transformer.setParameter("component", this);
252     }
253
254 }
255
Popular Tags