views:

109

answers:

1

I have custom JSP tags that generate some HTML content, along with some javascript functions that get called by this HTML code. In the current implementation, the SCRIPT tags are created just above the HTML code.

To avoid modifying the existing code base, I want to pull up these scripts inside the HEAD section of the page using SiteMesh or some other decorator tool.

I know SiteMesh can extract content from <content tag="..."> elements, but I was wondering if it was possible also with other tags, such as SCRIPT.

Is this possible with SiteMesh, or know of any tools that could allow me to do that?

Thank you!

A: 

SiteMesh's HTMLPageParser is extensible, so you can add your own custom rule to extract <script> elements by extending HTMLPageParser and configuring SiteMesh to use your class instead of HTMLPageParser, something like this:

import com.opensymphony.module.sitemesh.parser.HTMLPageParser;

public CustomPageParser extends HTMLPageParser {

    protected void addUserDefinedRules(State html, PageBuilder page) {
        super.addUserDefinedRules(html, page);

        html.addRule(new ScriptExtractingRule(page));
    }

}

I imagine your ScriptExtractingRule would be modeled after the standard SiteMesh ContentBlockExtractingRule, storing the content in the page context so your decorator can access the blocks as if they were <content> blocks.

ZoogieZork