FileDocCategorySizeDatePackage
XSLTRenderHelper.javaAPI DocExample2558Sun Sep 02 14:59:04 BST 2001com.oreilly.forum.servlet

XSLTRenderHelper

public class XSLTRenderHelper extends Object
A helper class that makes rendering of XSLT easier. This eliminates the need to duplicate a lot of code for each of the web pages in this app.

Fields Summary
private static Map
filenameCache
Constructors Summary
private XSLTRenderHelper()

    
Methods Summary
public static voidrender(javax.servlet.http.HttpServlet servlet, Document xmlJDOMData, java.lang.String xsltBaseName, javax.servlet.http.HttpServletResponse response)
Perform an XSLT transformation.

param
servlet provides access to the ServletContext so the XSLT directory can be determined.
param
xmlJDOMData JDOM data for the XML Document.
param
xsltBaseName the name of the stylesheet without a directory.
param
response the Servlet response to write output to.


                                                                     
          
               
               

        String xsltFileName = null;
        try {
            // figure out the complete XSLT stylesheet file name
            synchronized (filenameCache) {
                xsltFileName = (String) filenameCache.get(xsltBaseName);
                if (xsltFileName == null) {
                    ServletContext ctx = servlet.getServletContext();
                    xsltFileName = ctx.getRealPath(
                            "/WEB-INF/xslt/" + xsltBaseName);
                    filenameCache.put(xsltBaseName, xsltFileName);
                }
            }

            // write the JDOM data to a StringWriter
            StringWriter sw = new StringWriter();
            XMLOutputter xmlOut = new XMLOutputter("", false, "UTF-8");
            xmlOut.output(xmlJDOMData, sw);

            response.setContentType("text/html");
            Transformer trans = StylesheetCache.newTransformer(xsltFileName);

            // pass a parameter to the XSLT stylesheet
            trans.setParameter("rootDir", "/forum/");

            trans.transform(new StreamSource(new StringReader(sw.toString())),
                            new StreamResult(response.getWriter()));
        } catch (IOException ioe) {
            throw ioe;
        } catch (Exception ex) {
            throw new ServletException(ex);
        }