01: /*
02: * Created on 18 Sep 2006
03: */
04: package uk.org.ponder.rsf.renderer.html;
05:
06: import java.util.HashSet;
07: import java.util.Set;
08:
09: import uk.org.ponder.rsf.renderer.ComponentRenderer;
10: import uk.org.ponder.rsf.renderer.RenderUtil;
11: import uk.org.ponder.rsf.renderer.scr.CollectingSCR;
12: import uk.org.ponder.rsf.template.XMLLump;
13: import uk.org.ponder.rsf.template.XMLLumpList;
14: import uk.org.ponder.streamutil.write.PrintOutputStream;
15: import uk.org.ponder.xml.XMLWriter;
16:
17: /**
18: * A basic collector of <head> material for HTML pages. Will emit all
19: * collected <style> and <script> tags, and leave the tag in an open
20: * condition.
21: *
22: * @author Antranig Basman (antranig@caret.cam.ac.uk)
23: *
24: */
25:
26: public class HeadCollectingSCR implements CollectingSCR {
27: public static final String NAME = "head-collect";
28: private URLRewriteSCR urlRewriteSCR;
29:
30: public String getName() {
31: return NAME;
32: }
33:
34: public String[] getCollectingNames() {
35: return new String[] { "style", "script" };
36: }
37:
38: public void setURLRewriteSCR(URLRewriteSCR urlRewriteSCR) {
39: this .urlRewriteSCR = urlRewriteSCR;
40: }
41:
42: public int render(XMLLump lump, XMLLumpList collected,
43: XMLWriter xmlw) {
44: PrintOutputStream pos = xmlw.getInternalWriter();
45: RenderUtil.dumpTillLump(lump.parent.lumps, lump.lumpindex,
46: lump.open_end.lumpindex + 1, pos);
47: Set used = new HashSet();
48: for (int i = 0; i < collected.size(); ++i) {
49: XMLLump collump = collected.lumpAt(i);
50: String attr = URLRewriteSCR.getLinkAttribute(collump);
51: if (attr != null) {
52: String attrval = (String) collump.attributemap
53: .get(attr);
54: if (attrval != null) {
55: String rewritten = urlRewriteSCR.resolveURL(
56: collump.parent, attrval);
57: if (rewritten == null)
58: rewritten = attrval;
59: int qpos = rewritten.indexOf('?');
60: if (qpos != -1)
61: rewritten = rewritten.substring(0, qpos);
62: if (used.contains(rewritten))
63: continue;
64: else
65: used.add(rewritten);
66: }
67: }
68: // TODO: equivalent of TagRenderContext for SCRs
69: urlRewriteSCR.render(collump, xmlw);
70: RenderUtil.dumpTillLump(collump.parent.lumps,
71: collump.open_end.lumpindex + 1,
72: collump.close_tag.lumpindex + 1, pos);
73: }
74: return ComponentRenderer.NESTING_TAG;
75: }
76:
77: }
|