StylesheetFilter.javaAPI DocExample3585Sun Sep 02 14:59:04 BST 2001com.oreilly.javaxslt.util


public class StylesheetFilter extends Object implements Filter
A utility class that uses the Servlet 2.3 Filtering API to apply an XSLT stylesheet to a servlet response.
Eric M. Burke

Fields Summary
private FilterConfig
private String
Constructors Summary
Methods Summary
public voiddestroy()
The counterpart to the init() method.

        this.filterConfig = null;
public voiddoFilter(javax.servlet.ServletRequest req, javax.servlet.ServletResponse res, javax.servlet.FilterChain chain)

        if (!(res instanceof HttpServletResponse)) {
            throw new ServletException("This filter only supports HTTP");

        BufferedHttpResponseWrapper responseWrapper =
                new BufferedHttpResponseWrapper((HttpServletResponse) res);
        chain.doFilter(req, responseWrapper);

        // Tomcat 4.0 reuses instances of its HttpServletResponse
        // implementation class in some scenarios. For instance, hitting
        // reload() repeatedly on a web browser will cause this to happen.
        // Unfortunately, when this occurs, output is never written to the
        // BufferedHttpResponseWrapper's OutputStream. This means that the
        // XML output array is empty when this happens. The following
        // code is a workaround:
        byte[] origXML = responseWrapper.getBuffer();
        if (origXML == null || origXML.length == 0) {
            // just let Tomcat deliver its cached data back to the client
            chain.doFilter(req, res);

        try {
            // do the XSLT transformation
            Transformer trans = StylesheetCache.newTransformer(

            ByteArrayInputStream origXMLIn = new ByteArrayInputStream(origXML);
            Source xmlSource = new StreamSource(origXMLIn);

            ByteArrayOutputStream resultBuf = new ByteArrayOutputStream();
            trans.transform(xmlSource, new StreamResult(resultBuf));

        } catch (TransformerException te) {
            throw new ServletException(te);
public voidinit(javax.servlet.FilterConfig filterConfig)
This method is called once when the filter is first loaded.

        this.filterConfig = filterConfig;

        // xsltPath should be something like "/WEB-INF/xslt/a.xslt"
        String xsltPath = filterConfig.getInitParameter("xsltPath");
        if (xsltPath == null) {
            throw new UnavailableException(
                    "xsltPath is a required parameter. Please "
                    + "check the deployment descriptor.");

        // convert the context-relative path to a physical path name
        this.xsltFileName = filterConfig.getServletContext()

        // verify that the file exists
        if (this.xsltFileName == null ||
                !new File(this.xsltFileName).exists()) {
            throw new UnavailableException(
                    "Unable to locate stylesheet: " + this.xsltFileName, 30);