public Page parse(SitemeshBuffer buffer) throws IOException {
SitemeshBufferFragment.Builder head = SitemeshBufferFragment.builder().setBuffer(buffer).setLength(0);
SitemeshBufferFragment.Builder body = SitemeshBufferFragment.builder().setBuffer(buffer);
TokenizedHTMLPage page = new TokenizedHTMLPage(buffer);
HTMLProcessor processor = new HTMLProcessor(buffer, body);
State html = processor.defaultState();
// Core rules for SiteMesh to be functional.
html.addRule(new HeadExtractingRule(head)); // contents of <head>
html.addRule(new BodyTagRule(page, body)); // contents of <body>
html.addRule(new TitleExtractingRule(page)); // the <title>
html.addRule(new FramesetRule(page)); // if the page is a frameset
// Additional rules - designed to be tweaked.
addUserDefinedRules(html, page);
processor.process();
page.setBody(body.build());
page.setHead(head.build());
return page;
}