001: /*
002: * Created on May 11, 2006
003: */
004: package uk.org.ponder.rsf.renderer;
005:
006: import java.util.Map;
007:
008: import uk.org.ponder.rsf.template.XMLLump;
009: import uk.org.ponder.rsf.uitype.UITypes;
010: import uk.org.ponder.streamutil.write.PrintOutputStream;
011: import uk.org.ponder.xml.XMLUtil;
012: import uk.org.ponder.xml.XMLWriter;
013:
014: /** The context necessary for a ComponentRenderer to do its work */
015:
016: public class TagRenderContext {
017: public boolean isEmpty() {
018: return endopen.lumpindex == close.lumpindex;
019: }
020:
021: public Map attrcopy;
022: public XMLLump uselump;
023: public XMLLump endopen;
024: public XMLLump close;
025: public PrintOutputStream pos;
026: public XMLWriter xmlw;
027: public boolean iselide;
028:
029: public int nextpos;
030:
031: public TagRenderContext(Map attrcopy, XMLLump uselump,
032: XMLLump endopen, XMLLump close, PrintOutputStream pos,
033: XMLWriter xmlw, int nextpos, boolean iselide) {
034: this .attrcopy = attrcopy;
035: this .uselump = uselump;
036: this .endopen = endopen;
037: this .close = close;
038: this .pos = pos;
039: this .xmlw = xmlw;
040: this .nextpos = nextpos;
041: this .iselide = iselide;
042: }
043:
044: public void closeTag() {
045: if (!iselide) {
046: pos.print("</");
047: pos.write(uselump.parent.buffer, uselump.start + 1,
048: uselump.length - 2);
049: pos.print(">");
050: }
051: }
052:
053: public void renderUnchanged() {
054: RenderUtil.dumpTillLump(uselump.parent.lumps,
055: uselump.lumpindex + 1, close.lumpindex
056: + (iselide ? 0 : 1), pos);
057: }
058:
059: public void rewriteLeaf(String value) {
060: if (value != null && !UITypes.isPlaceholder(value))
061: replaceBody(value);
062: else
063: replaceAttributes();
064: }
065:
066: public void rewriteLeafOpen(String value) {
067: if (iselide) {
068: rewriteLeaf(value);
069: } else {
070: if (value != null && !UITypes.isPlaceholder(value))
071: replaceBody(value);
072: else
073: replaceAttributesOpen();
074: }
075: }
076:
077: public void replaceBody(String value) {
078: XMLUtil.dumpAttributes(attrcopy, xmlw);
079: if (!iselide) {
080: pos.print(">");
081: }
082: xmlw.write(value);
083: closeTag();
084: }
085:
086: public void replaceAttributes() {
087: XMLUtil.dumpAttributes(attrcopy, xmlw);
088:
089: dumpTemplateBody();
090: }
091:
092: public void replaceAttributesOpen() {
093: if (iselide) {
094: replaceAttributes();
095: } else {
096: XMLUtil.dumpAttributes(attrcopy, xmlw);
097: pos.print(isEmpty() ? "/>" : ">");
098:
099: nextpos = endopen.lumpindex + 1;
100: }
101: }
102:
103: public void dumpTemplateBody() {
104: if (isEmpty()) {
105: if (!iselide) {
106: pos.print("/>");
107: }
108: } else {
109: pos.print(">");
110: RenderUtil.dumpTillLump(uselump.parent.lumps,
111: endopen.lumpindex + 1, close.lumpindex
112: + (iselide ? 0 : 1), pos);
113: }
114: }
115:
116: }
|