Package org.apache.cocoon.components.xslt

Source Code of org.apache.cocoon.components.xslt.XSLTProcessorImpl

/*

============================================================================
                   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.components.xslt;

import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.logger.AbstractLoggable;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.parameters.Parameterizable;
import org.apache.avalon.framework.parameters.ParameterException;

import org.apache.cocoon.ResourceNotFoundException;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.caching.CacheValidity;
import org.apache.cocoon.caching.TimeStampCacheValidity;
import org.apache.cocoon.caching.AggregatedCacheValidity;
import org.apache.cocoon.components.store.Store;
import org.apache.cocoon.environment.Source;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.util.ClassUtils;
import org.apache.cocoon.util.TraxErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.XMLFilter;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;

import javax.xml.transform.URIResolver;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Templates;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TemplatesHandler;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.ArrayList;

/**
* This class defines the implementation of the {@link XSLTProcessor}
* component.
*
* To configure it, add the following lines in the
* <file>cocoon.xconf</file> file:
*
* <pre>
* &lt;xslt-processor class="org.apache.cocoon.components.xslt.XSLTProcessorImpl"&gt;
*    &lt;parameter name="use-store" value="true"/&gt;
*    &lt;parameter name="transformer-factory" value="org.apache.xalan.processor.TransformerFactoryImpl"/&gt;
* &lt;/xslt-processor&gt;
* </pre>
*
* The &lt;use-store&gt; configuration forces the transformer to put the
* <code>Templates</code> generated from the XSLT stylesheet into the
* <code>Store</code>. This property is true by default.
* <p>
* The &lt;transformer-factory&gt; configuration tells the transformer to use a particular
* implementation of <code>javax.xml.transform.TransformerFactory</code>. This allows to force
* the use of a given TRAX implementation (e.g. xalan or saxon) if several are available in the
* classpath. If this property is not set, the transformer uses the standard TRAX mechanism
* (<code>TransformerFactory.newInstance()</code>).
*
* @author <a href="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
* @version CVS $Id: XSLTProcessorImpl.java,v 1.18.2.6 2002/09/21 04:03:51 vgritsenko Exp $
* @version 1.0
* @since   July 11, 2001
*/
public class XSLTProcessorImpl
  extends AbstractLoggable
  implements XSLTProcessor,
             Composable,
             Disposable,
             Parameterizable,
             URIResolver {

    protected ComponentManager manager;

    /** The store service instance */
    protected Store store;

    /** The trax TransformerFactory */
    protected SAXTransformerFactory tfactory;

    /** The factory class used to create tfactory */
    protected Class tfactoryClass;

    /** Is the store turned on? (default is on) */
    protected boolean useStore = true;

    /** Is incremental processing turned on? (default for Xalan: no) */
    protected boolean incrementalProcessing = false;

    /** Resolver used to resolve XSLT document() calls, imports and includes */
    protected SourceResolver resolver;

    /** List accumulating included stylesheets */
    protected List includes;

    /**
     * Compose. Try to get the store
     */
    public void compose(ComponentManager manager)
    throws ComponentException {
        this.manager = manager;
        getLogger().debug("XSLTProcessorImpl component initialized.");
        this.store = (Store)manager.lookup(Store.TRANSIENT_CACHE);
    }

    /**
     * Dispose
     */
    public void dispose() {
        if (this.manager != null) {
            this.manager.release(this.store);
            this.store = null;
        }

        this.manager = null;
        this.tfactoryClass = null;
        this.tfactory = null;
    }

    /**
     * Configure the component
     */
    public void parameterize(Parameters params)
    throws ParameterException {
        this.useStore = params.getParameterAsBoolean("use-store", true);
        this.incrementalProcessing = params.getParameterAsBoolean("incremental-processing", false);

        String factoryName = params.getParameter("transformer-factory", null);
        if (factoryName == null) {
            // Will use default TRAX mechanism
            this.tfactoryClass = null;
        } else {
            // Will use specific class
            getLogger().debug("Using factory " + factoryName);
            try {
                this.tfactoryClass = ClassUtils.loadClass(factoryName);
            } catch(ClassNotFoundException cnfe) {
                throw new ParameterException("Cannot load TransformerFactory class", cnfe);
            }

            if (! TransformerFactory.class.isAssignableFrom(tfactoryClass)) {
                throw new ParameterException("Class " + factoryName + " isn't a TransformerFactory");
            }
        }
    }

    public CacheValidity getTransformerValidity(Source stylesheet)
    {
        // No lastModified - no validity
        if (stylesheet.getLastModified() == 0) {
            return null;
        }

        // No store - fallback to previous implementation
        if (!useStore) {
            return new TimeStampCacheValidity(stylesheet.getLastModified());
        }

        // Get data from cache
        String id = stylesheet.getSystemId();
        Object[] templateAndTimeAndIncludes = (Object[])store.get(id);
        if (templateAndTimeAndIncludes != null && templateAndTimeAndIncludes[2] != null) {
            // Create aggregated validity
            List includes = (List)templateAndTimeAndIncludes[2];
            AggregatedCacheValidity validity = new AggregatedCacheValidity();
            validity.add(new TimeStampCacheValidity(stylesheet.getLastModified()));
            for (int i = includes.size() - 1; i >= 0; i--) {
                Object[] pair = (Object[])includes.get(i);
                long time = ((Long)pair[1]).longValue();
                validity.add(new TimeStampCacheValidity(time));
            }
            return validity;
        } else {
            // No data - fallback to previous implementation
            return new TimeStampCacheValidity(stylesheet.getLastModified());
        }
    }

    public TransformerHandler getTransformerHandler(Source stylesheet)
    throws ProcessingException {
        return getTransformerHandler(stylesheet, null);
    }

    public TransformerHandler getTransformerHandler(Source stylesheet,
                                                    XMLFilter filter)
    throws ProcessingException {
        try {
            final String id = stylesheet.getSystemId();
            Templates templates = getTemplates(stylesheet, id);
            if (templates == null) {
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("Creating new Templates for " + id);
                }
                // Create a Templates ContentHandler to handle parsing of the
                // stylesheet.
                TemplatesHandler templatesHandler
                    = getTransformerFactory().newTemplatesHandler();

                if (filter != null) {
                    filter.setContentHandler(templatesHandler);
                }

                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("Source = " + stylesheet
                        + ", templatesHandler = " + templatesHandler);
                }

                // Process the stylesheet.
                includes = new ArrayList();
                stylesheet.toSAX(filter != null ?
                            (ContentHandler)filter : (ContentHandler)templatesHandler);

                // Get the Templates object (generated during the parsing of
                // the stylesheet) from the TemplatesHandler.
                templates = templatesHandler.getTemplates();

                if( null == templates )
                {
                    throw new ProcessingException( "Unable to create templates for stylesheet: "
                                                   + stylesheet.getSystemId() );
                }

                putTemplates (templates, stylesheet, id);
                includes = null;
            } else {
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("Reusing Templates for " + id);
                }
            }

            TransformerHandler handler = getTransformerFactory().newTransformerHandler(templates);
            handler.getTransformer().setErrorListener(new TraxErrorHandler(getLogger()));
            handler.getTransformer().setURIResolver(this);
            return handler;
        } catch (ProcessingException e) {
            throw e;
        } catch (SAXException e) {
            if (e.getException() == null) {
                throw new ProcessingException("Exception in creating Transform Handler", e);
            } else {
                getLogger().debug("Got SAXException. Rethrowing cause exception.", e);
                throw new ProcessingException("Exception in creating Transform Handler", e.getException());
            }
        } catch (Exception e) {
            throw new ProcessingException("Exception in creating Transform Handler", e);
        }
    }

    public void transform(Source source,
                          Source stylesheet,
                          Parameters params,
                          Result result)
    throws ProcessingException {
        try {
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("XSLTProcessorImpl: transform source = " + source
                    + ", stylesheet = " + stylesheet
                    + ", parameters = " + params
                    + ", result = " + result);
            }
            TransformerHandler handler = getTransformerHandler(stylesheet);
            if (params != null) {
                Transformer transformer = handler.getTransformer();
                transformer.clearParameters();
                String[] names = params.getNames();
                for (int i = names.length -1 ; i >= 0; i--) {
                    transformer.setParameter(names[i], params.getParameter(names[i]));
                }
            }

            handler.setResult(result);
            source.toSAX(handler);
            getLogger().debug("XSLTProcessorImpl: transform done");
        } catch (ProcessingException e) {
            throw e;
        } catch (ParameterException e) {
            throw new ProcessingException("Error in running Transformation", e);
        } catch (SAXException e) {
            if (e.getException() == null) {
                throw new ProcessingException("Error in running Transformation", e);
            } else {
                getLogger().debug("Got SAXException. Rethrowing cause exception.", e);
                throw new ProcessingException("Error in running Transformation", e.getException());
            }
        }
    }

    /**
     * Helper for TransformerFactory.
     */
    private SAXTransformerFactory getTransformerFactory() throws Exception {
        if(tfactory == null) {
            if (tfactoryClass == null) {
                tfactory = (SAXTransformerFactory)TransformerFactory.newInstance();
            } else {
                tfactory = (SAXTransformerFactory)tfactoryClass.newInstance();
            }
            tfactory.setErrorListener(new TraxErrorHandler(getLogger()));
            tfactory.setURIResolver(this);
            // TODO: If we will support this feature with a different
            // transformer than Xalan we'll have to set that corresponding
            // feature
            if (tfactory.getClass().getName().equals("org.apache.xalan.processor.TransformerFactoryImpl")) {
                tfactory.setAttribute("http://xml.apache.org/xalan/features/incremental",
                        new Boolean (incrementalProcessing));
            }
        }
        return tfactory;
    }

    private Templates getTemplates(Source stylesheet, String id)
    throws ProcessingException, IOException, SAXException {
        if (!useStore) {
            return null;
        }

        if (getLogger().isDebugEnabled()) {
            getLogger().debug("XSLTProcessorImpl getTemplates: stylesheet " + id);
        }

        // Only stylesheets with a last modification date are stored
        if (stylesheet.getLastModified() == 0) {
            // Remove an old template
            store.remove(id);
            return null;
        }

        // Stored is an array of the templates and the caching time and list of includes
        Object[] templateAndTimeAndIncludes = (Object[])store.get(id);
        if (templateAndTimeAndIncludes == null) {
            // Templates not found in cache
            return null;
        }

        // Check template modification time
        long storedTime = ((Long)templateAndTimeAndIncludes[1]).longValue();
        if (storedTime != stylesheet.getLastModified()) {
            store.remove(id);
            return null;
        }

        // Check includes
        List includes = (List)templateAndTimeAndIncludes[2];
        if (includes != null) {
            for (int i = includes.size() - 1; i >= 0; i--) {
                // Every include stored as pair of source ID and timestamp
                Object[] pair = (Object[])includes.get(i);
                Source included = resolver.resolve((String)pair[0]);
                if (included.getLastModified() != ((Long)pair[1]).longValue()) {
                    store.remove(id);
                    return null;
                }
            }
        }

        // Templates were not modified, return from cache
        return (Templates)templateAndTimeAndIncludes[0];
    }

    private void putTemplates (Templates templates, Source stylesheet, String id)
    throws IOException {
        if (!useStore) {
            return;
        }

        // Only stylesheets with a last modification date are stored
        if (stylesheet.getLastModified() != 0) {
            // Check that included stylesheets are cacheable
            for (int i = includes.size() - 1; i >= 0; i--) {
                Object[] pair = (Object[])includes.get(i);
                if (((Long)pair[1]).longValue() == 0) {
                    return;
                }
            }

            // Stored is an array of the template and the current time
            Object[] templateAndTimeAndIncludes = new Object[3];
            templateAndTimeAndIncludes[0] = templates;
            templateAndTimeAndIncludes[1] = new Long(stylesheet.getLastModified());
            if (includes.size() > 0) {
                templateAndTimeAndIncludes[2] = includes;
            }
            store.hold(id, templateAndTimeAndIncludes);
        }
    }

    /**
     * Called by the processor when it encounters
     * an xsl:include, xsl:import, or document() function.
     *
     * @param href An href attribute, which may be relative or absolute.
     * @param base The base URI in effect when the href attribute
     * was encountered.
     *
     * @return A Source object, or null if the href cannot be resolved,
     * and the processor should try to resolve the URI itself.
     *
     * @throws TransformerException if an error occurs when trying to
     * resolve the URI.
     */
    public javax.xml.transform.Source resolve(String href, String base)
    throws TransformerException {
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("resolve(href = " + href +
                              ", base = " + base + "); resolver = " + resolver);
        }

        Source xslSource = null;
        try {
            if (base == null || href.indexOf(":") > 1) {
                // Null base - href must be an absolute URL
                xslSource = resolver.resolve(href);
            } else if (href.length() == 0) {
                // Empty href resolves to base
                xslSource = resolver.resolve(base);
            } else {
                // is the base a file or a real url
                if (!base.startsWith("file:")) {
                    int lastPathElementPos = base.lastIndexOf('/');
                    if (lastPathElementPos == -1) {
                        // this should never occur as the base should
                        // always be protocol:/....
                        return null; // we can't resolve this
                    } else {
                        xslSource = resolver.resolve(
                            base.substring(0, lastPathElementPos) + "/" + href);
                    }
                } else {
                    File parent = new File(base.substring(5));
                    File parent2 = new File(parent.getParentFile(), href);
                    xslSource = resolver.resolve(parent2.toURL().toExternalForm());
                }
            }

            if (getLogger().isDebugEnabled()) {
                getLogger().debug("xslSource = " + xslSource
                    + ", system id = " + xslSource.getSystemId());
            }

            InputSource is = xslSource.getInputSource();
            if (includes != null) {
                includes.add(new Object[]{xslSource.getSystemId(), new Long(xslSource.getLastModified())});
            }
            return new StreamSource(is.getByteStream(), is.getSystemId());
        } catch (ResourceNotFoundException rnfe) {
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("Failed to resolve " + href
                    + "(base = " + base + "), return null", rnfe);
            }

            // CZ: To obtain the same behaviour as when the resource is
            // transformed by the XSLT Transformer we should return null here.
            return null;
        } catch (MalformedURLException mue) {
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("Failed to resolve " + href
                    + "(base = " + base + "), return null", mue);
            }

            return null;
        } catch (IOException ioe) {
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("Failed to resolve " + href
                    + "(base = " + base + "), return null", ioe);
            }

            return null;
        } catch (SAXException se) {
            throw new TransformerException(se);
        } catch (ProcessingException pe) {
            throw new TransformerException(pe);
        } finally {
            if (xslSource != null) xslSource.recycle();
        }
    }

    public void setSourceResolver(SourceResolver resolver) {
        this.resolver = resolver;
    }
}
TOP

Related Classes of org.apache.cocoon.components.xslt.XSLTProcessorImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.