/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <stefano@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.portal.layout.renderer.aspect.impl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.TransformerHandler;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.parameters.ParameterException;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.components.variables.VariableResolver;
import org.apache.cocoon.components.variables.VariableResolverFactory;
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.excalibur.source.Source;
import org.apache.excalibur.source.SourceResolver;
import org.apache.excalibur.xml.xslt.XSLTProcessor;
import org.apache.excalibur.xml.xslt.XSLTProcessorException;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.ext.LexicalHandler;
/**
*
* @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
* @author <a href="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
*
* @version CVS $Id: XSLTAspect.java,v 1.7 2003/09/24 21:22:33 cziegeler Exp $
*/
public class XSLTAspect
extends AbstractAspect
implements Disposable {
protected List variables = new ArrayList();
protected VariableResolverFactory variableFactory;
/* (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)
*/
public void toSAX(RendererAspectContext context,
Layout layout,
PortalService service,
ContentHandler handler)
throws SAXException {
PreparedConfiguration config = (PreparedConfiguration)context.getAspectConfiguration();
XSLTProcessor processor = null;
Source stylesheet = null;
SourceResolver resolver = null;
try {
resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
stylesheet = resolver.resolveURI(this.getStylesheetURI(config, layout));
processor = (XSLTProcessor) this.manager.lookup(config.xsltRole);
TransformerHandler transformer = processor.getTransformerHandler(stylesheet);
SAXResult result = new SAXResult(new IncludeXMLConsumer((handler)));
if (handler instanceof LexicalHandler) {
result.setLexicalHandler((LexicalHandler) handler);
}
transformer.setResult(result);
transformer.startDocument();
context.invokeNext(layout, service, transformer);
transformer.endDocument();
} catch (XSLTProcessorException xpe) {
throw new SAXException("XSLT Exception.", xpe);
} catch (IOException io) {
throw new SAXException("Error in resolving.", io);
} catch (ComponentException ce) {
throw new SAXException("Unable to lookup component.", ce);
} finally {
if (null != resolver) {
resolver.release(stylesheet);
this.manager.release(resolver);
}
this.manager.release(processor);
}
}
protected String getStylesheetURI(PreparedConfiguration config, Layout layout)
throws SAXException {
// FIXME Get the stylesheet either from a layout attribute or another aspect
try {
String stylesheet = config.stylesheet.resolve();
return stylesheet;
} catch (PatternException pe) {
throw new SAXException("Pattern exception during variable resolving.", pe);
}
}
protected class PreparedConfiguration {
public VariableResolver stylesheet;
public String xsltRole;
public void takeValues(PreparedConfiguration from) {
this.stylesheet = from.stylesheet;
this.xsltRole = from.xsltRole;
}
}
/* (non-Javadoc)
* @see org.apache.cocoon.portal.layout.renderer.aspect.RendererAspect#prepareConfiguration(org.apache.avalon.framework.parameters.Parameters)
*/
public Object prepareConfiguration(Parameters configuration)
throws ParameterException {
PreparedConfiguration pc = new PreparedConfiguration();
pc.xsltRole = configuration.getParameter("xslt-processor-role", XSLTProcessor.ROLE);
String stylesheet = configuration.getParameter("style");
try {
pc.stylesheet = this.variableFactory.lookup( stylesheet );
} catch (PatternException pe) {
throw new ParameterException("Unknown pattern for stylesheet " + stylesheet, pe);
}
this.variables.add(pc.stylesheet);
return pc;
}
/* (non-Javadoc)
* @see org.apache.avalon.framework.activity.Disposable#dispose()
*/
public void dispose() {
if ( this.manager != null ) {
Iterator vars = this.variables.iterator();
while ( vars.hasNext() ) {
this.variableFactory.release( (VariableResolver) vars.next() );
}
this.variables.clear();
this.manager.release((Component) this.variableFactory);
this.manager = null;
this.variableFactory = null;
}
}
/* (non-Javadoc)
* @see org.apache.avalon.framework.component.Composable#compose(org.apache.avalon.framework.component.ComponentManager)
*/
public void compose(ComponentManager componentManager)
throws ComponentException {
super.compose(componentManager);
this.variableFactory = (VariableResolverFactory) this.manager.lookup(VariableResolverFactory.ROLE);
}
}