1 29 30 package com.caucho.jsp.java; 31 32 import com.caucho.jsp.JspParseException; 33 import com.caucho.jsp.cfg.TldVariable; 34 import com.caucho.vfs.WriteStream; 35 import com.caucho.xml.QName; 36 37 import java.io.IOException ; 38 import java.util.ArrayList ; 39 40 43 public class JspDoBody extends JspNode { 44 private static final QName VAR = new QName("var"); 45 private static final QName VAR_READER = new QName("varReader"); 46 private static final QName SCOPE = new QName("scope"); 47 48 private String _var; 49 private String _varReader; 50 private String _scope; 51 52 55 public void addAttribute(QName name, String value) 56 throws JspParseException 57 { 58 if (VAR.equals(name)) 59 _var = value; 60 else if (VAR_READER.equals(name)) 61 _varReader = value; 62 else if (SCOPE.equals(name)) 63 _scope = value; 64 else 65 throw error(L.l("'{0}' is an unknown attribute for jsp:doBody.", 66 name.getName())); 67 } 68 69 72 public void endAttributes() 73 throws JspParseException 74 { 75 if (! _gen.getParseState().isTag()) 76 throw error(L.l("'{0}' is only allowed in .tag files. Attribute directives are not allowed in normal JSP files.", 77 getTagName())); 78 79 if (_var != null && _varReader != null) 80 throw error(L.l("'var' and 'varReader' cannot both be set in jsp:doBody.")); 81 82 if (_scope != null && _var == null && _varReader == null) 83 throw error(L.l("jsp:doBody requires a 'var' or 'varReader' if 'scope' is set.")); 84 } 85 86 91 public void printXml(WriteStream os) 92 throws IOException 93 { 94 os.print("<jsp:do-body"); 95 os.print(" jsp:id=\"" + _gen.generateJspId() + "\""); 96 if (_var != null) 97 os.print(" var=\"" + _var + "\""); 98 os.print(">"); 99 100 os.print("</jsp:do-body>"); 101 } 102 103 106 public void generate(JspJavaWriter out) 107 throws Exception 108 { 109 String name = "_jsp_frag_" + _gen.uniqueId(); 110 111 if (_gen.hasScripting()) 112 out.println("javax.servlet.jsp.tagext.JspFragment " + name + " = getJspBody();"); 113 else 114 out.println("javax.servlet.jsp.tagext.JspFragment " + name + " = _jspBody;"); 115 116 out.println("if (" + name + " != null) {"); 117 out.pushDepth(); 118 119 JavaTagGenerator gen = (JavaTagGenerator) _gen; 120 ArrayList <TldVariable> vars = gen.getVariables(); 121 122 for (int i = 0; i < vars.size(); i++) { 123 TldVariable var = vars.get(i); 124 125 if (var.getScope().equals("AT_END")) 126 continue; 127 128 String srcName = var.getNameGiven(); 129 String dstName = srcName; 130 131 if (srcName == null) { 132 srcName = var.getAlias(); 133 dstName = var.getNameFromAttribute(); 134 dstName = "_jsp_var_from_attribute_" + i; 135 } 136 else 137 dstName = "\"" + dstName + "\""; 138 139 out.print("_jsp_parentContext.setAttribute(" + dstName + ", "); 140 out.println("pageContext.getAttribute(\"" + srcName + "\"));"); 141 } 142 143 149 150 if (_var != null) { 151 out.print(getScope() + ".setAttribute(\"" + _var + "\", "); 152 out.println("pageContext.invoke(" + name + "));"); 153 } 154 else if (_varReader != null) { 155 out.print(getScope() + ".setAttribute(\"" + _varReader + "\", "); 156 out.println("pageContext.invokeReader(" + name + "));"); 157 } 158 else { 159 out.println(name + ".invoke(null);"); 160 } 161 162 193 194 out.popDepth(); 195 out.println("}"); 196 } 197 198 private String getScope() 199 throws JspParseException 200 { 201 String context = null; 202 if (_scope == null || _scope.equals("page")) 203 return "pageContext"; 204 else if (_scope.equals("request")) { 205 return "pageContext.getRequest()"; 206 } 207 else if (_scope.equals("session")) { 208 return "pageContext.getSession()"; 209 } 210 else if (_scope.equals("application")) { 211 return "pageContext.getApplication()"; 212 } 213 else 214 throw error(L.l("Unknown scope '{0}' in <jsp:doBody>. Scope must be 'page', 'request', 'session', or 'application'.", _scope)); 215 } 216 } 217 218 | Popular Tags |