1 28 29 package com.caucho.jsp; 30 31 import com.caucho.jsp.java.JspTagFileSupport; 32 import com.caucho.log.Log; 33 import com.caucho.util.L10N; 34 import com.caucho.xml.QName; 35 36 import javax.servlet.jsp.tagext.SimpleTag ; 37 import javax.servlet.jsp.tagext.TagAttributeInfo ; 38 import javax.servlet.jsp.tagext.TagInfo ; 39 import javax.servlet.jsp.tagext.VariableInfo ; 40 import java.util.ArrayList ; 41 import java.util.Iterator ; 42 import java.util.logging.Logger ; 43 44 47 public class TagInstance { 48 static final L10N L = new L10N(TagInstance.class); 49 static final Logger log = Log.open(TagInstance.class); 50 51 private static final Object VARIES = new Varies(); 53 54 public static final String TOP_TAG = null; 55 public static final String FRAGMENT_WITH_TAG_PARENT = "top_tag_fragment"; 56 public static final String FRAGMENT_WITH_SIMPLE_PARENT = "top_simple_fragment"; 57 58 private ParseTagManager _manager; 59 60 private TagInstance _top; 61 private TagInfo _tagInfo; 62 private int _maxId; 63 64 private TagInstance _parent; 65 private ArrayList <TagInstance> _children = new ArrayList <TagInstance>(); 66 private ArrayList <TagInstance> _tags = new ArrayList <TagInstance>(); 67 68 private String _tagId = null; 69 private QName _qname; 70 private Class _cl; 71 private VariableInfo []_varInfo; 72 private boolean _needsAdapter; 73 private boolean _hasAdapterDeclaration; 74 75 private AnalyzedTag _analyzedTag; 76 77 private ArrayList <QName> _attributeNames = new ArrayList <QName>(); 78 private ArrayList <Object > _attributeValues = new ArrayList <Object >(); 79 80 private boolean _hasBodyContent; 81 82 public TagInstance(ParseTagManager manager) 83 { 84 _manager = manager; 85 86 _top = this; 87 _tagId = null; 88 } 89 90 public TagInstance(ParseTagManager manager, String id) 91 { 92 _manager = manager; 93 _top = this; 94 _tagId = id; 95 } 96 97 TagInstance(TagInstance parent, TagInfo tagInfo, QName qname, Class cl) 98 { 99 _qname = qname; 100 _parent = parent; 101 _manager = parent._manager; 102 _tagInfo = tagInfo; 103 _cl = cl; 104 105 _top = parent._top; 106 parent._children.add(this); 107 _top._tags.add(this); 108 109 if (tagInfo != null) { 110 String className = tagInfo.getTagClassName(); 111 int p = className.lastIndexOf('.'); 112 if (p >= 0) 113 className = className.substring(p + 1); 114 _tagId = "_jsp_" + className + "_" + _top._maxId++; 115 116 } 117 118 _analyzedTag = _manager.analyzeTag(cl); 119 } 120 121 124 public QName getQName() 125 { 126 return _qname; 127 } 128 129 132 public String getName() 133 { 134 return _qname.getName(); 135 } 136 137 public String getId() 138 { 139 return _tagId; 140 } 141 142 public void setId(String id) 143 { 144 _tagId = id; 145 } 146 147 public boolean generateAdapterDeclaration() 148 { 149 if (_hasAdapterDeclaration) { 150 return false; 151 } 152 else { 153 _hasAdapterDeclaration = true; 154 155 return true; 156 } 157 } 158 159 public TagInstance getParent() 160 { 161 return _parent; 162 } 163 164 167 public boolean isTop() 168 { 169 return _tagId == null || _tagId.startsWith("top_"); 170 } 171 172 175 public Class getTagClass() 176 { 177 return _cl; 178 } 179 180 183 public boolean isSimpleTag() 184 { 185 return _cl != null && SimpleTag .class.isAssignableFrom(_cl); 186 } 187 188 191 public boolean isTagFileTag() 192 { 193 return _cl != null && JspTagFileSupport.class.isAssignableFrom(_cl); 194 } 195 196 public TagInfo getTagInfo() 197 { 198 return _tagInfo; 199 } 200 201 204 public AnalyzedTag getAnalyzedTag() 205 { 206 return _analyzedTag; 207 } 208 209 void setVarInfo(VariableInfo []varInfo) 210 { 211 _varInfo = varInfo; 212 } 213 214 public VariableInfo []getVarInfo() 215 { 216 return _varInfo; 217 } 218 219 public int size() 220 { 221 return _top._maxId; 222 } 223 224 227 public boolean hasChildren() 228 { 229 return _children != null && _children.size() > 0; 230 } 231 232 235 public boolean getNeedsAdapter() 236 { 237 return _needsAdapter || isSimpleTag() && hasChildren(); 238 } 239 240 243 public void setNeedsAdapter(boolean needsAdapter) 244 { 245 _needsAdapter = needsAdapter; 246 } 247 248 251 public Iterator <TagInstance> iterator() 252 { 253 return _children.iterator(); 254 } 255 256 259 public TagInstance get(int i) 260 { 261 return _top._tags.get(i); 262 } 263 264 267 public ArrayList <QName> getAttributeNames() 268 { 269 return _attributeNames; 270 } 271 272 275 public void setBodyContent(boolean hasBodyContent) 276 { 277 _hasBodyContent = hasBodyContent; 278 } 279 280 283 public boolean getBodyContent() 284 { 285 return _hasBodyContent; 286 } 287 288 297 public TagInstance addTag(QName tagName, TagInfo tagInfo, 298 Class cl, 299 ArrayList <QName> names, 300 ArrayList <Object > values, 301 boolean hasBodyContent) 302 { 303 TagInstance child = null; if (child == null) 305 child = new TagInstance(this, tagInfo, tagName, cl); 306 307 child.setBodyContent(hasBodyContent); 308 309 for (int i = 0; i < names.size(); i++) { 310 QName name = names.get(i); 311 Object value = values.get(i); 312 313 if (value instanceof String ) { 314 String strValue = (String ) value; 315 316 if (strValue.startsWith("<%=") || strValue.startsWith("%=")) 318 value = null; 319 else if (strValue.indexOf("${") >= 0) 320 value = null; 321 322 child.addAttribute(name, value); 323 } else { 324 child.addAttribute(name, null); 325 } 326 } 327 328 return child; 329 } 330 331 334 public TagInstance addNewTag(QName tagName, TagInfo tagInfo, 335 Class cl, 336 ArrayList <QName> names, 337 ArrayList <String > values, 338 boolean hasBodyContent) 339 { 340 TagInstance child = null; 341 if (child == null) 342 child = new TagInstance(this, tagInfo, tagName, cl); 343 344 child.setBodyContent(hasBodyContent); 345 346 for (int i = 0; i < names.size(); i++) { 347 QName name = names.get(i); 348 String value = values.get(i); 349 350 if (value.startsWith("<%=") || value.startsWith("%=")) 351 value = null; 352 353 child.addAttribute(name, value); 354 } 355 356 return child; 357 } 358 359 362 public void addAttribute(QName name, Object value) 363 { 364 for (int i = 0; i < _attributeNames.size(); i++) { 365 QName attrName = _attributeNames.get(i); 366 367 if (attrName.equals(name)) { 368 Object oldValue = _attributeValues.get(i); 369 if (value == null || oldValue == null || ! value.equals(oldValue)) 370 _attributeValues.set(i, null); 371 372 return; 373 } 374 } 375 376 if (name == null) 377 throw new NullPointerException (); 378 379 _attributeNames.add(name); 380 _attributeValues.add(value); 381 } 382 383 public String getAttribute(QName name) 384 { 385 for (int i = 0; i < _attributeNames.size(); i++) { 386 if (name.equals(_attributeNames.get(i))) 387 return (String ) _attributeValues.get(i); 388 } 389 390 return null; 391 } 392 393 boolean canBeRequestTime(String name) 394 { 395 TagAttributeInfo attrs[] = _tagInfo.getAttributes(); 396 397 if (attrs == null) 398 return true; 399 400 for (int i = 0; i < attrs.length; i++) { 401 if (name.equals(attrs[i].getName())) 402 return attrs[i].canBeRequestTime(); 403 } 404 405 return false; 406 } 407 408 public TagAttributeInfo getAttributeInfo(String name) 409 { 410 TagAttributeInfo attrs[] = _tagInfo.getAttributes(); 411 412 if (attrs == null) 413 return null; 414 415 for (int i = 0; i < attrs.length; i++) { 416 if (name.equals(attrs[i].getName())) 417 return attrs[i]; 418 } 419 420 return null; 421 } 422 423 426 public TagInstance findTag(QName tagName, 427 ArrayList <QName> names, 428 boolean hasBodyContent) 429 { 430 for (int i = 0; i < _children.size(); i++) { 431 TagInstance child = _children.get(i); 432 433 if (child.match(tagName, names, hasBodyContent)) 434 return child; 435 } 436 437 return null; 438 } 439 440 443 boolean match(QName tagName, ArrayList <QName> names, boolean hasBodyContent) 444 { 445 if (! _qname.equals(tagName)) 446 return false; 447 448 if (_attributeNames.size() != names.size()) 449 return false; 450 451 if (_hasBodyContent != hasBodyContent) 452 return false; 453 454 for (int i = 0; i < _attributeNames.size(); i++) { 455 QName attrName = _attributeNames.get(i); 456 457 if (names.indexOf(attrName) < 0) 458 return false; 459 } 460 461 for (int i = 0; i < names.size(); i++) { 462 QName name = names.get(i); 463 464 if (_attributeNames.indexOf(name) < 0) 465 return false; 466 } 467 468 return true; 469 } 470 471 static class Varies { 472 public String toString() 473 { 474 return "Value-Varies[]"; 475 } 476 } 477 } 478 | Popular Tags |