Author: rgoers Date: Tue Nov 9 16:10:13 2004 New Revision: 57114 Modified: cocoon/trunk/src/blocks/portal/java/org/apache/cocoon/portal/layout/renderer/aspect/impl/XSLTAspect.java Log: Allow configuration parameters that can be passed to the invoked stylesheet.
Modified: cocoon/trunk/src/blocks/portal/java/org/apache/cocoon/portal/layout/renderer/aspect/impl/XSLTAspect.java ============================================================================== --- cocoon/trunk/src/blocks/portal/java/org/apache/cocoon/portal/layout/renderer/aspect/impl/XSLTAspect.java (original) +++ cocoon/trunk/src/blocks/portal/java/org/apache/cocoon/portal/layout/renderer/aspect/impl/XSLTAspect.java Tue Nov 9 16:10:13 2004 @@ -20,6 +20,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.HashMap; import javax.xml.transform.Transformer; import javax.xml.transform.sax.SAXResult; @@ -30,13 +31,16 @@ import org.apache.avalon.framework.parameters.Parameters; import org.apache.avalon.framework.service.ServiceException; import org.apache.avalon.framework.service.ServiceManager; -import org.apache.cocoon.components.variables.VariableResolver; -import org.apache.cocoon.components.variables.VariableResolverFactory; +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.configuration.ConfigurationException; +import org.apache.avalon.framework.configuration.Configurable; import org.apache.cocoon.portal.PortalService; import org.apache.cocoon.portal.layout.Layout; import org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext; import org.apache.cocoon.sitemap.PatternException; import org.apache.cocoon.xml.IncludeXMLConsumer; +import org.apache.cocoon.components.variables.VariableResolverFactory; +import org.apache.cocoon.components.variables.VariableResolver; import org.apache.excalibur.source.Source; import org.apache.excalibur.source.SourceResolver; import org.apache.excalibur.xml.xslt.XSLTProcessor; @@ -47,17 +51,32 @@ /** * Apply a XSLT stylesheet to the contained layout. All following renderer aspects - * are applied first before the XML is fed into the XSLT. All layout parameters - * are made available to the stylesheet. + * are applied first before the XML is fed into the XSLT. All configuration and layout + * parameters are made available to the stylesheet. * * <h2>Example XML:</h2> * <pre> * <-- result from output of following renderers transformed by stylesheet --> * </pre> * + * + * The parameter values may contain Strings and/or references to input modules which will be resolved each + * time the aspect is rendered. * <h2>Applicable to:</h2> * [EMAIL PROTECTED] org.apache.cocoon.portal.layout.Layout} * + * <h2>Configuration</h2> + * <h3>cocoon.xconf</h3> + * + * <pre> + * <aspect name="xslt" class="org.apache.cocoon.portal.layout.renderer.aspect.impl.XSLTAspect"> + * <parameters> + * <parameter name="<i>name1</i>" value="<i>parameter value</i>"/> + * <parameter name="<i>name2</i>" value="<i>parameter value</i>"/> + * <parameter> + * </aspect> + * </pre> + * * <h2>Parameters</h2> * <table><tbody> * <tr><th>style</th><td></td><td>req</td><td>String</td><td><code>null</code></td></tr> @@ -67,14 +86,16 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> * @author <a href="mailto:[EMAIL PROTECTED]">Volker Schmitt</a> * - * @version CVS $Id: XSLTAspect.java,v 1.10 2004/04/25 20:12:04 haul Exp $ + * @version CVS $Id$ */ public class XSLTAspect extends AbstractAspect - implements Disposable { + implements Disposable, Configurable { protected List variables = new ArrayList(); - + + protected Parameters parameters = null; + protected VariableResolverFactory variableFactory; /* (non-Javadoc) @@ -85,6 +106,18 @@ this.variableFactory = (VariableResolverFactory) this.manager.lookup(VariableResolverFactory.ROLE); } + + /* (non-Javadoc) + * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration) + */ + public void configure(Configuration config) throws ConfigurationException { + Configuration parameterItems = config.getChild("parameters", false); + + if (parameterItems != null) { + this.parameters = Parameters.fromConfiguration(parameterItems); + } + } + /* (non-Javadoc) * @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) */ @@ -103,6 +136,18 @@ stylesheet = resolver.resolveURI(this.getStylesheetURI(config, layout)); processor = (XSLTProcessor) this.manager.lookup(config.xsltRole); TransformerHandler transformer = processor.getTransformerHandler(stylesheet); + // Pass configured parameters to the stylesheet. + if (config.parameters.size() > 0) { + Map.Entry entry; + Transformer theTransformer = transformer.getTransformer(); + Iterator iter = config.parameters.entrySet().iterator(); + while (iter.hasNext()) { + entry = (Map.Entry) iter.next(); + String value = getParameterValue(entry); + theTransformer.setParameter((String) entry.getKey(), value); + } + } + Map parameter = layout.getParameters(); if (parameter.size() > 0) { Map.Entry entry; @@ -147,13 +192,24 @@ } } + protected String getParameterValue(Map.Entry entry) throws SAXException { + try { + return ((VariableResolver)entry.getValue()).resolve(); + } catch (PatternException pe) { + throw new SAXException("Unable to get value for parameter " + entry.getKey(), pe); + } + } + + protected class PreparedConfiguration { public VariableResolver stylesheet; - public String xsltRole; + public String xsltRole; + public Map parameters = new HashMap(); public void takeValues(PreparedConfiguration from) { this.stylesheet = from.stylesheet; this.xsltRole = from.xsltRole; + this.parameters = from.parameters; } } @@ -171,6 +227,19 @@ throw new ParameterException("Unknown pattern for stylesheet " + stylesheet, pe); } this.variables.add(pc.stylesheet); + if (this.parameters != null) { + String[] name = this.parameters.getNames(); + for (int i=0; i < name.length; ++i) { + try { + VariableResolver resolver = + this.variableFactory.lookup(this.parameters.getParameter(name[i])); + this.variables.add(resolver); + pc.parameters.put(name[i], resolver); + } catch (PatternException e) { + throw new ParameterException("Invalid value for parameter " + name[i], e); + } + } + } return pc; } @@ -189,6 +258,4 @@ this.variableFactory = null; } } - -} - +} \ No newline at end of file