1 28 29 package com.caucho.jsp.java; 30 31 import com.caucho.jsp.JspParseException; 32 import com.caucho.jsp.TagInstance; 33 import com.caucho.xml.QName; 34 35 import javax.servlet.jsp.tagext.JspIdConsumer ; 36 import javax.servlet.jsp.tagext.VariableInfo ; 37 import java.util.ArrayList ; 38 39 42 public class CustomSimpleTag extends GenericTag 43 { 44 JspBody _body; 45 private boolean _oldScriptingInvalid; 46 47 50 public boolean isReuse() 51 { 52 return false; 53 } 54 55 58 public void endAttributes() 59 throws JspParseException 60 { 61 super.endAttributes(); 62 63 _oldScriptingInvalid = _parseState.isScriptingInvalid(); 64 } 67 68 71 public void endElement() 72 throws Exception 73 { 74 super.endElement(); 75 76 _parseState.setScriptingInvalid(_oldScriptingInvalid); 77 78 if (isEmpty()) 79 return; 80 81 for (int i = 0; i < _children.size(); i++) { 82 JspNode node = (JspNode) _children.get(i); 83 84 if (node instanceof JspBody) { 85 if (_body != null) 86 throw error(L.l("Only one <jsp:body> is allowed as a child of a tag.")); 87 88 _body = (JspBody) node; 89 _children.remove(i); 90 return; 91 } 92 } 93 94 _body = new JspBody(); 95 _body.setParent(this); 96 _body.setStartLocation(_sourcePath, _filename, _startLine); 97 _body.setGenerator(_gen); 98 _body.endAttributes(); 99 _body.setEndLocation(_filename, _startLine); 100 101 for (int i = 0; i < _children.size(); i++) { 102 JspNode node = _children.get(i); 103 104 if (! (node instanceof JspAttribute)) 105 _body.addChild(node); 106 107 _body.setEndLocation(node.getFilename(), node.getEndLine()); 108 } 109 _body.endElement(); 110 _children = null; 111 } 112 113 116 public boolean hasCustomTag() 117 { 118 if (_body != null && _body.hasCustomTag()) 119 return true; 120 else 121 return super.hasCustomTag(); 122 } 123 124 127 public void generatePrologue(JspJavaWriter out) 128 throws Exception 129 { 130 super.generatePrologue(out); 131 132 if (_body != null) { 133 _body.setJspFragment(true); 134 _body.generateFragmentPrologue(out); 135 } 136 137 if (hasCustomTag()) { 138 if (_tag.generateAdapterDeclaration()) { 140 out.println("javax.servlet.jsp.tagext.Tag " + _tag.getId() + "_adapter = null;"); 141 } 142 } 143 } 144 145 150 public void generate(JspJavaWriter out) 151 throws Exception 152 { 153 String name = _tag.getId(); 154 String className = _tagInfo.getTagClassName(); 155 Class cl = _tagClass; 156 157 if (! isReuse()) { 158 generateTagInit(out); 159 } 160 else if (! isDeclared()) { 161 out.println("if (" + name + " == null) {"); 162 out.pushDepth(); 163 generateTagInit(out); 164 out.popDepth(); 165 out.println("}"); 166 out.println(); 167 } 168 169 fillAttributes(out, name); 170 171 if (_body != null) { 172 out.print(name + ".setJspBody("); 173 generateFragment(out, _body, "pageContext"); 174 out.println(");"); 175 } 176 177 out.println(name + ".doTag();"); 178 179 printVarDeclaration(out, VariableInfo.AT_END); 180 } 181 182 187 private void generateTagInit(JspJavaWriter out) 188 throws Exception 189 { 190 TagInstance parent = _tag.getParent(); 191 192 String var = _tag.getId(); 193 String className = _tag.getTagClass().getName(); 194 195 out.print(var + " = new "); 196 out.printClass(_tag.getTagClass()); 197 out.println("();"); 198 199 if (JspIdConsumer .class.isAssignableFrom(_tag.getTagClass())) { 200 String shortName = className; 201 int p = shortName.lastIndexOf('.'); 202 if (p >= 0) 203 shortName = shortName.substring(p + 1); 204 205 out.print(var + ".setJspId(\"" + shortName + "-" + _gen.generateJspId() + "\");"); 206 } 207 208 out.println(var + ".setJspContext(pageContext);"); 209 JspNode parentNode = getParent().getParentTagNode(); 210 if (parentNode != null) { 211 out.println(var + ".setParent(" + parentNode.getCustomTagName() + ");"); 212 } 213 214 if (_tag.getAnalyzedTag() != null 215 && _tag.getAnalyzedTag().getHasInjection()) { 216 out.println("_jsp_inject_" + _tag.getId() + ".configure(" + var + ");"); 217 } 218 219 if (hasCustomTag()) { 220 out.println(var + "_adapter = new javax.servlet.jsp.tagext.TagAdapter(" + var + ");"); 221 } 222 223 ArrayList <QName> names = _tag.getAttributeNames(); 224 for (int i = 0; i < names.size(); i++) { 225 QName name = names.get(i); 226 227 String value = _tag.getAttribute(name); 228 if (value == null) 229 continue; 230 231 generateSetAttribute(out, var, name, value, false, false, 232 _tag.getAttributeInfo(name.getLocalName())); 233 } 234 } 235 } 236 | Popular Tags |