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