KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > layout > renderer > aspect > impl > XSLTAspect


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

16 package org.apache.cocoon.portal.layout.renderer.aspect.impl;
17
18 import java.io.IOException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 import javax.xml.transform.Transformer JavaDoc;
26 import javax.xml.transform.sax.SAXResult JavaDoc;
27 import javax.xml.transform.sax.TransformerHandler JavaDoc;
28
29 import org.apache.avalon.framework.activity.Disposable;
30 import org.apache.avalon.framework.parameters.ParameterException;
31 import org.apache.avalon.framework.parameters.Parameters;
32 import org.apache.avalon.framework.service.ServiceException;
33 import org.apache.avalon.framework.service.ServiceManager;
34 import org.apache.avalon.framework.configuration.Configuration;
35 import org.apache.avalon.framework.configuration.ConfigurationException;
36 import org.apache.avalon.framework.configuration.Configurable;
37 import org.apache.cocoon.portal.PortalService;
38 import org.apache.cocoon.portal.layout.Layout;
39 import org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext;
40 import org.apache.cocoon.sitemap.PatternException;
41 import org.apache.cocoon.xml.IncludeXMLConsumer;
42 import org.apache.cocoon.components.variables.VariableResolverFactory;
43 import org.apache.cocoon.components.variables.VariableResolver;
44 import org.apache.excalibur.source.Source;
45 import org.apache.excalibur.source.SourceResolver;
46 import org.apache.excalibur.xml.xslt.XSLTProcessor;
47 import org.apache.excalibur.xml.xslt.XSLTProcessorException;
48 import org.xml.sax.ContentHandler JavaDoc;
49 import org.xml.sax.SAXException JavaDoc;
50 import org.xml.sax.ext.LexicalHandler JavaDoc;
51
52 /**
53  * Apply a XSLT stylesheet to the contained layout. All following renderer aspects
54  * are applied first before the XML is fed into the XSLT. All configuration and layout
55  * parameters are made available to the stylesheet.
56  *
57  * <h2>Example XML:</h2>
58  * <pre>
59  * &lt;-- result from output of following renderers transformed by stylesheet --&gt;
60  * </pre>
61  *
62  *
63  * The parameter values may contain Strings and/or references to input modules which will be resolved each
64  * time the aspect is rendered.
65  * <h2>Applicable to:</h2>
66  * {@link org.apache.cocoon.portal.layout.Layout}
67  *
68  * <h2>Configuration</h2>
69  * <h3>cocoon.xconf</h3>
70  *
71  * <pre>
72  * &lt;aspect name="xslt" class="org.apache.cocoon.portal.layout.renderer.aspect.impl.XSLTAspect"&gt;
73  * &lt;parameters&gt;
74  * &lt;parameter name="<i>name1</i>" value="<i>parameter value</i>"/&gt;
75  * &lt;parameter name="<i>name2</i>" value="<i>parameter value</i>"/&gt;
76  * &lt;parameter&gt;
77  * &lt;/aspect&gt;
78  * </pre>
79  *
80  * <h2>Parameters</h2>
81  * <table><tbody>
82  * <tr><th>style</th><td></td><td>req</td><td>String</td><td><code>null</code></td></tr>
83  * <tr><th>xslt-processor-role</th><td></td><td>req</td><td>String</td><td><code>null</code></td></tr>
84  * </tbody></table>
85  *
86  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
87  * @author <a HREF="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
88  *
89  * @version CVS $Id: XSLTAspect.java 331660 2005-11-08 01:22:30Z rgoers $
90  */

