1 28 29 package com.caucho.jsp.java; 30 31 import com.caucho.xml.QName; 32 33 import javax.servlet.jsp.tagext.JspFragment ; 34 import javax.servlet.jsp.tagext.TagAttributeInfo ; 35 import javax.servlet.jsp.tagext.VariableInfo ; 36 import java.io.IOException ; 37 import java.util.ArrayList ; 38 39 42 public class TagFileTag extends GenericTag { 43 private boolean _oldScriptingInvalid; 44 private JspBody _body; 45 private int _maxFragmentIndex; 46 private String _contextVarName; 47 48 51 public void endAttributes() 52 { 53 _oldScriptingInvalid = _parseState.isScriptingInvalid(); 54 _parseState.setScriptingInvalid(true); 55 } 56 57 60 public void endElement() 61 throws Exception 62 { 63 super.endElement(); 64 65 _parseState.setScriptingInvalid(_oldScriptingInvalid); 66 67 if (_children == null || _children.size() == 0) 68 return; 69 70 for (int i = 0; i < _children.size(); i++) { 71 JspNode node = _children.get(i); 72 73 if (node instanceof JspBody) { 74 if (_body != null) 75 throw error(L.l("Only one <jsp:body> is allowed as a child of a tag.")); 76 _body = (JspBody) node; 77 _children.remove(i); 78 return; 79 } 80 } 81 82 for (int i = 0; i < _children.size(); i++) { 83 JspNode node = _children.get(i); 84 85 if (! (node instanceof JspAttribute)) { 86 if (_body == null) { 87 _body = new JspBody(); 88 _body.setParent(this); 89 _body.setGenerator(_gen); 90 _body.endAttributes(); 91 } 92 93 _body.addChild(node); 94 } 95 } 96 _body.endElement(); 97 _children = null; 98 } 99 100 105 public void generateDeclaration(JspJavaWriter out) 106 throws IOException 107 { 108 super.generateDeclaration(out); 109 110 115 } 116 117 120 public boolean hasTag() 121 { 122 return super.hasTag() || _body != null && _body.hasTag(); 123 } 124 127 public String getCustomTagName() 128 { 129 return null; 130 } 131 132 135 public void generatePrologue(JspJavaWriter out) 136 throws Exception 137 { 138 super.generatePrologue(out); 139 140 if (_body != null) { 141 _body.setJspFragment(true); 142 _body.generateFragmentPrologue(out); 143 } 144 } 145 146 151 public void generate(JspJavaWriter out) 152 throws Exception 153 { 154 String className = _tagInfo.getTagClassName(); 155 Class cl = _tagClass; 156 157 String name = className; 158 159 String childContext = fillTagFileAttributes(out, name); 160 161 out.print(name + ".doTag(pageContext, " + childContext + ", out, "); 162 if (_body != null) 163 generateFragment(out, _body, "pageContext"); 164 else 165 out.print("null"); 166 167 out.println(");"); 168 169 printVarDeclaration(out, VariableInfo.AT_END); 170 } 171 172 public String fillTagFileAttributes(JspJavaWriter out, String tagName) 173 throws Exception 174 { 175 _contextVarName = "_jsp_context" + _gen.uniqueId(); 176 177 String name = _contextVarName; 178 179 out.println("com.caucho.jsp.PageContextWrapper " + name); 180 out.println(" = com.caucho.jsp.PageContextWrapper.create(pageContext);"); 181 182 TagAttributeInfo attrs[] = _tagInfo.getAttributes(); 183 184 for (int i = 0; attrs != null && i < attrs.length; i++) { 186 int p = indexOf(_attributeNames, attrs[i].getName()); 187 188 if (p < 0 && attrs[i].isRequired()) { 189 throw error(L.l("required attribute `{0}' missing from <{1}>", 190 attrs[i].getName(), 191 getTagName())); 192 } 193 } 194 195 boolean isDynamic = _tagInfo.hasDynamicAttributes(); 196 String mapAttribute = null; 197 String mapName = null; 198 199 if (isDynamic) { 200 TagInfoExt tagInfoImpl = (TagInfoExt) _tagInfo; 201 mapAttribute = tagInfoImpl.getDynamicAttributesName(); 202 } 203 204 for (int i = 0; i < _attributeNames.size(); i++) { 206 QName attrName = _attributeNames.get(i); 207 Object value = _attributeValues.get(i); 208 209 TagAttributeInfo attribute = null; 210 int j = 0; 211 for (j = 0; attrs != null && j < attrs.length; j++) { 212 if (attrs[j].getName().equals(attrName.getName())) { 213 attribute = attrs[j]; 214 break; 215 } 216 } 217 218 if (attribute == null && ! isDynamic) 219 throw error(L.l("unexpected attribute `{0}' in <{1}>", 220 attrName.getName(), getTagName())); 221 222 boolean rtexprvalue = true; 223 224 Class cl = null; 225 226 if (attribute != null) { 227 cl = _gen.loadBeanClass(attribute.getTypeName()); 228 229 rtexprvalue = attribute.canBeRequestTime(); 230 } 231 232 if (cl == null) 233 cl = String .class; 234 235 if (attribute == null) { 236 if (mapName == null) { 237 mapName = "_jsp_map_" + _gen.uniqueId(); 238 out.println("java.util.HashMap " + mapName + " = new java.util.HashMap(8);"); 239 out.println(name + ".setAttribute(\"" + mapAttribute + "\", " + mapName + ");"); 240 } 241 242 out.print(mapName + ".put(\"" + attrName.getName() + "\", "); 243 } 244 else 245 out.print(name + ".setAttribute(\"" + attrName.getName() + "\", "); 246 247 if (value instanceof JspNode) { 248 JspFragmentNode frag = (JspFragmentNode) value; 249 250 if (attribute != null && 251 attribute.getTypeName().equals(JspFragment .class.getName())) { 252 out.println(generateFragment(frag, "pageContext") + ");"); 253 } 254 else 255 out.println(frag.generateValue() + ");"); 256 } 257 else { 258 String convValue = generateParameterValue(cl, 259 (String ) value, 260 rtexprvalue, 261 attribute); 262 263 265 out.println(toObject(cl, convValue) + ");"); 266 } 267 } 268 269 return name; 270 } 271 272 private int indexOf(ArrayList <QName> names, String name) 273 { 274 for (int i = 0; i < names.size(); i++) { 275 if (names.get(i).getName().equals(name)) 276 return i; 277 } 278 279 return -1; 280 } 281 282 private String toObject(Class cl, String value) 283 { 284 if (boolean.class.equals(cl)) 285 return "new Boolean(" + value + ")"; 286 else if (byte.class.equals(cl)) 287 return "new Byte(" + value + ")"; 288 else if (short.class.equals(cl)) 289 return "new Short(" + value + ")"; 290 else if (int.class.equals(cl)) 291 return "new Integer(" + value + ")"; 292 else if (long.class.equals(cl)) 293 return "new Long(" + value + ")"; 294 else if (char.class.equals(cl)) 295 return "new Character(" + value + ")"; 296 else if (float.class.equals(cl)) 297 return "new Float(" + value + ")"; 298 else if (double.class.equals(cl)) 299 return "new Double(" + value + ")"; 300 else 301 return value; 302 } 303 } 304 | Popular Tags |