12.5. Special-Case Stylesheets

You may occasionally want to override the default stylesheet rules for one particular page. Because of the way XSLT works, you can choose to override only some template match=... rules, allowing others to be handled by the default stylesheet.

You should handle this in the dispatcher for your application, or by overriding the getStylesheetTransformer() or servePage methods in a PresentationManager class specific to your application. For example:

public class SockPuppetPresentationManager extends 
    BasePresentationManager {

    public void servePage(Page p, 
                          HttpServletRequest req, 
                          HttpServletResponse resp) 
        throws IOException, ServletException {

        RequestContext rctx = DispatcherHelper.getRequestContext(req);
        String url = rctx.getRemainingURLPart();

        // serve page as usual, except for URLs foo/*; those get a 
        // special-case stylesheet
        if (!url.startsWith("foo/")) {
            super.servePage(p, req, resp);
        } else {
            // use custom stylesheet for foo/*
            // in reality, probably cache Transformer for performance
            Stylesheet fooSS = ... get overridden rules 
               from somewhere ...;
            Transformer xformer = fooSS.newTransformer();
            Document doc = p.buildDocument(req, resp);
            Writer out = resp.getWriter();
            xformer.transform(new DOMSource(doc), new StreamResult(out));
        }
    }
}