1 10 package org.mmbase.bridge.jsp.taglib.tree; 11 12 import java.io.IOException ; 13 import java.util.Stack ; 14 15 import javax.servlet.jsp.*; 16 17 import org.mmbase.bridge.jsp.taglib.*; 18 import org.mmbase.util.Casting; 19 import org.mmbase.util.logging.*; 20 21 26 public class ShrinkTag extends AbstractTreeReferrerListTag implements Writer { 27 28 private static final Logger log = Logging.getLoggerInstance(ShrinkTag.class); 29 private int startDepth; 30 private int endDepth; 31 private int size; 32 private boolean foundSize; 33 private boolean foundBody; 34 private boolean write; 35 36 public int size() { 37 if (! foundSize) { size = index; 39 while (((Entry) tree.getShrinkStack().peek()).depth < endDepth) { 40 size ++; 41 } 42 foundSize = true; 43 } 44 return size; 45 } 46 47 48 public int doStartTag() throws JspTagException { 49 log.debug("starttag"); 50 doStartTagHelper(); 51 52 Stack stack = tree.getShrinkStack(); 53 54 55 if (log.isDebugEnabled()) { 56 log.debug("Shrinking " + stack + " to " + tree.getNextDepth()); 57 } 58 if (stack.size() == 0) return SKIP_BODY; 59 60 Entry entry = (Entry) stack.peek(); 61 62 startDepth = entry.depth; 63 depth = startDepth; 64 endDepth = tree.getNextDepth(); 65 foundSize = false; 66 foundBody = false; 67 write = false; 68 69 helper.setValue(""); 70 helper.useEscaper(false); 71 72 if (depth >= endDepth) { 73 return EVAL_BODY_BUFFERED; 74 } else { 75 log.debug("nothing to shrink"); 76 return SKIP_BODY; 77 } 78 } 79 public void doInitBody() throws JspException { 80 log.debug("initbody"); 82 Stack stack = tree.getShrinkStack(); 83 if (stack.size() > 0) { 84 Entry entry = (Entry) stack.peek(); 85 log.debug("making available now " + entry.string); 86 helper.setValue(entry.string); 87 helper.doAfterBody(); 88 write = helper.getWrite().getBoolean(this, false); 89 if (write) { 90 try { 91 pageContext.getOut().write(Casting.toString(helper.getValue())); 92 } catch (IOException ioe){ 93 throw new TaglibException(ioe); 94 } 95 } 96 } 97 super.doInitBody(); 98 } 99 100 101 public int doAfterBody() throws JspTagException { 102 log.debug("afterbody"); 103 collector.doAfterBody(); 104 if (index == 0) { 105 foundBody = true; 106 } 107 108 Stack stack = tree.getShrinkStack(); 109 110 stack.pop(); 111 if (stack.size() > 0) { 112 Entry entry = (Entry) stack.peek(); 113 depth = entry.depth; 114 if (depth >= endDepth) { 115 log.debug("making available now " + entry.string); 116 helper.setValue(entry.string); 117 if (write) { 118 try { 119 pageContext.getOut().write(Casting.toString(helper.getValue())); 120 } catch (IOException ioe){ 121 throw new TaglibException(ioe); 122 } 123 } 124 125 index++; 126 return EVAL_BODY_AGAIN; 127 } 128 } 129 130 131 if (bodyContent != null) { 132 try { 133 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 134 } catch (IOException ioe){ 135 throw new TaglibException(ioe); 136 } 137 } 138 if (log.isDebugEnabled()) { 139 log.debug("stack now " + stack); 140 } 141 return SKIP_BODY; 142 143 144 145 } 146 147 public int doEndTag() throws JspTagException { 148 log.debug("endtag"); 149 if (! foundBody) { Stack stack = tree.getShrinkStack(); 151 if (stack.size() > 0) { 152 log.debug("no body, doing work"); 153 Entry entry = (Entry) stack.peek(); 154 StringBuffer value = new StringBuffer (); 155 while (depth >= endDepth) { 156 log.debug("writing now " + entry.string); 157 value.append(entry.string); 158 stack.pop(); 159 if (stack.size() == 0) break; 160 entry = (Entry) stack.peek(); 161 depth = entry.depth; 162 } 163 helper.setValue(value.toString()); 164 helper.doEndTag(); 165 return super.doEndTag(); 166 } else { 167 log.debug("Empty stack, nothing to do"); 168 return super.doEndTag(); 169 } 170 } else { 171 log.debug("handled by doAfterBodies"); 172 return super.doEndTag(); 173 } 174 175 } 176 177 178 static class Entry { 179 Entry(int d, String s) { 180 depth = d; string = s; 181 } 182 public int depth; 183 public String string; 184 public String toString() { 185 return "" + depth + ":" + string; 186 } 187 } 188 189 190 191 } 192 193 | Popular Tags |