1 28 29 package com.caucho.jsp.java; 30 31 import com.caucho.jsp.JspParseException; 32 import com.caucho.jsp.cfg.TldVariable; 33 import com.caucho.vfs.WriteStream; 34 import com.caucho.xml.QName; 35 36 import java.io.IOException ; 37 import java.util.ArrayList ; 38 39 42 public class JspInvoke extends JspNode { 43 private static final QName FRAGMENT = new QName("fragment"); 44 private static final QName VAR = new QName("var"); 45 private static final QName SCOPE = new QName("scope"); 46 private static final QName VAR_READER = new QName("varReader"); 47 48 private String _name; 49 private String _var; 50 private String _varReader; 51 private String _scope; 52 53 56 public void addAttribute(QName name, String value) 57 throws JspParseException 58 { 59 if (FRAGMENT.equals(name)) 60 _name = value; 61 else if (VAR.equals(name)) 62 _var = value; 63 else if (VAR_READER.equals(name)) 64 _varReader = value; 65 else if (SCOPE.equals(name)) 66 _scope = value; 67 else 68 throw error(L.l("`{0}' is an unknown attribute for jsp:invoke.", 69 name.getName())); 70 } 71 72 75 public void endAttributes() 76 throws JspParseException 77 { 78 if (! _gen.getParseState().isTag()) 79 throw error(L.l("`{0}' is only allowed in .tag files. Attribute directives are not allowed in normal JSP files.", 80 getTagName())); 81 } 82 83 86 public void endElement() 87 throws JspParseException 88 { 89 if (_name == null) 90 throw error(L.l("'fragment' is a required attribute of <jsp:invoke>.")); 91 92 if (_scope != null && _var == null && _varReader == null) 93 throw error(L.l("'scope' requires a 'var' or a 'varReader' attribute for <jsp:invoke>.")); 94 95 if (_var != null && _varReader != null) 96 throw error(L.l("jsp:invoke may not have both 'var' and 'varReader'")); 97 } 98 99 104 public void printXml(WriteStream os) 105 throws IOException 106 { 107 os.print("<jsp:invoke"); 108 109 if (_name != null) 110 os.print(" name=\"" + _name + "\""); 111 112 if (_var != null) 113 os.print(" var=\"" + _var + "\""); 114 115 if (_varReader != null) 116 os.print(" varReader=\"" + _varReader + "\""); 117 118 if (_scope != null) 119 os.print(" scope=\"" + _scope + "\""); 120 121 os.print("/>"); 122 } 123 124 127 public void generate(JspJavaWriter out) 128 throws Exception 129 { 130 String name = "_jsp_frag_" + _gen.uniqueId(); 131 132 out.println("javax.servlet.jsp.tagext.JspFragment " + name + " = (javax.servlet.jsp.tagext.JspFragment) pageContext.getAttribute(\"" + _name + "\");"); 133 134 out.println("if (" + name + " != null) {"); 135 out.pushDepth(); 136 JavaTagGenerator gen = (JavaTagGenerator) _gen; 137 ArrayList <TldVariable> vars = gen.getVariables(); 138 139 for (int i = 0; i < vars.size(); i++) { 140 TldVariable var = vars.get(i); 141 142 if (var.getScope().equals("AT_END")) 143 continue; 144 145 String srcName = var.getNameGiven(); 146 String dstName = srcName; 147 148 if (srcName == null) { 149 srcName = var.getAlias(); 150 dstName = var.getNameFromAttribute(); 151 dstName = "_jsp_var_from_attribute_" + i; 152 } 153 else 154 dstName = "\"" + dstName + "\""; 155 156 out.print("_jsp_parentContext.setAttribute(" + dstName + ", "); 157 out.println("pageContext.getAttribute(\"" + srcName + "\"));"); 158 } 159 160 166 167 String context = null; 168 if (_scope == null || _scope.equals("page")) 169 context = "pageContext"; 170 else if (_scope.equals("request")) 171 context = "pageContext.getRequest()"; 172 else if (_scope.equals("session")) 173 context = "pageContext.getSessionScope()"; 174 else if (_scope.equals("application")) 175 context = "pageContext.getApplication()"; 176 else 177 throw error(L.l("Unknown scope `{0}' in <jsp:invoke>. Scope must be `page', `request', `session', or `application'.", _scope)); 178 179 180 if (_var != null) { 181 out.print(context + ".setAttribute(\"" + _var + "\", "); 182 out.println("pageContext.invoke(" + name + "));"); 183 } 184 else if (_varReader != null) { 185 out.print(context + ".setAttribute(\"" + _varReader + "\", "); 186 out.println("pageContext.invokeReader(" + name + "));"); 187 } 188 else { 189 out.println(name + ".invoke(null);"); 190 } 191 192 out.popDepth(); 193 out.println("}"); 194 } 195 } 196 | Popular Tags |