RE: [xsl] tomcat web server and dynamic XSL

Subject: RE: [xsl] tomcat web server and dynamic XSL
From: David.Pawson@xxxxxxxxxxx
Date: Tue, 27 Apr 2004 11:03:15 +0100
MK said.
    That's not the way it works. You have to write some servlet 
    code (or install some code that's already been written) 
    that uses the JAXP interface to invoke the transformation.

This is a part of a tomcat servlet I had working for the faq.

It may help. The full file pulls from mySQL too, but I don't think
that's of interest?

HTH DaveP


    /************************************************************/
   
    /**
     * Apply stylesheet to source document and display
     *@param style - stylesheet to use, as a string
     *@param source - Source XML file, as string
     *@param request
     *@param response 
     *@return void
     **/

    private void apply(String style, String source,
                           HttpServletRequest req, HttpServletResponse res)
                           throws TransformerException, java.io.IOException
{
                            
        //ServletOutputStream out = res.getOutputStream();
	//Above removed; reported
	//java.lang.IllegalStateException: getWriter() has already been
called for this response


        if (style==null) {
            out.println("No style parameter supplied");
            return;
        }
        if (source==null) {
            out.println("No source parameter supplied");
            return;
        }
        try {
            Templates pss = tryCache(style);
            Transformer transformer = pss.newTransformer();
            Properties details = pss.getOutputProperties();

            String mime =
pss.getOutputProperties().getProperty(OutputKeys.MEDIA_TYPE);
            if (mime==null) {
               // guess
                res.setContentType("text/html");
            } else {
                res.setContentType(mime);
            }

            Enumeration p = req.getParameterNames();
            while (p.hasMoreElements()) {
                String name = (String)p.nextElement();
                if (!(name.equals("style") || name.equals("source"))) {
                    String value = req.getParameter(name);
                    transformer.setParameter(name, new StringValue(value));
                }
            }
	
            // FIXME String path = getServletContext().getRealPath(source);
	    String path = getServletContext().getRealPath("N1553.1");
            if (path==null) {
                throw new TransformerException("Source file " + source + "
not found");
            }
            File sourceFile = new File(path);
            transformer.transform(new StreamSource(sourceFile), new
StreamResult(out));
        } catch (Exception err) {
            out.println(err.getMessage());
            err.printStackTrace();
        }

    }

    /**
    * Maintain prepared stylesheets in memory for reuse
    */
    private synchronized Templates tryCache(String url) 
	throws TransformerException, java.io.IOException {
        String path = getServletContext().getRealPath(url);
        if (path==null) {
            throw new TransformerException("Stylesheet " + url + " not
found");
        }
        
        Templates x = (Templates)cache.get(path);
        if (x==null) {
            TransformerFactory factory = TransformerFactory.newInstance();
// These two  calls to enable the resolver.
	    String r = "com.sun.resolver.tools.CatalogResolver";
	    factory.setURIResolver(makeURIResolver(r));

	    factory.setAttribute
("http://icl.com/saxon/feature/styleParserClass";,
	    "com.sun.resolver.tools.ResolvingXMLReader");  

	    // factory.setAttribute("STYLE_PARSER_CLASS",
	    // "com.sun.resolver.tools.ResolvingXMLReader");
	
//factory.setAttribute("com.icl.saxon.FeatureKeys.SOURCE_PARSER_CLASS",
	    //		 "com.sun.resolver.tools.ResolvingXMLReader");

	
factory.setAttribute("http://icl.com/saxon/feature/sourceParserClass";,
	
"com.sun.resolver.tools.ResolvingXMLReader");

	    
            x = factory.newTemplates(new StreamSource(new File(path)));
            cache.put(path, x);
        }
        return x;
    }

    /**
    * Clear the cache. Useful if stylesheets have been modified, or simply
if space is
    * running low. We let the garbage collector do the work.
    */

    private synchronized void clearCache() {
        cache = new Hashtable();
    }

    private Hashtable cache = new Hashtable();

- 
DISCLAIMER: 

NOTICE: The information contained in this email and any attachments is 
confidential and may be privileged. If you are not the intended 
recipient you should not use, disclose, distribute or copy any of the 
content of it or of any attachment; you are requested to notify the 
sender immediately of your receipt of the email and then to delete it 
and any attachments from your system. 

RNIB endeavours to ensure that emails and any attachments generated by 
its staff are free from viruses or other contaminants. However, it 
cannot accept any responsibility for any  such which are transmitted.
We therefore recommend you scan all attachments. 

Please note that the statements and views expressed in this email and 
any attachments are those of the author and do not necessarily represent 
those of RNIB. 

RNIB Registered Charity Number: 226227 

Website: http://www.rnib.org.uk 

Current Thread