001: /*
002: * Created on Sep 23, 2005
003: */
004: package uk.org.ponder.rsf.renderer.html;
005:
006: import java.util.HashMap;
007: import java.util.Map;
008:
009: import uk.org.ponder.htmlutil.HTMLConstants;
010: import uk.org.ponder.rsf.renderer.ComponentRenderer;
011: import uk.org.ponder.rsf.renderer.RenderUtil;
012: import uk.org.ponder.rsf.renderer.scr.BasicSCR;
013: import uk.org.ponder.rsf.template.XMLLump;
014: import uk.org.ponder.rsf.view.BasedViewTemplate;
015: import uk.org.ponder.rsf.viewstate.ContextURLProvider;
016: import uk.org.ponder.rsf.viewstate.URLRewriter;
017: import uk.org.ponder.streamutil.write.PrintOutputStream;
018: import uk.org.ponder.xml.XMLUtil;
019: import uk.org.ponder.xml.XMLWriter;
020:
021: /** Performs a URL-rewrite for a template-relative
022: * "resource" URL (e.g. image or CSS) as found in a template.
023: * @author Antranig Basman (antranig@caret.cam.ac.uk)
024: */
025:
026: public class URLRewriteSCR implements BasicSCR {
027: public static final String NAME = "rewrite-url";
028: private URLRewriter rewriter;
029: private ContextURLProvider cup;
030:
031: // private String resourcebase;
032:
033: public String getName() {
034: return NAME;
035: }
036:
037: public void setURLRewriter(URLRewriter resolver) {
038: this .rewriter = resolver;
039: }
040:
041: public void setContextBaseProvider(ContextURLProvider cup) {
042: this .cup = cup;
043: }
044:
045: public String resolveURL(BasedViewTemplate template,
046: String toresolve) {
047: String extresourcebase = template.getExtResourceBase();
048: if (extresourcebase == null || extresourcebase == "") {
049: extresourcebase = cup.getContextBaseURL();
050: }
051: String resourcebase = extresourcebase
052: + template.getRelativeResourceBase();
053: return rewriter.rewriteResourceURL(toresolve, resourcebase);
054: }
055:
056: /**
057: * @param attrs
058: * A non-modifiable map of the attributes that may need to be
059: * filtered.
060: * @param name
061: * The name of an attribute whose value is a URL requiring rewriting.
062: * @param cloned
063: * Either a modifiable map of the attributes, or <code>null</code>
064: * if none yet exists.
065: * @return Either <code>null</code> if no modification was performed, or a
066: * modifiable attribute map.
067: */
068: public Map getResolvedURLMap(XMLLump lump, String name, Map cloned) {
069: String toresolve = (String) lump.attributemap.get(name);
070: if (toresolve == null || rewriter == null)
071: return cloned;
072: String resolved = resolveURL(lump.parent, toresolve);
073: if (resolved == null) {
074: return cloned;
075: } else {
076: Map togo = null;
077: if (cloned == null) {
078: togo = new HashMap();
079: togo.putAll(lump.attributemap);
080: } else {
081: togo = cloned;
082: }
083: togo.put(name, resolved);
084: return togo;
085: }
086: }
087:
088: public static String getLinkAttribute(XMLLump lump) {
089: for (int i = 0; i < HTMLConstants.tagtoURL.length; ++i) {
090: String[] tags = HTMLConstants.tagtoURL[i];
091: String tag = tags[0];
092: for (int j = 1; j < tags.length; ++j) {
093: if (lump.textEquals(tags[j]))
094: return tag;
095: }
096: }
097: return null;
098: }
099:
100: public Map rewriteURLs(XMLLump lump, Map attrcopy) {
101: String linkattr = getLinkAttribute(lump);
102: attrcopy = getResolvedURLMap(lump, linkattr, attrcopy);
103: for (int i = 0; i < HTMLConstants.ubiquitousURL.length; ++i) {
104: attrcopy = getResolvedURLMap(lump,
105: HTMLConstants.ubiquitousURL[i], attrcopy);
106: }
107: return attrcopy;
108: }
109:
110: public int render(XMLLump lump, XMLWriter xmlw) {
111: PrintOutputStream pos = xmlw.getInternalWriter();
112: Map newattrs = null;
113: XMLLump close = lump.close_tag;
114: XMLLump endopen = lump.open_end;
115: newattrs = rewriteURLs(lump, newattrs);
116:
117: xmlw.writeRaw(lump.parent.buffer, lump.start, lump.length);
118: if (newattrs == null) {
119: RenderUtil.dumpTillLump(lump.parent.lumps,
120: lump.lumpindex + 1, endopen.lumpindex + 1, pos);
121: } else {
122: newattrs.remove(XMLLump.ID_ATTRIBUTE);
123: XMLUtil.dumpAttributes(newattrs, xmlw);
124: if (endopen == close) {
125: pos.print("/>");
126: } else {
127: pos.print(">");
128: // RenderUtil.dumpTillLump(lumps, endopen.lumpindex + 1,
129: // close.lumpindex + 1, pos);
130: }
131: }
132: return ComponentRenderer.NESTING_TAG;
133: }
134:
135: }
|