1 28 29 package com.caucho.jsp.java; 30 31 import com.caucho.config.types.Signature; 32 import com.caucho.jsp.JspParseException; 33 import com.caucho.jsp.ParseTagManager; 34 import com.caucho.jsp.cfg.TldAttribute; 35 import com.caucho.jsp.cfg.TldTag; 36 import com.caucho.jsp.cfg.TldVariable; 37 import com.caucho.log.Log; 38 import com.caucho.util.L10N; 39 40 import javax.servlet.jsp.tagext.TagInfo ; 41 import javax.servlet.jsp.tagext.TagLibraryInfo ; 42 import java.io.IOException ; 43 import java.util.ArrayList ; 44 import java.util.HashSet ; 45 import java.util.logging.Level ; 46 import java.util.logging.Logger ; 47 48 56 public class JavaTagGenerator extends JavaJspGenerator { 57 static final L10N L = new L10N(JavaTagGenerator.class); 58 static final Logger log = Log.open(JavaTagGenerator.class); 59 60 private static HashSet <String > _reserved 61 = new HashSet <String >(); 62 63 private String _description = null; 64 private String _displayName = null; 65 private String _smallIcon = null; 66 private String _largeIcon = null; 67 private String _example = null; 68 private String _bodyContent = null; 69 private String _dynamicAttributes = null; 70 71 private ArrayList <TldAttribute> _attributes = new ArrayList <TldAttribute>(); 72 private ArrayList <TldVariable> _variables = new ArrayList <TldVariable>(); 73 74 public JavaTagGenerator(ParseTagManager tagManager) 75 { 76 super(tagManager); 77 78 setOmitXmlDeclaration(true); 79 } 80 81 public void init() 82 { 83 super.init(); 84 85 setOmitXmlDeclaration(true); 86 } 87 88 91 98 99 public void setDescription(String description) 100 { 101 _description = description; 102 } 103 104 public String getDescription() 105 { 106 return _description; 107 } 108 109 public void setDisplayName(String displayName) 110 { 111 _displayName = displayName; 112 } 113 114 public String getDisplayName() 115 { 116 return _displayName; 117 } 118 119 public void setSmallIcon(String smallIcon) 120 { 121 _smallIcon = smallIcon; 122 } 123 124 public String getSmallIcon() 125 { 126 return _smallIcon; 127 } 128 129 public void setLargeIcon(String largeIcon) 130 { 131 _largeIcon = largeIcon; 132 } 133 134 public String getLargeIcon() 135 { 136 return _largeIcon; 137 } 138 139 public void setExample(String example) 140 { 141 _example = example; 142 } 143 144 public String getExample() 145 { 146 return _example; 147 } 148 149 152 public void setBodyContent(String bodyContent) 153 { 154 _bodyContent = bodyContent; 155 } 156 157 160 public String getBodyContent() 161 { 162 return _bodyContent; 163 } 164 165 168 public void setDynamicAttributes(String dynamicAttributes) 169 { 170 _dynamicAttributes = dynamicAttributes; 171 } 172 173 176 public String getDynamicAttributes() 177 { 178 return _dynamicAttributes; 179 } 180 181 184 public void addAttribute(TldAttribute attribute) 185 { 186 _attributes.add(attribute); 187 } 188 189 192 public ArrayList <TldAttribute> getAttributes() 193 { 194 return _attributes; 195 } 196 197 200 public TldAttribute findAttribute(String name) 201 { 202 for (int i = 0; i < _attributes.size(); i++) { 203 TldAttribute attr = _attributes.get(i); 204 205 if (name.equals(attr.getName())) 206 return attr; 207 } 208 209 return null; 210 } 211 212 215 public void addVariable(TldVariable var) 216 { 217 _variables.add(var); 218 } 219 220 223 public TldVariable findVariable(String name) 224 { 225 for (int i = 0; i < _variables.size(); i++) { 226 TldVariable var = _variables.get(i); 227 228 if (name.equals(var.getNameGiven()) 230 || name.equals(var.getAlias())) 231 return var; 232 } 233 234 return null; 235 } 236 237 240 public ArrayList <TldVariable> getVariables() 241 { 242 return _variables; 243 } 244 245 public boolean isTag() 246 { 247 return true; 248 } 249 250 253 boolean isXml() 254 { 255 return _parseState.isXml(); 256 } 257 258 261 protected void generate(JspJavaWriter out) 262 throws Exception 263 { 264 out.setLineMap(_lineMap); 265 266 generateClassHeader(out); 267 268 generateAttributes(out); 269 270 if (_dynamicAttributes != null) 271 generateDynamicAttributes(out); 272 273 generateDoTag(out, _rootNode); 274 275 if (isStaticDoTag()) 276 generateStaticDoTag(out, _rootNode); 277 278 generateTagInfo(out); 279 280 generateClassFooter(out); 281 } 282 283 protected boolean isStaticDoTag() 284 { 285 return ! hasScripting(); 286 } 287 288 293 protected void generateClassHeader(JspJavaWriter out) 294 throws IOException , JspParseException 295 { 296 out.println("/*"); 297 out.println(" * JSP-Tag generated by " + com.caucho.Version.FULL_VERSION); 298 out.println(" */" ); 299 out.println(); 300 301 if (_pkg != null && ! _pkg.equals("")) 302 out.println("package " + _pkg + ";"); 303 304 out.println("import javax.servlet.*;"); 305 out.println("import javax.servlet.jsp.*;"); 306 out.println("import javax.servlet.http.*;"); 307 308 fillSingleTaglibImports(); 309 310 ArrayList <String > imports = _parseState.getImportList(); 311 for (int i = 0; i < imports.size(); i++) { 312 String name = imports.get(i); 313 out.print("import "); 314 out.print(name); 315 out.println(";"); 316 } 317 _parseState.addImport("javax.servlet.*"); 318 _parseState.addImport("javax.servlet.jsp.*"); 319 _parseState.addImport("javax.servlet.http.*"); 320 _parseState.addImport("java.lang.*"); 321 out.println(); 322 323 out.print("public class "); 324 out.print(_className); 325 326 if (hasScripting()) 327 out.print(" extends com.caucho.jsp.java.JspTagSupport"); 328 else 329 out.print(" extends com.caucho.jsp.java.JspTagFileSupport"); 330 331 if (_dynamicAttributes != null) 332 out.print(" implements javax.servlet.jsp.tagext.DynamicAttributes"); 333 334 out.println(" {"); 335 out.pushDepth(); 336 337 out.println("private final java.util.HashMap<String,java.lang.reflect.Method> _jsp_functionMap = new java.util.HashMap<String,java.lang.reflect.Method>();"); 338 339 out.println("private boolean _caucho_isDead;"); 340 341 for (int i = 0; i < _declarations.size(); i++) { 342 JspDeclaration decl = _declarations.get(i); 343 344 out.println(); 345 decl.generateDeclaration(out); 346 } 347 } 348 349 354 protected void generateAttributes(JspJavaWriter out) 355 throws IOException , JspParseException 356 { 357 for (int i = 0; i < _attributes.size(); i++) { 358 TldAttribute attr = _attributes.get(i); 359 360 String name = attr.getName(); 361 362 String upperName; 363 char ch = name.charAt(0); 364 upperName = Character.toUpperCase(ch) + name.substring(1); 365 366 Class cl = attr.getType(); 367 if (cl == null) 368 cl = String .class; 369 String type = cl.getName(); 370 371 String isSetName = "_jsp_" + name + "_isSet"; 372 373 String fieldName = toFieldName(name); 374 375 out.println(); 376 out.print("private "); 377 out.printClass(cl); 378 out.println(" " + fieldName + ";"); 379 out.println("private boolean " + isSetName + ";"); 380 381 out.println(); 382 out.print("public void set" + upperName + "("); 383 out.printClass(cl); 384 out.println(" value)"); 385 out.println("{"); 386 out.pushDepth(); 387 out.println("this." + isSetName + " = true;"); 388 out.println("this." + fieldName + " = value;"); 389 out.popDepth(); 390 out.println("}"); 391 392 402 } 403 } 404 405 410 protected void generateDynamicAttributes(JspJavaWriter out) 411 throws IOException , JspParseException 412 { 413 String dyn = toFieldName(_dynamicAttributes); 414 415 out.println(); 416 out.println("java.util.HashMap " + dyn + " = new java.util.HashMap();"); 417 out.println(); 418 out.println("public void setDynamicAttribute(String uri, String localName, Object value)"); 419 out.println(" throws javax.servlet.jsp.JspException"); 420 out.println("{"); 421 out.println(" if (uri == null || \"\".equals(uri))"); 422 out.println(" " + dyn + ".put(localName, value);"); 423 out.println("}"); 424 } 425 426 429 protected void generateDoTag(JspJavaWriter out, JspNode node) 430 throws Exception 431 { 432 out.println(); 433 out.println("public void doTag()"); 434 out.println(" throws javax.servlet.jsp.JspException, java.io.IOException"); 435 out.println("{"); 436 out.pushDepth(); 437 438 out.println("javax.servlet.jsp.JspContext _jsp_parentContext = getJspContext();"); 439 out.println("com.caucho.jsp.PageContextWrapper pageContext = com.caucho.jsp.PageContextWrapper.create(_jsp_parentContext);"); 440 out.println("setJspContext(pageContext);"); 442 443 if (hasScripting()) { 444 out.println("javax.servlet.http.HttpServletRequest request = (javax.servlet.http.HttpServletRequest) pageContext.getRequest();"); 445 out.println("javax.servlet.http.HttpServletResponse response = (javax.servlet.http.HttpServletResponse) pageContext.getResponse();"); 446 out.println("javax.servlet.http.HttpSession session = pageContext.getSession();"); 447 out.println("javax.servlet.ServletContext application = pageContext.getServletContext();"); 448 out.println("javax.servlet.ServletConfig config = pageContext.getServletConfig();"); 449 } 450 451 out.println("com.caucho.jsp.PageContextWrapper jspContext = pageContext;"); 452 out.println("javax.el.ELContext _jsp_env = pageContext.getELContext();"); 453 out.println("javax.servlet.jsp.JspWriter out = pageContext.getOut();"); 454 generateTagAttributes(out); 455 if (hasScripting()) 456 generatePrologue(out); 457 458 out.println("try {"); 459 out.pushDepth(); 460 461 if (hasScripting()) { 462 node.generate(out); 463 464 generateTagVariablesAtEnd(out); 465 } else { 466 out.println("doTag(_jsp_parentContext, pageContext, out, null);"); 467 } 468 469 out.popDepth(); 470 out.println("} catch (Throwable e) {"); 471 out.println(" if (e instanceof java.io.IOException)"); 472 out.println(" throw (java.io.IOException) e;"); 473 out.println(" throw com.caucho.jsp.QJspException.createJspException(e);"); 474 out.println("}"); 475 476 if (hasScripting() && _variables.size() > 0) { 477 out.println("finally {"); 478 out.pushDepth(); 479 480 generateTagVariablesAtEnd(out); 481 482 out.println("setJspContext(_jsp_parentContext);"); 483 out.println("com.caucho.jsp.PageContextWrapper.free(pageContext);"); 484 485 out.popDepth(); 486 out.println("}"); 487 } 488 489 out.popDepth(); 490 out.println("}"); 491 } 492 493 498 protected void generateTagAttributes(JspJavaWriter out) 499 throws IOException , JspParseException 500 { 501 for (int i = 0; i < _attributes.size(); i++) { 502 TldAttribute attr = _attributes.get(i); 503 504 String name = attr.getName(); 505 506 String upperName; 507 char ch = name.charAt(0); 508 upperName = Character.toUpperCase(ch) + name.substring(1); 509 510 Class cl = attr.getType(); 511 if (cl == null) 512 cl = String .class; 513 String type = cl.getName(); 514 515 String isSetName = "_jsp_" + name + "_isSet"; 516 String fieldName = toFieldName(name); 517 518 out.println("if (" + isSetName + ")"); 519 out.println(" pageContext.setAttribute(\"" + name + "\", " + 520 JspNode.toELObject(fieldName, cl) + ");"); 521 } 522 523 if (_dynamicAttributes != null) { 525 out.println("pageContext.setAttribute(\"" + _dynamicAttributes + "\"," + 526 toFieldName(_dynamicAttributes) + ");"); 527 } 528 } 529 530 533 protected void generateStaticDoTag(JspJavaWriter out, JspNode node) 534 throws Exception 535 { 536 out.println(); 537 out.println("public static void doTag(javax.servlet.jsp.JspContext _jsp_parentContext,"); 538 out.println(" com.caucho.jsp.PageContextWrapper pageContext,"); 539 out.println(" javax.servlet.jsp.JspWriter out,"); 540 out.println(" javax.servlet.jsp.tagext.JspFragment _jspBody)"); 541 out.println(" throws Throwable"); 542 out.println("{"); 543 out.pushDepth(); 544 545 out.println("javax.el.ELContext _jsp_env = pageContext.getELContext();"); 546 generatePrologue(out); 547 548 out.println("try {"); 549 out.pushDepth(); 550 551 node.generate(out); 552 553 out.popDepth(); 554 out.println("} finally {"); 555 out.pushDepth(); 556 557 generateTagVariablesAtEnd(out); 558 559 out.println("com.caucho.jsp.PageContextWrapper.free(pageContext);"); 560 561 out.popDepth(); 562 out.println("}"); 563 564 out.popDepth(); 565 out.println("}"); 566 } 567 568 573 protected void generatePrologue(JspJavaWriter out) 574 throws Exception 575 { 576 _rootNode.generatePrologue(out); 577 578 for (int i = 0; i < _variables.size(); i++) { 579 TldVariable var = _variables.get(i); 580 581 if (var.getNameFromAttribute() != null) { 582 out.print("String _jsp_var_from_attribute_" + i + " = (String) "); 583 out.println("pageContext.getAttribute(\"" + 584 var.getNameFromAttribute() + "\");"); 585 } 586 587 if ("AT_END".equals(var.getScope())) 588 continue; 589 590 String srcName = var.getNameGiven(); 591 if (srcName == null) 592 srcName = var.getAlias(); 593 594 String dstName; 595 if (var.getNameGiven() != null) 596 dstName = "\"" + var.getNameGiven() + "\""; 597 else 598 dstName = "_jsp_var_from_attribute_" + i; 599 600 if ("NESTED".equals(var.getScope())) { 601 out.print("Object _jsp_nested_var_" + i + " = "); 602 out.println("_jsp_parentContext.getAttribute(" + dstName + ");"); 603 } 604 610 } 611 } 612 613 618 protected void generateTagVariablesAtEnd(JspJavaWriter out) 619 throws IOException , JspParseException 620 { 621 for (int i = 0; i < _variables.size(); i++) { 622 TldVariable var = _variables.get(i); 623 624 String srcName = var.getNameGiven(); 625 if (srcName == null) 626 srcName = var.getAlias(); 627 628 String dstName; 629 if (var.getNameGiven() != null) 630 dstName = "\"" + var.getNameGiven() + "\""; 631 else 632 dstName = "_jsp_var_from_attribute_" + i; 633 634 if ("NESTED".equals(var.getScope())) { 635 out.println("_jsp_parentContext.setAttribute(" + dstName + ", _jsp_nested_var_" + i + ");"); 636 } 637 else { 638 out.print("_jsp_parentContext.setAttribute(" + dstName + ","); 639 out.println("pageContext.getAttribute(\"" + srcName + "\"));"); 640 } 641 } 642 } 643 644 public TagInfo generateTagInfo(String className, TagLibraryInfo taglib) 645 { 646 init(className); 647 648 TldTag tag = new TldTag(); 649 650 for (int i = 0; i < _attributes.size(); i++) { 651 TldAttribute attr = _attributes.get(i); 652 653 tag.addAttribute(attr); 654 } 655 656 for (int i = 0; i < _variables.size(); i++) { 657 TldVariable var = _variables.get(i); 658 659 try { 660 tag.addVariable(var); 661 } catch (Exception e) { 662 log.log(Level.WARNING, e.toString(), e); 663 } 664 } 665 666 String bodyContent = _bodyContent; 667 if (bodyContent == null) 668 bodyContent = "scriptless"; 669 670 return new TagInfoExt(tag.getName(), 671 _fullClassName, 672 bodyContent, 673 getDescription(), 674 taglib, 675 null, 676 tag.getAttributes(), 677 getDisplayName(), 678 getSmallIcon(), 679 getLargeIcon(), 680 tag.getVariables(), 681 _dynamicAttributes != null, 682 _dynamicAttributes, 683 null); 684 } 685 686 691 protected void generateTagInfo(JspJavaWriter out) 692 throws IOException , JspParseException 693 { 694 704 705 out.println(); 706 out.println("public javax.servlet.jsp.tagext.TagInfo _caucho_getTagInfo(javax.servlet.jsp.tagext.TagLibraryInfo taglib)"); 707 out.println(" throws com.caucho.config.ConfigException"); 708 out.println("{"); 709 out.pushDepth(); 710 out.println("com.caucho.jsp.cfg.TldTag tag = new com.caucho.jsp.cfg.TldTag();"); 711 712 out.println("tag.setName(\"test\");"); 713 714 out.println("com.caucho.jsp.cfg.TldAttribute attr;"); 715 for (int i = 0; i < _attributes.size(); i++) { 716 TldAttribute attr = _attributes.get(i); 717 718 out.println("attr = new com.caucho.jsp.cfg.TldAttribute();"); 719 out.println("attr.setName(\"" + attr.getName() + "\");"); 720 721 Class type = attr.getType(); 722 if (type != null) { 723 out.print("attr.setType("); 724 out.printClass(type); 725 out.println(".class);"); 726 } 727 out.println("attr.setRtexprvalue(" + attr.getRtexprvalue() + ");"); 728 out.println("attr.setRequired(" + attr.getRequired() + ");"); 729 730 if (attr.getDeferredValue() != null) { 731 out.println("attr.setDeferredValue(new com.caucho.jsp.cfg.TldAttribute.DeferredValue());"); 732 733 if (attr.getDeferredValue().getType() != null) { 734 out.print("attr.getDeferredValue().setType(\""); 735 out.printJavaString(attr.getDeferredValue().getType()); 736 out.println("\");"); 737 } 738 } 739 740 if (attr.getDeferredMethod() != null) { 741 out.println("attr.setDeferredMethod(new com.caucho.jsp.cfg.TldAttribute.DeferredMethod());"); 742 743 Signature sig = attr.getDeferredMethod().getMethodSignature(); 744 745 if (sig != null) { 746 out.print("attr.getDeferredMethod().setMethodSignature("); 747 out.print("new com.caucho.config.types.Signature(\""); 748 out.printJavaString(sig.getSignature()); 749 out.println("\"));"); 750 } 751 } 752 753 out.println("tag.addAttribute(attr);"); 754 } 755 756 out.println("com.caucho.jsp.cfg.TldVariable var;"); 757 for (int i = 0; i < _variables.size(); i++) { 758 TldVariable var = _variables.get(i); 759 760 out.println("var = new com.caucho.jsp.cfg.TldVariable();"); 761 762 if (var.getNameGiven() != null) 763 out.println("var.setNameGiven(\"" + var.getNameGiven() + "\");"); 764 765 if (var.getNameFromAttribute() != null) 766 out.println("var.setNameFromAttribute(\"" + var.getNameFromAttribute() + "\");"); 767 768 String type = var.getVariableClass(); 769 if (type != null) 770 out.println("var.setVariableClass(\"" + type + "\");"); 771 out.println("var.setDeclare(" + var.getDeclare() + ");"); 772 if (var.getScope() != null) 773 out.println("var.setScope(\"" + var.getScope() + "\");"); 774 775 out.println("tag.addVariable(var);"); 776 } 777 778 String bodyContent = _bodyContent; 779 if (bodyContent == null) 780 bodyContent = "scriptless"; 781 782 out.println("return new com.caucho.jsp.java.TagInfoExt(tag.getName(),"); 783 out.println(" getClass().getName(),"); 784 out.println(" \"" + bodyContent + "\","); 785 out.print(" \""); 786 if (_description != null) 787 out.printJavaString(_description); 788 else 789 out.printJavaString("A simple tag"); 790 out.println("\","); 791 out.println(" taglib,"); 792 out.println(" null,"); 793 out.println(" tag.getAttributes(),"); 794 795 if (_displayName != null) { 796 out.print(" \""); 797 out.printJavaString(_displayName); 798 out.println("\","); 799 } 800 else { 801 out.println(" null,"); 802 } 803 804 if (_smallIcon != null) { 805 out.print(" \""); 806 out.printJavaString(_smallIcon); 807 out.println("\","); 808 } 809 else { 810 out.println(" null,"); 811 } 812 813 if (_largeIcon != null) { 814 out.print(" \""); 815 out.printJavaString(_largeIcon); 816 out.println("\","); 817 } 818 else { 819 out.println(" null,"); 820 } 821 822 out.println(" tag.getVariables(),"); 823 out.println(" " + (_dynamicAttributes != null) + ","); 824 if (_dynamicAttributes != null) 825 out.println(" \"" + _dynamicAttributes + "\","); 826 else 827 out.println(" null,"); 828 out.println(" _caucho_depends);"); 829 830 out.popDepth(); 831 out.println("}"); 832 833 out.println(); 834 out.println("public String _caucho_getDynamicAttributes()"); 835 out.println("{"); 836 out.pushDepth(); 837 838 if (_dynamicAttributes != null) 839 out.println("return \"" + _dynamicAttributes + "\";"); 840 else 841 out.println("return null;"); 842 843 out.popDepth(); 844 out.println("}"); 845 846 generateTagLibrary(out); 847 } 848 849 854 protected void generateTagLibrary(JspJavaWriter out) 855 throws IOException , JspParseException 856 { 857 out.println(); 858 out.println("private javax.servlet.jsp.tagext.TagLibraryInfo _caucho_getTagLibrary()"); 859 out.println(" throws com.caucho.config.ConfigException"); 860 out.println("{"); 861 out.pushDepth(); 862 863 out.println("return new com.caucho.jsp.java.TagTaglib(\"x\", \"http://test.com\");"); 864 out.popDepth(); 865 out.println("}"); 866 } 867 868 private String toFieldName(String name) 869 { 870 if (hasScripting() && ! _reserved.contains(name)) 871 return name; 872 else 873 return "_" + name; 874 } 875 876 static { 877 _reserved.add("public"); 878 _reserved.add("private"); 879 _reserved.add("protected"); 880 _reserved.add("static"); 881 _reserved.add("final"); 882 _reserved.add("class"); 883 _reserved.add("interface"); 884 _reserved.add("extends"); 885 _reserved.add("implements"); 886 _reserved.add("package"); 887 _reserved.add("import"); 888 _reserved.add("new"); 889 _reserved.add("if"); 890 _reserved.add("else"); 891 _reserved.add("for"); 892 _reserved.add("do"); 893 _reserved.add("while"); 894 _reserved.add("break"); 895 _reserved.add("continue"); 896 _reserved.add("switch"); 897 _reserved.add("case"); 898 _reserved.add("default"); 899 _reserved.add("throw"); 900 _reserved.add("enum"); 901 _reserved.add("throws"); 902 903 _reserved.add("void"); 904 _reserved.add("boolean"); 905 _reserved.add("byte"); 906 _reserved.add("char"); 907 _reserved.add("short"); 908 _reserved.add("int"); 909 _reserved.add("long"); 910 _reserved.add("float"); 911 _reserved.add("double"); 912 } 913 } 914 | Popular Tags |