private void apply(String style,
                       String source,
                       HttpServletRequest req,
                       HttpServletResponse res) throws Exception {

        // Validate parameters
        if (style == null) {
            throw new TransformerException("No style parameter supplied");
        }
        if (source == null) {
            throw new TransformerException("No source parameter supplied");
        }

        InputStream sourceStream = null;
        try {
            // Load the stylesheet (adding to cache if necessary)
            Templates pss = tryCache(style);
            Transformer transformer = pss.newTransformer();

            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));
                }
            }

            // Start loading the document to be transformed
            sourceStream = getInputStream(source);

            // Set the appropriate output mime type
            String mime =
                    pss.getOutputProperties()
                            .getProperty(OutputKeys.MEDIA_TYPE);
            if (mime == null) {
                res.setContentType("text/html");
            } else {
                res.setContentType(mime);
            }

            // Transform
            StreamSource ss = new StreamSource(sourceStream);
            ss.setSystemId(source);
            transformer.transform(ss, new StreamResult(res.getOutputStream()));

        } finally {
            if (sourceStream != null) {
                try {
                    sourceStream.close();
                } catch (Exception e) {
                }
            }
        }

    }
private void apply ( String style , String source , HttpServletRequest req , HttpServletResponse res ) throws Exception { if ( style == null ) { throw new TransformerException ( "No style parameter supplied" ) ; } if ( source == null ) { throw new TransformerException ( "No source parameter supplied" ) ; } InputStream sourceStream = null ; try { Templates pss = tryCache ( style ) ; Transformer transformer = pss . newTransformer ( ) ; 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 ) ) ; } } sourceStream = getInputStream ( source ) ; String mime = pss . getOutputProperties ( ) . getProperty ( OutputKeys . MEDIA_TYPE ) ; if ( mime == null ) { res . setContentType ( "text/html" ) ; } else { res . setContentType ( mime ) ; } StreamSource ss = new StreamSource ( sourceStream ) ; ss . setSystemId ( source ) ; transformer . transform ( ss , new StreamResult ( res . getOutputStream ( ) ) ) ; } finally { if ( sourceStream != null ) { try { sourceStream . close ( ) ; } catch ( Exception e ) { } } } }
Apply stylesheet to source document