1 28 29 package com.caucho.jsp.java; 30 31 import com.caucho.jsp.JspBuilder; 32 import com.caucho.jsp.JspGenerator; 33 import com.caucho.jsp.JspLineParseException; 34 import com.caucho.jsp.JspParseException; 35 import com.caucho.log.Log; 36 import com.caucho.util.CompileException; 37 import com.caucho.util.L10N; 38 import com.caucho.util.LineCompileException; 39 import com.caucho.vfs.PersistentDependency; 40 import com.caucho.xml.QName; 41 42 import javax.servlet.jsp.tagext.SimpleTag ; 43 import javax.servlet.jsp.tagext.Tag ; 44 import javax.servlet.jsp.tagext.TagInfo ; 45 import java.util.ArrayList ; 46 import java.util.HashMap ; 47 import java.util.logging.Level ; 48 import java.util.logging.Logger ; 49 50 53 public class JavaJspBuilder extends JspBuilder { 54 public static final String JSTL_CORE_URI = "http://java.sun.com/jsp/jstl/core"; 55 public static final String JSTL_EL_CORE_URI = "http://java.sun.com/jstl/core"; 56 public static final String JSTL_RT_CORE_URI = "http://java.sun.com/jstl/core_rt"; 57 58 public static final String JSTL_FMT_URI = "http://java.sun.com/jsp/jstl/fmt"; 59 60 public static final String JSTL_EL_FMT_URI = "http://java.sun.com/jstl/fmt"; 61 public static final String JSTL_RT_FMT_URI = "http://java.sun.com/jstl/fmt_rt"; 62 63 public static final String JSTL_XML_URI = "http://java.sun.com/jsp/jstl/xml"; 64 65 public static final String JSTL_EL_XML_URI = "http://java.sun.com/jstl/xml"; 66 public static final String JSTL_RT_XML_URI = "http://java.sun.com/jstl/xml_rt"; 67 68 public static final String JSTL_SQL_URI = "http://java.sun.com/jsp/jstl/sql"; 69 70 public static final String JSTL_EL_SQL_URI = "http://java.sun.com/jstl/sql"; 71 public static final String JSTL_RT_SQL_URI = "http://java.sun.com/jstl/sql_rt"; 72 73 private static L10N L = new L10N(JavaJspBuilder.class); 74 private static Logger log = Logger.getLogger(JavaJspBuilder.class.getName()); 75 76 static final int IGNORE = 0; 77 static final int DIRECTIVE_PAGE = IGNORE + 1; 78 static final int TEXT = DIRECTIVE_PAGE + 1; 79 static final int EXPR = TEXT + 1; 80 static final int SCRIPTLET = EXPR + 1; 81 static final int DECLARATION = SCRIPTLET + 1; 82 static final int FUNCTION = DECLARATION + 1; 83 static final int SET = FUNCTION + 1; 84 static final int INCLUDE = SET + 1; 85 static final int FORWARD = INCLUDE + 1; 86 static final int REQUEST = INCLUDE + 1; 87 static final int USE_BEAN = REQUEST + 1; 88 static final int GET_PROPERTY = USE_BEAN + 1; 89 static final int SET_PROPERTY = GET_PROPERTY + 1; 90 static final int TAG = GET_PROPERTY + 1; 91 static final int PLUGIN = TAG + 1; 92 static final int ROOT = PLUGIN + 1; 93 static final int JSP_TEXT = ROOT + 1; 94 static final int JSP_ATTRIBUTE = JSP_TEXT + 1; 95 static final int JSP_BODY = JSP_ATTRIBUTE + 1; 96 97 static final int IF = JSP_BODY + 1; 98 static final int FOREACH = IF + 1; 99 static final int EXPR_EL = FOREACH + 1; 100 101 static HashMap <QName,Class > _tagMap; 102 static HashMap <QName,Class > _fastTagMap; 103 104 private JavaJspGenerator _gen; 105 private JspNode _rootNode; 106 private JspNode _currentNode; 107 private JspNode _openNode; 108 109 private boolean _isPrototype; 110 111 114 public JavaJspBuilder() 115 { 116 } 117 118 121 public JspGenerator getGenerator() 122 { 123 return _gen; 124 } 125 126 129 public JspNode getRootNode() 130 { 131 return _rootNode; 132 } 133 134 137 public void setPrototype(boolean prototype) 138 { 139 _isPrototype = prototype; 140 } 141 142 145 public boolean isPrototype() 146 { 147 return _isPrototype; 148 } 149 150 153 public void startDocument() 154 throws JspParseException 155 { 156 try { 157 if (_rootNode != null) 159 return; 160 161 if (_parseState.isTag()) 162 _gen = new JavaTagGenerator(_tagManager); 163 else 164 _gen = new JavaJspGenerator(_tagManager); 165 _gen.setParseState(getParseState()); 166 _gen.setJspCompiler(getJspCompiler()); 167 _gen.setJspParser(getJspParser()); 168 _gen.setRequireSource(getRequireSource()); 169 170 _rootNode = new JspTop(); 171 _rootNode.setParseState(getParseState()); 172 _rootNode.setGenerator(_gen); 173 _rootNode.setStartLocation(_sourcePath, _filename, _line); 174 _gen.setRootNode(_rootNode); 175 _gen.init(); 176 177 _currentNode = _rootNode; 178 } catch (Exception e) { 179 throw JspParseException.create(e); 180 } 181 } 182 183 186 public void endDocument() 187 throws JspParseException 188 { 189 } 190 191 196 public void startElement(QName qname) 197 throws JspParseException 198 { 199 Class cl; 200 201 if (isFastJstl()) 202 cl = _fastTagMap.get(qname); 203 else 204 cl = _tagMap.get(qname); 205 206 if (cl != null) { 207 try { 208 JspNode node = (JspNode) cl.newInstance(); 209 210 node.setGenerator(_gen); 211 node.setParseState(_parseState); 212 node.setQName(qname); 213 node.setParent(_currentNode); 214 node.setStartLocation(_sourcePath, _filename, _line); 215 216 _openNode = node; 217 218 return; 219 } catch (Exception e) { 220 throw new JspParseException(e); 221 } 222 } 223 224 if (isPrototype()) { 225 JspXmlElement elt = new JspXmlElement(); 226 elt.setGenerator(_gen); 227 elt.setParseState(_parseState); 228 elt.setQName(qname); 229 elt.setParent(_currentNode); 230 elt.setStartLocation(_sourcePath, _filename, _line); 231 232 _openNode = elt; 233 234 return; 235 } 236 237 if (JspNode.JSP_NS.equals(qname.getNamespace())) 238 throw new JspParseException(L.l("unknown JSP <{0}>", qname.getName())); 239 240 TagInfo tagInfo; 241 242 try { 243 tagInfo = _gen.getTag(qname); 244 } catch (JspParseException e) { 245 throw error(e); 246 } 247 248 if (tagInfo == null) { 249 JspXmlElement elt = new JspXmlElement(); 250 elt.setGenerator(_gen); 251 elt.setParseState(_parseState); 252 elt.setQName(qname); 253 elt.setParent(_currentNode); 254 elt.setStartLocation(_sourcePath, _filename, _line); 255 256 _openNode = elt; 257 258 return; 259 } 260 261 Class tagClass = null; 262 263 try { 264 tagClass = _gen.getTagClass(qname); 265 } catch (ClassNotFoundException e) { 266 tagClass = JspTagFileSupport.class; 267 log.log(Level.FINE, e.toString(), e); 268 } catch (Exception e) { 269 throw error(e); 270 } 271 272 if (tagInfo == null) 273 throw _gen.error(L.l("<{0}> is an unknown tag.", qname.getName())); 274 275 if (tagInfo instanceof TagInfoExt) { 276 TagInfoExt tagInfoExt = (TagInfoExt) tagInfo; 277 278 ArrayList <PersistentDependency> dependList = tagInfoExt.getDependList(); 279 280 for (int i = 0; i < dependList.size(); i++) { 281 _gen.addDepend(dependList.get(i)); 282 } 283 } 284 285 if (JspTagFileSupport.class.isAssignableFrom(tagClass)) { 286 TagFileTag customTag = new TagFileTag(); 287 customTag.setGenerator(_gen); 288 customTag.setParseState(_parseState); 289 customTag.setQName(qname); 290 customTag.setParent(_currentNode); 291 customTag.setTagInfo(tagInfo); 292 customTag.setTagClass(tagClass); 293 294 _openNode = customTag; 295 _openNode.setStartLocation(_sourcePath, _filename, _line); 296 } 297 else if (Tag .class.isAssignableFrom(tagClass)) { 298 CustomTag customTag = new CustomTag(); 299 customTag.setGenerator(_gen); 300 customTag.setParseState(_parseState); 301 customTag.setQName(qname); 302 customTag.setParent(_currentNode); 303 customTag.setTagInfo(tagInfo); 304 customTag.setTagClass(tagClass); 305 306 _openNode = customTag; 307 _openNode.setStartLocation(_sourcePath, _filename, _line); 308 } 309 else if (SimpleTag .class.isAssignableFrom(tagClass)) { 310 CustomSimpleTag customTag = new CustomSimpleTag(); 311 customTag.setGenerator(_gen); 312 customTag.setParseState(_parseState); 313 customTag.setQName(qname); 314 customTag.setParent(_currentNode); 315 customTag.setTagInfo(tagInfo); 316 customTag.setTagClass(tagClass); 317 318 _openNode = customTag; 319 _openNode.setStartLocation(_sourcePath, _filename, _line); 320 } 321 else 322 throw _gen.error(L.l("<{0}>: tag class {0} must either implement Tag or SimpleTag.", qname.getName(), tagClass.getName())); 323 } 324 325 331 public void startPrefixMapping(String prefix, String uri) 332 throws JspParseException 333 { 334 getParseState().pushNamespace(prefix, uri); 335 336 _gen.addOptionalTaglib(prefix, uri); 337 } 338 339 345 public void attribute(QName name, String value) 346 throws JspParseException 347 { 348 _openNode.addAttribute(name, value); 349 } 350 351 354 public void endAttributes() 355 throws JspParseException 356 { 357 _currentNode.addChild(_openNode); 358 359 _currentNode = _openNode; 360 _currentNode.setNamespace(_parseState.getNamespaces()); 361 _currentNode.endAttributes(); 362 _currentNode.setEndAttributeLocation(_filename, _line); 363 } 364 365 370 public void endElement(String name) 371 throws JspParseException 372 { 373 if (! _currentNode.getTagName().equals(name)) { 374 throw error(L.l("Close tag </{0}> does not match the current tag, <{1}>.", name, _currentNode.getTagName())); 375 } 376 377 try { 378 JspNode node = _currentNode; 379 380 _currentNode = node.getParent(); 381 382 node.setEndLocation(_filename, _line); 383 node.endElement(); 384 385 _currentNode.addChildEnd(node); 386 } catch (JspLineParseException e) { 387 throw e; 388 } catch (JspParseException e) { 389 throw error(e); 390 } catch (Exception e) { 391 throw new JspParseException(e); 392 } 393 } 394 395 400 public void text(String text) 401 throws JspParseException 402 { 403 if (_currentNode != null) { 404 JspNode node = _currentNode.addText(text); 405 406 if (node != null) { 407 node.setStartLocation(_sourcePath, _filename, _line); 408 } 409 } 410 } 411 412 417 public void text(String text, String srcFilename, int startLine, int endLine) 418 throws JspParseException 419 { 420 if (_currentNode != null) { 421 JspNode node = _currentNode.addText(text); 422 423 if (node != null) { 424 node.setStartLocation(null, srcFilename, startLine); 425 node.setEndLocation(srcFilename, endLine); 426 } 427 } 428 } 429 430 433 public JspNode getCurrentNode() 434 { 435 return _currentNode; 436 } 437 438 public JspParseException error(String message) 439 { 440 return new JspParseException(_filename + ":" + _line + ": " + message); 441 } 442 443 public JspParseException error(Throwable e) 444 { 445 if (e instanceof LineCompileException) 446 return new JspLineParseException(e); 447 else if (e instanceof CompileException) 448 return new JspLineParseException(_filename + ":" + _line + ": " + e.getMessage(), e); 449 else 450 return new JspLineParseException(_filename + ":" + _line + ": " + 451 String.valueOf(e), e); 452 } 453 454 private static void addMap(HashMap <QName,Class > map, 455 String prefix, 456 String localName, 457 String uri, 458 Class cl) 459 { 460 map.put(new QName(prefix, localName, uri), cl); 461 map.put(new QName(prefix, localName, "urn:jsptld:" + uri), cl); 462 } 463 464 static { 465 _tagMap = new HashMap <QName,Class >(); 466 467 addMap(_tagMap, "jsp", "root", JspNode.JSP_NS, JspRoot.class); 468 469 addMap(_tagMap, "jsp", "directive.page", JspNode.JSP_NS, 470 JspDirectivePage.class); 471 addMap(_tagMap, "jsp", "directive.include", JspNode.JSP_NS, 472 JspDirectiveInclude.class); 473 addMap(_tagMap, "jsp", "directive.cache", JspNode.JSP_NS, 474 NullTag.class); 475 addMap(_tagMap, "jsp", "directive.taglib", JspNode.JSP_NS, 476 JspDirectiveTaglib.class); 477 addMap(_tagMap, "jsp", "directive.attribute", JspNode.JSP_NS, 478 JspDirectiveAttribute.class); 479 addMap(_tagMap, "jsp", "directive.variable", JspNode.JSP_NS, 480 JspDirectiveVariable.class); 481 addMap(_tagMap, "jsp", "directive.tag", JspNode.JSP_NS, 482 JspDirectiveTag.class); 483 484 addMap(_tagMap, "jsp", "expression", JspNode.JSP_NS, 485 JspExpression.class); 486 addMap(_tagMap, "jsp", "scriptlet", JspNode.JSP_NS, 487 JspScriptlet.class); 488 addMap(_tagMap, "jsp", "declaration", JspNode.JSP_NS, 489 JspDeclaration.class); 490 addMap(_tagMap, "jsp", "useBean", JspNode.JSP_NS, 491 JspUseBean.class); 492 addMap(_tagMap, "jsp", "getProperty", JspNode.JSP_NS, 493 JspGetProperty.class); 494 addMap(_tagMap, "jsp", "setProperty", JspNode.JSP_NS, 495 JspSetProperty.class); 496 addMap(_tagMap, "jsp", "include", JspNode.JSP_NS, 497 JspInclude.class); 498 addMap(_tagMap, "jsp", "forward", JspNode.JSP_NS, 499 JspForward.class); 500 addMap(_tagMap, "jsp", "param", JspNode.JSP_NS, 501 JspParam.class); 502 addMap(_tagMap, "jsp", "plugin", JspNode.JSP_NS, 503 JspPlugin.class); 504 addMap(_tagMap, "jsp", "params", JspNode.JSP_NS, 505 JspParams.class); 506 addMap(_tagMap, "jsp", "fallback", JspNode.JSP_NS, 507 JspFallback.class); 508 509 addMap(_tagMap, "jsp", "attribute", JspNode.JSP_NS, 510 JspAttribute.class); 511 addMap(_tagMap, "jsp", "doBody", JspNode.JSP_NS, 512 JspDoBody.class); 513 addMap(_tagMap, "jsp", "invoke", JspNode.JSP_NS, 514 JspInvoke.class); 515 addMap(_tagMap, "jsp", "body", JspNode.JSP_NS, 516 JspBody.class); 517 addMap(_tagMap, "jsp", "text", JspNode.JSP_NS, 518 JspText.class); 519 addMap(_tagMap, "jsp", "element", JspNode.JSP_NS, 520 JspElement.class); 521 addMap(_tagMap, "jsp", "output", JspNode.JSP_NS, 522 JspOutput.class); 523 524 _fastTagMap = new HashMap <QName,Class >(_tagMap); 525 526 addMap(_fastTagMap, "resin-c", "out", JSTL_CORE_URI, JstlCoreOut.class); 528 addMap(_fastTagMap, "resin-c", "out", JSTL_RT_CORE_URI, JstlCoreOut.class); 529 addMap(_fastTagMap, "resin-c", "out", JSTL_EL_CORE_URI, JstlCoreOut.class); 530 531 addMap(_fastTagMap, "resin-c", "set", JSTL_CORE_URI, JstlCoreSet.class); 532 addMap(_fastTagMap, "resin-c", "set", JSTL_EL_CORE_URI, JstlCoreSet.class); 533 addMap(_fastTagMap, "resin-c", "set", JSTL_RT_CORE_URI, JstlCoreSet.class); 534 535 addMap(_fastTagMap, "resin-c", "remove", JSTL_CORE_URI, 536 JstlCoreRemove.class); 537 addMap(_fastTagMap, "resin-c", "remove", JSTL_EL_CORE_URI, 538 JstlCoreRemove.class); 539 addMap(_fastTagMap, "resin-c", "remove", JSTL_RT_CORE_URI, 540 JstlCoreRemove.class); 541 542 addMap(_fastTagMap, "resin-c", "catch", JSTL_CORE_URI, 543 JstlCoreCatch.class); 544 addMap(_fastTagMap, "resin-c", "catch", JSTL_EL_CORE_URI, 545 JstlCoreCatch.class); 546 addMap(_fastTagMap, "resin-c", "catch", JSTL_RT_CORE_URI, 547 JstlCoreCatch.class); 548 549 addMap(_fastTagMap, "resin-c", "if", JSTL_CORE_URI, JstlCoreIf.class); 550 addMap(_fastTagMap, "resin-c", "if", JSTL_EL_CORE_URI, JstlCoreIf.class); 551 addMap(_fastTagMap, "resin-c", "if", JSTL_RT_CORE_URI, JstlCoreIf.class); 552 553 addMap(_fastTagMap, "resin-c", "choose", JSTL_CORE_URI, 554 JstlCoreChoose.class); 555 addMap(_fastTagMap, "resin-c", "choose", JSTL_EL_CORE_URI, 556 JstlCoreChoose.class); 557 addMap(_fastTagMap, "resin-c", "choose", JSTL_RT_CORE_URI, 558 JstlCoreChoose.class); 559 560 addMap(_fastTagMap, "resin-c", "when", JSTL_CORE_URI, 561 JstlCoreWhen.class); 562 addMap(_fastTagMap, "resin-c", "when", JSTL_EL_CORE_URI, 563 JstlCoreWhen.class); 564 addMap(_fastTagMap, "resin-c", "when", JSTL_RT_CORE_URI, 565 JstlCoreWhen.class); 566 567 addMap(_fastTagMap, "resin-c", "otherwise", JSTL_CORE_URI, 568 JstlCoreOtherwise.class); 569 addMap(_fastTagMap, "resin-c", "otherwise", JSTL_EL_CORE_URI, 570 JstlCoreOtherwise.class); 571 addMap(_fastTagMap, "resin-c", "otherwise", JSTL_RT_CORE_URI, 572 JstlCoreOtherwise.class); 573 574 addMap(_fastTagMap, "resin-c", "forEach", JSTL_CORE_URI, 575 JstlCoreForEach.class); 576 addMap(_fastTagMap, "resin-c", "forEach", JSTL_EL_CORE_URI, 577 JstlCoreForEach.class); 578 addMap(_fastTagMap, "resin-c", "forEach", JSTL_RT_CORE_URI, 579 JstlCoreForEach.class); 580 581 addMap(_fastTagMap, "resin-fmt", "message", JSTL_FMT_URI, 582 JstlFmtMessage.class); 583 addMap(_fastTagMap, "resin-fmt", "message", JSTL_EL_FMT_URI, 584 JstlFmtMessage.class); 585 addMap(_fastTagMap, "resin-fmt", "message", JSTL_RT_FMT_URI, 586 JstlFmtMessage.class); 587 588 addMap(_fastTagMap, "resin-fmt", "setBundle", JSTL_FMT_URI, 589 JstlFmtSetBundle.class); 590 addMap(_fastTagMap, "resin-fmt", "setBundle", JSTL_EL_FMT_URI, 591 JstlFmtSetBundle.class); 592 addMap(_fastTagMap, "resin-fmt", "setBundle", JSTL_RT_FMT_URI, 593 JstlFmtSetBundle.class); 594 595 addMap(_fastTagMap, "resin-fmt", "bundle", JSTL_FMT_URI, 596 JstlFmtBundle.class); 597 addMap(_fastTagMap, "resin-fmt", "bundle", JSTL_EL_FMT_URI, 598 JstlFmtBundle.class); 599 addMap(_fastTagMap, "resin-fmt", "bundle", JSTL_RT_FMT_URI, 600 JstlFmtBundle.class); 601 602 addMap(_fastTagMap, "resin-fmt", "param", JSTL_FMT_URI, 603 JstlFmtParam.class); 604 addMap(_fastTagMap, "resin-fmt", "param", JSTL_EL_FMT_URI, 605 JstlFmtParam.class); 606 addMap(_fastTagMap, "resin-fmt", "param", JSTL_RT_FMT_URI, 607 JstlFmtParam.class); 608 609 addMap(_fastTagMap, "resin-xml", "out", JSTL_XML_URI, JstlXmlOut.class); 610 addMap(_fastTagMap, "resin-xml", "out", JSTL_RT_XML_URI, JstlXmlOut.class); 611 addMap(_fastTagMap, "resin-xml", "out", JSTL_EL_XML_URI, JstlXmlOut.class); 612 613 addMap(_fastTagMap, "resin-xml", "set", JSTL_XML_URI, JstlXmlSet.class); 614 addMap(_fastTagMap, "resin-xml", "set", JSTL_RT_XML_URI, JstlXmlSet.class); 615 addMap(_fastTagMap, "resin-xml", "set", JSTL_EL_XML_URI, JstlXmlSet.class); 616 617 addMap(_fastTagMap, "resin-xml", "if", JSTL_XML_URI, JstlXmlIf.class); 618 addMap(_fastTagMap, "resin-xml", "if", JSTL_RT_XML_URI, JstlXmlIf.class); 619 addMap(_fastTagMap, "resin-xml", "if", JSTL_EL_XML_URI, JstlXmlIf.class); 620 621 addMap(_fastTagMap, "resin-xml", "choose", JSTL_XML_URI, 622 JstlCoreChoose.class); 623 addMap(_fastTagMap, "resin-xml", "choose", JSTL_RT_XML_URI, 624 JstlCoreChoose.class); 625 addMap(_fastTagMap, "resin-xml", "choose", JSTL_EL_XML_URI, 626 JstlCoreChoose.class); 627 628 addMap(_fastTagMap, "resin-xml", "when", JSTL_XML_URI, 629 JstlXmlWhen.class); 630 addMap(_fastTagMap, "resin-xml", "when", JSTL_RT_XML_URI, 631 JstlXmlWhen.class); 632 addMap(_fastTagMap, "resin-xml", "when", JSTL_EL_XML_URI, 633 JstlXmlWhen.class); 634 635 addMap(_fastTagMap, "resin-xml", "otherwise", JSTL_XML_URI, 636 JstlCoreOtherwise.class); 637 addMap(_fastTagMap, "resin-xml", "otherwise", JSTL_RT_XML_URI, 638 JstlCoreOtherwise.class); 639 addMap(_fastTagMap, "resin-xml", "otherwise", JSTL_EL_XML_URI, 640 JstlCoreOtherwise.class); 641 } 642 } 643 | Popular Tags |