01: package com.opensymphony.module.sitemesh.multipass;
02:
03: import com.opensymphony.module.sitemesh.html.BasicRule;
04: import com.opensymphony.module.sitemesh.html.State;
05: import com.opensymphony.module.sitemesh.html.Tag;
06: import com.opensymphony.module.sitemesh.html.rules.PageBuilder;
07: import com.opensymphony.module.sitemesh.html.util.CharArray;
08: import com.opensymphony.module.sitemesh.parser.HTMLPageParser;
09:
10: public class DivExtractingPageParser extends HTMLPageParser {
11:
12: protected void addUserDefinedRules(State html,
13: final PageBuilder page) {
14: super .addUserDefinedRules(html, page);
15: html.addRule(new TopLevelDivExtractingRule(page));
16: }
17:
18: private static class TopLevelDivExtractingRule extends BasicRule {
19: private String blockId;
20: private int depth;
21: private final PageBuilder page;
22:
23: public TopLevelDivExtractingRule(PageBuilder page) {
24: super ("div");
25: this .page = page;
26: }
27:
28: public void process(Tag tag) {
29: if (tag.getType() == Tag.OPEN) {
30: String id = tag.getAttributeValue("id", false);
31: if (depth == 0 && id != null) {
32: currentBuffer().append(
33: "<sitemesh:multipass id=\"div." + id
34: + "\"/>");
35: blockId = id;
36: context.pushBuffer(new CharArray(512));
37: }
38: tag.writeTo(currentBuffer());
39: depth++;
40: } else if (tag.getType() == Tag.CLOSE) {
41: depth--;
42: tag.writeTo(currentBuffer());
43: if (depth == 0 && blockId != null) {
44: page.addProperty("div." + blockId, currentBuffer()
45: .toString());
46: blockId = null;
47: context.popBuffer();
48: }
49: }
50: }
51: }
52: }
|