1 28 29 package com.caucho.jsp.java; 30 31 import com.caucho.jsp.JspParseException; 32 import com.caucho.util.CharBuffer; 33 import com.caucho.util.L10N; 34 import com.caucho.util.LineCompileException; 35 import com.caucho.vfs.WriteStream; 36 import com.caucho.xml.QName; 37 import com.caucho.xml.XmlChar; 38 39 import java.io.IOException ; 40 import java.util.ArrayList ; 41 42 45 public abstract class JspContainerNode extends JspNode { 46 static final L10N L = new L10N(JspContainerNode.class); 47 48 protected ArrayList <QName> _attributeNames = new ArrayList <QName>(); 49 protected ArrayList <Object > _attributeValues = new ArrayList <Object >(); 50 51 protected ArrayList <JspNode> _children = new ArrayList <JspNode>(); 52 53 protected boolean _hasJspAttribute; 54 55 61 public void addAttribute(QName name, String value) 62 throws JspParseException 63 { 64 if (name == null) 65 throw new NullPointerException (); 66 67 if (_attributeNames.indexOf(name) >= 0) 68 throw error(L.l("'{0}' is a duplicate attribute name. Attributes must occur only once in a tag.", name.getName())); 69 70 _attributeNames.add(name); 71 _attributeValues.add(value); 72 } 73 74 80 public void addAttribute(QName name, JspAttribute value) 81 throws JspParseException 82 { 83 if (name == null) 84 throw new NullPointerException (); 85 86 if (_attributeNames.indexOf(name) >= 0) 87 throw error(L.l("'{0}' is a duplicate attribute name. Attributes must occur only once in a tag.", name.getName())); 88 89 _attributeNames.add(name); 90 _attributeValues.add(value); 91 92 _gen.addFragment(value); 93 } 94 95 98 public Object getAttribute(String name) 99 throws JspParseException 100 { 101 for (int i = 0; i < _attributeNames.size(); i++) { 102 if (name.equals(_attributeNames.get(i).getName())) 103 return _attributeValues.get(i); 104 } 105 106 return null; 107 } 108 109 112 public void addChild(JspNode node) 113 throws JspParseException 114 { 115 node.setParent(this); 116 117 if (node instanceof JspAttribute) { 118 JspAttribute attr = (JspAttribute) node; 119 120 QName name = attr.getName(); 121 122 addAttribute(name, attr); 123 124 _hasJspAttribute = true; 125 126 int i = 0; 127 while (_children != null && i < _children.size()) { 128 JspNode child = _children.get(i); 129 130 if (child instanceof StaticText) { 131 String text = ((StaticText) child).getText(); 132 133 if (isWhitespace(text)) 134 _children.remove(i); 135 else 136 throw child.error(L.l("tags using jsp:attribute must put body content in a jsp:body tag")); 137 } 138 else if (child instanceof JspBody) 139 i++; 140 else if (child instanceof JspAttribute) 141 i++; 142 else { 143 throw child.error(L.l("tags using jsp:attribute must put body content in a jsp:body tag")); 144 } 145 } 146 } 147 else if (_hasJspAttribute && ! (node instanceof JspBody)) { 148 throw node.error(L.l("tags using jsp:attribute must put body content in a jsp:body tag")); 149 } 150 else if (node instanceof StaticText) { 151 StaticText text = (StaticText) node; 152 String data = text.getText(); 153 154 boolean isXml = _gen.isXml(); 155 for (JspNode ptr = this; ptr != null; ptr = ptr.getParent()) { 156 if (ptr instanceof JspRoot) 157 isXml = true; 158 } 159 160 if (_children == null) 161 _children = new ArrayList <JspNode>(); 162 163 for (int i = 0; 164 i < data.length(); 165 i++) { 166 if (! XmlChar.isWhitespace(data.charAt(i))) { 167 _children.add(node); 168 return; 169 } 170 } 171 172 if (! isXml) 173 _children.add(node); 174 } 175 else { 176 if (_children == null) 177 _children = new ArrayList <JspNode>(); 178 179 _children.add(node); 180 } 181 } 182 183 186 public void addChildEnd(JspNode node) 187 throws JspParseException 188 { 189 } 190 191 194 public boolean isEmpty() 195 { 196 if (_children == null || _children.size() == 0) 197 return true; 198 199 for (int i = 0; i < _children.size(); i++) { 200 JspNode child = _children.get(i); 201 202 if (child instanceof JspBody) { 203 JspBody body = (JspBody) child; 204 205 return body.isEmpty(); 206 } 207 else if (child instanceof StaticText) { 208 StaticText text = (StaticText) child; 209 210 if (! text.isWhitespace()) 211 return false; 212 } 213 else 214 return false; 215 } 216 217 return false; 218 } 219 220 223 public void getStaticText(CharBuffer cb) 224 { 225 if (_children == null) 226 return; 227 228 for (int i = 0; i < _children.size(); i++) 229 _children.get(i).getStaticText(cb); 230 } 231 232 235 public boolean hasCustomTag() 236 { 237 for (int i = 0; _children != null && i < _children.size(); i++) { 238 JspNode child = _children.get(i); 239 240 if (child instanceof CustomTag) 241 return true; 242 243 if (child.hasCustomTag()) 244 return true; 245 } 246 247 for (int i = 0; 248 _attributeValues != null && i < _attributeValues.size(); 249 i++) { 250 Object value = _attributeValues.get(i); 251 252 if (value instanceof CustomTag) 253 return true; 254 else if (value instanceof JspNode && 255 ((JspNode) value).hasCustomTag()) 256 return true; 257 } 258 259 return false; 260 } 261 262 265 public boolean hasTag() 266 { 267 for (int i = 0; _children != null && i < _children.size(); i++) { 268 JspNode child = _children.get(i); 269 270 if (child instanceof CustomTag || 271 child instanceof CustomSimpleTag) 272 return true; 273 274 if (child.hasTag()) 275 return true; 276 } 277 278 for (int i = 0; 279 _attributeValues != null && i < _attributeValues.size(); 280 i++) { 281 Object value = _attributeValues.get(i); 282 283 if (value instanceof CustomTag || value instanceof CustomSimpleTag) 284 return true; 285 286 else if (value instanceof JspNode && 287 ((JspNode) value).hasTag()) 288 return true; 289 } 290 291 return false; 292 } 293 294 297 public JspNode addText(String text) 298 throws JspParseException 299 { 300 if (! _hasJspAttribute) { 301 JspNode node = new StaticText(_gen, text, this); 302 303 addChild(node); 304 305 return node; 306 } 307 else if (! isWhitespace(text)) { 308 throw error(L.l("tags using jsp:attribute must put body content in a jsp:body tag")); 309 } 310 311 return null; 312 } 313 314 protected boolean isWhitespace(String text) 315 { 316 for (int i = 0; i < text.length(); i++) { 317 if (! Character.isWhitespace(text.charAt(i))) 318 return false; 319 } 320 321 return true; 322 } 323 324 327 public ArrayList <JspNode> getChildren() 328 { 329 return _children; 330 } 331 332 335 public boolean hasChildren() 336 { 337 return ! isEmpty(); 338 } 339 340 343 public boolean hasScripting() 344 { 345 for (int i = 0; _children != null && i < _children.size(); i++) { 346 if (_children.get(i).hasScripting()) 347 return true; 348 } 349 350 return false; 351 } 352 353 356 public boolean isChildrenStatic() 357 { 358 if (_children == null) 359 return true; 360 361 for (int i = 0; i < _children.size(); i++) { 362 if (! _children.get(i).isStatic()) 363 return false; 364 } 365 366 return true; 367 } 368 369 374 public void printXmlChildren(WriteStream os) 375 throws IOException 376 { 377 if (_children == null) 378 return; 379 380 for (int i = 0; i < _children.size(); i++) { 381 JspNode child = _children.get(i); 382 383 child.printXml(os); 384 } 385 } 386 387 390 public void generatePrologueChildren(JspJavaWriter out) 391 throws Exception 392 { 393 if (_children == null) 394 return; 395 396 for (int i = 0; i < _children.size(); i++) { 397 JspNode child = _children.get(i); 398 399 child.generatePrologue(out); 400 } 401 } 402 403 406 public void generateDeclarationChildren(JspJavaWriter out) 407 throws IOException 408 { 409 if (_children == null) 410 return; 411 412 for (int i = 0; i < _children.size(); i++) { 413 JspNode child = _children.get(i); 414 415 child.generateDeclaration(out); 416 } 417 } 418 419 424 public void generateChildren(JspJavaWriter out) 425 throws Exception 426 { 427 if (_children == null) 428 return; 429 430 for (int i = 0; i < _children.size(); i++) { 431 JspNode child = _children.get(i); 432 433 child.generateStartLocation(out); 434 try { 435 child.generate(out); 436 } catch (Exception e) { 437 if (e instanceof LineCompileException) 438 throw e; 439 else 440 throw child.error(e); 441 } 442 child.generateEndLocation(out); 443 } 444 } 445 446 451 public void generateChildrenEmpty() 452 throws Exception 453 { 454 if (_children == null) 455 return; 456 457 for (int i = 0; i < _children.size(); i++) { 458 JspNode child = _children.get(i); 459 460 child.generateEmpty(); 461 } 462 } 463 464 469 public void generateStatic(JspJavaWriter out) 470 throws Exception 471 { 472 if (_children == null) 473 return; 474 475 for (int i = 0; i < _children.size(); i++) { 476 JspNode child = _children.get(i); 477 478 out.setLocation(child.getFilename(), child.getStartLine()); 479 child.generateStatic(out); 480 } 482 } 483 } 484 485 | Popular Tags |