91 public class XSLTAspect
92     extends AbstractAspect
93     implements Disposable, Configurable {
94
95     protected List JavaDoc variables = new ArrayList JavaDoc();
96
97     protected Parameters parameters = null;
98
99     protected VariableResolverFactory variableFactory;
100     
101     /* (non-Javadoc)
102      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
103      */

104     public void service(ServiceManager manager) throws ServiceException {
105         super.service(manager);
106         this.variableFactory = (VariableResolverFactory) this.manager.lookup(VariableResolverFactory.ROLE);
107     }
108
109
110     /* (non-Javadoc)
111     * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
112     */

113     public void configure(Configuration config) throws ConfigurationException {
114         Configuration parameterItems = config.getChild("parameters", false);
115
116         if (parameterItems != null) {
117             this.parameters = Parameters.fromConfiguration(parameterItems);
118         }
119     }
120
121     /* (non-Javadoc)
122      * @see org.apache.cocoon.portal.layout.renderer.RendererAspect#toSAX(org.apache.cocoon.portal.layout.renderer.RendererAspectContext, org.apache.cocoon.portal.layout.Layout, org.apache.cocoon.portal.PortalService, org.xml.sax.ContentHandler)
123      */

124     public void toSAX(RendererAspectContext context,
125                         Layout layout,
126                         PortalService service,
127                         ContentHandler JavaDoc handler)
128     throws SAXException JavaDoc {
129         PreparedConfiguration config = (PreparedConfiguration)context.getAspectConfiguration();
130
131         XSLTProcessor processor = null;
132         Source stylesheet = null;
133         SourceResolver resolver = null;
134         try {
135             resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
136             stylesheet = resolver.resolveURI(this.getStylesheetURI(config, layout));
137             processor = (XSLTProcessor) this.manager.lookup(config.xsltRole);
138             TransformerHandler JavaDoc transformer = processor.getTransformerHandler(stylesheet);
139             // Pass configured parameters to the stylesheet.
140
if (config.parameters.size() > 0) {
141                 Map.Entry JavaDoc entry;
142                 Transformer JavaDoc theTransformer = transformer.getTransformer();
143                 Iterator JavaDoc iter = config.parameters.entrySet().iterator();
144                 while (iter.hasNext()) {
145                     entry = (Map.Entry JavaDoc) iter.next();
146                     String JavaDoc value = getParameterValue(entry);
147                     theTransformer.setParameter((String JavaDoc) entry.getKey(), value);
148                 }
149             }
150
151             Map JavaDoc parameter = layout.getParameters();
152             if (parameter.size() > 0) {
153                 Map.Entry JavaDoc entry;
154                 Transformer JavaDoc theTransformer = transformer.getTransformer();
155                 for (Iterator JavaDoc iter = parameter.entrySet().iterator(); iter.hasNext();) {
156                     entry = (Map.Entry JavaDoc) iter.next();
157                     theTransformer.setParameter((String JavaDoc)entry.getKey(), entry.getValue());
158                 }
159             }
160             SAXResult JavaDoc result = new SAXResult JavaDoc(new IncludeXMLConsumer((handler)));
161             if (handler instanceof LexicalHandler JavaDoc) {
162                 result.setLexicalHandler((LexicalHandler JavaDoc) handler);
163             }
164             transformer.setResult(result);
165             transformer.startDocument();
166             context.invokeNext(layout, service, transformer);
167
168             transformer.endDocument();
169         } catch (XSLTProcessorException xpe) {
170             throw new SAXException JavaDoc("XSLT Exception.", xpe);
171         } catch (IOException JavaDoc io) {
172             throw new SAXException JavaDoc("Error in resolving.", io);
173         } catch (ServiceException ce) {
174             throw new SAXException JavaDoc("Unable to lookup component.", ce);
175         } finally {
176             if (null != resolver) {
177                 resolver.release(stylesheet);
178                 this.manager.release(resolver);
179             }
180             this.manager.release(processor);
181         }
182     }
183
184     protected String JavaDoc getStylesheetURI(PreparedConfiguration config, Layout layout)
185     throws SAXException JavaDoc {
186         // FIXME Get the stylesheet either from a layout attribute or another aspect
187
try {
188             String JavaDoc stylesheet = config.stylesheet.resolve();
189             return stylesheet;
190         } catch (PatternException pe) {
191             throw new SAXException JavaDoc("Pattern exception during variable resolving.", pe);
192         }
193     }
194
195     protected String JavaDoc getParameterValue(Map.Entry JavaDoc entry) throws SAXException JavaDoc {
196         try {
197             return ((VariableResolver)entry.getValue()).resolve();
198         } catch (PatternException pe) {
199             throw new SAXException JavaDoc("Unable to get value for parameter " + entry.getKey(), pe);
200         }
201     }
202
203
204     protected static class PreparedConfiguration {
205         public VariableResolver stylesheet;
206         public String JavaDoc xsltRole;
207         public Map JavaDoc parameters = new HashMap JavaDoc();
208
209         public void takeValues(PreparedConfiguration from) {
210             this.stylesheet = from.stylesheet;
211             this.xsltRole = from.xsltRole;
212             this.parameters = from.parameters;
213         }
214     }
215     
216     /* (non-Javadoc)
217      * @see org.apache.cocoon.portal.layout.renderer.aspect.RendererAspect#prepareConfiguration(org.apache.avalon.framework.parameters.Parameters)
218      */

219     public Object JavaDoc prepareConfiguration(Parameters configuration)
220     throws ParameterException {
221         PreparedConfiguration pc = new PreparedConfiguration();
222         pc.xsltRole = configuration.getParameter("xslt-processor-role", XSLTProcessor.ROLE);
223         String JavaDoc stylesheet = configuration.getParameter("style");
224         try {
225             pc.stylesheet = this.variableFactory.lookup( stylesheet );
226         } catch (PatternException pe) {
227             throw new ParameterException("Unknown pattern for stylesheet " + stylesheet, pe);
228         }
229         this.variables.add(pc.stylesheet);
230         if (this.parameters != null) {
231             String JavaDoc[] name = this.parameters.getNames();
232             for (int i=0; i < name.length; ++i) {
233                 try {
234                     VariableResolver resolver =
235                         this.variableFactory.lookup(this.parameters.getParameter(name[i]));
236                     this.variables.add(resolver);
237                     pc.parameters.put(name[i], resolver);
238                 } catch (PatternException e) {
239                     throw new ParameterException("Invalid value for parameter " + name[i], e);
240                 }
241             }
242         }
243         return pc;
244     }
245
246     /* (non-Javadoc)
247      * @see org.apache.avalon.framework.activity.Disposable#dispose()
248      */

249     public void dispose() {
250         if ( this.manager != null ) {
251             Iterator JavaDoc vars = this.variables.iterator();
252             while ( vars.hasNext() ) {
253                 this.variableFactory.release( (VariableResolver) vars.next() );
254             }
255             this.variables.clear();
256             this.manager.release( this.variableFactory);
257             this.manager = null;
258             this.variableFactory = null;
259         }
260     }
261 }
Popular Tags