1 29 30 package com.caucho.jsp.java; 31 32 import com.caucho.jsp.JspParseException; 33 import com.caucho.jsp.TagInstance; 34 import com.caucho.util.CharBuffer; 35 36 39 abstract public class JspFragmentNode extends JspContainerNode 40 implements JspSegmentNode 41 { 42 private int _fragmentCode; 43 private String _fragmentName; 44 45 private boolean _isValueFragment; 46 private boolean _isJspFragment; 47 48 public JspFragmentNode() 49 { 50 } 51 52 55 public void endAttributes() 56 throws JspParseException 57 { 58 _fragmentCode = _gen.uniqueId(); 59 60 _fragmentName = "_jsp_fragment_" + _fragmentCode; 61 } 62 63 66 public String getFragmentName() 67 { 68 return _fragmentName; 69 } 70 71 74 public String getCustomTagName() 75 { 76 return "_jsp_parent_tag"; 77 } 78 79 82 public JspNode addText(String text) 83 throws JspParseException 84 { 85 JspNode node = new StaticText(_gen, text, this); 86 87 addChild(node); 88 89 return node; 90 } 91 92 95 public boolean isTrim() 96 { 97 return false; 98 } 99 100 103 public boolean isStatic() 104 { 105 if (_children == null) 106 return true; 107 108 for (int i = 0; i < _children.size(); i++) { 109 if (! _children.get(i).isStatic()) 110 return false; 111 } 112 113 return true; 114 } 115 116 119 public boolean isValueFragment() 120 { 121 return _isValueFragment; 122 } 123 124 127 public void setJspFragment(boolean isFragment) 128 { 129 _isJspFragment = isFragment; 130 } 131 132 135 public boolean isJspFragment() 136 { 137 return _isJspFragment; 138 } 139 140 143 public void generateFragmentPrologue(JspJavaWriter out) 144 throws Exception 145 { 146 if (_isValueFragment) 147 return; 148 149 _isJspFragment = true; 150 151 if (isStatic()) 152 out.println("com.caucho.jsp.StaticJspFragmentSupport " + _fragmentName + " = null;"); 153 else 154 out.println("_CauchoFragment " + _fragmentName + " = null;"); 155 } 156 157 160 public void generate(JspJavaWriter out) 161 throws Exception 162 { 163 if (hasScripting() && isJspFragment()) 164 throw error(L.l("Fragments may not contain scripting elements")); 165 166 generateChildren(out); 167 } 168 169 172 protected String generateValue() 173 throws Exception 174 { 175 if (isStatic()) 176 return '"' + escapeJavaString(getStaticText()) + '"'; 177 178 _isValueFragment = true; 179 180 if (hasScripting() && isJspFragment()) 181 throw error(L.l("Fragments may not contain scripting elements")); 182 183 _gen.addFragment(this); 184 185 TagInstance parent = getParent().getTag(); 186 187 CharBuffer cb = new CharBuffer(); 188 189 cb.append("_CauchoFragment." + _fragmentName + "(pageContext, "); 190 191 for (; 192 parent != null && parent.isTagFileTag(); 193 parent = parent.getParent()) { 194 } 195 196 if (parent == null || parent.getId() == TagInstance.TOP_TAG) 197 cb.append("null"); 198 else if (parent.getId().startsWith("top_")) 199 cb.append("_jsp_parent_tag"); 200 else if (! hasCustomTag()) 201 cb.append(parent.getId()); 202 else if (parent.isSimpleTag()) 203 cb.append(parent.getId() + "_adapter"); 204 else 205 cb.append(parent.getId()); 206 207 if (_gen instanceof JavaTagGenerator) 208 cb.append(", _jspBody"); 209 else 210 cb.append(", null"); 211 212 cb.append(")"); 213 214 return cb.close(); 215 } 216 217 220 void generateValueMethod(JspJavaWriter out) 221 throws Exception 222 { 223 if (hasScripting() && isJspFragment()) 224 throw error(L.l("Fragments may not contain scripting elements")); 225 226 out.println(); 227 out.println("static String " + _fragmentName + "("); 228 out.println(" com.caucho.jsp.PageContextImpl pageContext,"); 229 out.println(" javax.servlet.jsp.tagext.JspTag _jsp_parent_tag,"); 230 out.println(" javax.servlet.jsp.tagext.JspFragment _jspBody)"); 231 out.println(" throws Throwable"); 232 out.println("{"); 233 out.pushDepth(); 234 235 out.println("JspWriter out = pageContext.pushBody();"); 236 out.println("javax.el.ELContext _jsp_env = pageContext.getELContext();"); 237 238 out.println("try {"); 239 out.pushDepth(); 240 241 generatePrologue(out); 242 243 generate(out); 244 245 out.print("return ((com.caucho.jsp.BodyContentImpl) out)"); 246 252 out.println(".getString();"); 254 255 out.popDepth(); 256 out.println("} finally {"); 257 out.pushDepth(); 258 out.println("pageContext.popAndReleaseBody();"); 259 out.popDepth(); 260 out.println("}"); 261 262 out.popDepth(); 263 out.println("}"); 264 } 265 } 266 | Popular Tags |