01: /*
02: * Created on 19 Aug 2006
03: */
04: package uk.org.ponder.rsf.renderer.html;
05:
06: import java.util.HashMap;
07: import java.util.Map;
08:
09: import uk.org.ponder.htmlutil.HTMLConstants;
10: import uk.org.ponder.rsf.content.ContentTypeInfoRegistry;
11: import uk.org.ponder.rsf.template.ContentTypedTPI;
12: import uk.org.ponder.rsf.template.XMLLump;
13: import uk.org.ponder.stringutil.URLUtil;
14:
15: /**
16: * This TemplateParseInterceptor automatically marks up all relative URLs on
17: * relevant HTML tags with the rsf:id="scr=rewrite-url" attribute.
18: *
19: * @author Antranig Basman (antranig@caret.cam.ac.uk)
20: */
21:
22: public class RelativeURLInferringTPI implements ContentTypedTPI {
23:
24: public static Map tagToAttrName = new HashMap();
25:
26: static {
27: for (int i = 0; i < HTMLConstants.tagtoURL.length; ++i) {
28: String[] tags = HTMLConstants.tagtoURL[i];
29: for (int j = 1; j < tags.length; ++j) {
30: tagToAttrName.put(XMLLump.textToTag(tags[j]), tags[0]);
31: }
32: }
33: }
34:
35: public void adjustAttributes(String tag, Map attributes) {
36: String attrname = (String) tagToAttrName.get(tag);
37: if (attrname == null)
38: return;
39: if (attributes.get(XMLLump.ID_ATTRIBUTE) != null)
40: return;
41: String url = (String) attributes.get(attrname);
42: if (url == null || URLUtil.isAbsolute(url) || url.equals("")
43: || url.charAt(0) == '/' || url.charAt(0) == '#')
44: return;
45: attributes.put(XMLLump.ID_ATTRIBUTE, XMLLump.SCR_PREFIX
46: + URLRewriteSCR.NAME);
47: }
48:
49: public String[] getInterceptedContentTypes() {
50: return new String[] { ContentTypeInfoRegistry.HTML,
51: ContentTypeInfoRegistry.HTML_FRAGMENT };
52: }
53:
54: }
|