1 29 30 package com.caucho.jsp.java; 31 32 import com.caucho.el.Expr; 33 import com.caucho.jsp.JspLineParseException; 34 import com.caucho.jsp.JspParseException; 35 import com.caucho.jsp.JspParser; 36 import com.caucho.jsp.Namespace; 37 import com.caucho.jsp.ParseState; 38 import com.caucho.jsp.TagInstance; 39 import com.caucho.log.Log; 40 import com.caucho.util.CharBuffer; 41 import com.caucho.util.CompileException; 42 import com.caucho.util.L10N; 43 import com.caucho.util.LineCompileException; 44 import com.caucho.vfs.Path; 45 import com.caucho.vfs.WriteStream; 46 import com.caucho.xml.QName; 47 import com.caucho.xml.XmlChar; 48 import com.caucho.xpath.NamespaceContext; 49 import com.caucho.xpath.XPath; 50 51 import javax.el.MethodExpression; 52 import javax.el.ValueExpression; 53 import javax.servlet.jsp.tagext.JspFragment ; 54 import javax.servlet.jsp.tagext.TagAttributeInfo ; 55 import java.beans.PropertyEditor ; 56 import java.beans.PropertyEditorManager ; 57 import java.io.IOException ; 58 import java.lang.reflect.Method ; 59 import java.math.BigDecimal ; 60 import java.math.BigInteger ; 61 import java.util.ArrayList ; 62 import java.util.logging.Level ; 63 import java.util.logging.Logger ; 64 65 public abstract class JspNode { 66 static final L10N L = new L10N(JspNode.class); 67 static final Logger log = Log.open(JspNode.class); 68 69 static final String JSP_NS = JspParser.JSP_NS; 70 71 protected Path _sourcePath; 72 protected String _filename; 73 protected int _startLine; 74 protected int _endAttributeLine; 75 protected int _endLine; 76 77 protected Namespace _ns; 78 79 protected JavaJspGenerator _gen; 80 protected ParseState _parseState; 81 82 protected QName _name; 83 protected JspNode _parent; 84 85 protected JspNode() 86 { 87 } 88 89 92 public void setGenerator(JavaJspGenerator gen) 93 { 94 _gen = gen; 95 } 96 97 100 public void setParseState(ParseState parseState) 101 { 102 _parseState = parseState; 103 } 104 105 108 public QName getQName() 109 { 110 return _name; 111 } 112 113 116 public void setQName(QName name) 117 { 118 _name = name; 119 } 120 121 124 public String getTagName() 125 { 126 if (_name != null) 127 return _name.getName(); 128 else 129 return "jsp:unknown"; 130 } 131 132 135 public JspNode getParent() 136 { 137 return _parent; 138 } 139 140 143 public void setParent(JspNode parent) 144 { 145 _parent = parent; 146 } 147 148 151 public void setStartLocation(Path sourcePath, String filename, int line) 152 { 153 _sourcePath = sourcePath; 154 _filename = filename; 155 _startLine = line; 156 _endAttributeLine = line; 157 } 158 159 162 public void setEndAttributeLocation(String filename, int line) 163 { 164 if (_filename != null && _filename.equals(filename)) 165 _endAttributeLine = line; 166 } 167 168 171 public void setEndLocation(String filename, int line) 172 { 173 if (_filename != null && _filename.equals(filename)) 174 _endLine = line; 175 } 176 177 180 public String getFilename() 181 { 182 return _filename; 183 } 184 185 188 public int getStartLine() 189 { 190 return _startLine; 191 } 192 193 196 public int getEndAttributeLine() 197 { 198 return _endAttributeLine; 199 } 200 201 204 public int getEndLine() 205 { 206 return _endLine; 207 } 208 209 212 public boolean isStatic() 213 { 214 return false; 215 } 216 217 220 public String getStaticText() 221 { 222 CharBuffer cb = CharBuffer.allocate(); 223 224 getStaticText(cb); 225 226 return cb.close(); 227 } 228 229 232 public void getStaticText(CharBuffer cb) 233 { 234 } 235 236 239 public boolean hasScripting() 240 { 241 return false; 242 } 243 244 247 public String getBodyContent() 248 { 249 return "jsp"; 250 } 251 252 255 public boolean hasCustomTag() 256 { 257 return false; 258 } 259 260 263 public boolean hasTag() 264 { 265 return hasCustomTag(); 266 } 267 268 271 public String getCustomTagName() 272 { 273 return null; 274 } 275 276 279 public boolean isSimpleTag() 280 { 281 return false; 282 } 283 284 287 public JspNode getParentTagNode() 288 { 289 if (getCustomTagName() != null) 290 return this; 291 else { 292 JspNode parent = getParent(); 293 294 if (parent != null) 295 return parent.getParentTagNode(); 296 else 297 return null; 298 } 299 } 300 301 304 public String getParentTagName() 305 { 306 if (getCustomTagName() != null) 307 return getCustomTagName(); 308 else { 309 JspNode parent = getParent(); 310 311 if (parent != null) 312 return parent.getParentTagName(); 313 else 314 return null; 315 } 316 } 317 318 321 public boolean hasNamespace(String prefix, String uri) 322 { 323 if (_parent == null) 324 return false; 325 else 326 return _parent.hasNamespace(prefix, uri); 327 } 328 329 332 public final void addNamespace(String prefix, String value) 333 { 334 addNamespaceRec(prefix, value); 335 } 336 337 340 public final void setNamespace(Namespace ns) 341 { 342 _ns = ns; 343 } 344 345 348 public final NamespaceContext getNamespaceContext() 349 { 350 NamespaceContext ns = null; 351 352 for (Namespace ptr = _ns; ptr != null; ptr = ptr.getNext()) { 353 ns = new NamespaceContext(ns, ptr.getPrefix(), ptr.getURI()); 354 } 355 356 return ns; 357 } 358 359 362 public void addNamespaceRec(String prefix, String value) 363 { 364 if (_parent != null) 365 _parent.addNamespaceRec(prefix, value); 366 } 367 368 371 public boolean hasNamespace(QName name) 372 { 373 return hasNamespace(name.getPrefix(), name.getNamespaceURI()); 374 } 375 376 379 public void addAttribute(QName name, String value) 380 throws JspParseException 381 { 382 throw error(L.l("attribute '{0}' is not allowed in <{1}>.", 383 name.getName(), getTagName())); 384 } 385 386 392 public void addAttribute(QName name, JspAttribute value) 393 throws JspParseException 394 { 395 if (value.isStatic()) { 396 addAttribute(name, value.getStaticText().trim()); 397 } 398 else 399 throw error(L.l("attribute '{0}' is not allowed in <{1}>.", 400 name.getName(), getTagName())); 401 } 402 403 406 public void endAttributes() 407 throws JspParseException 408 { 409 } 410 411 414 public JspNode addText(String text) 415 throws JspParseException 416 { 417 for (int i = 0; i < text.length(); i++) { 418 char ch = text.charAt(i); 419 420 if (! XmlChar.isWhitespace(ch)) 421 throw error(L.l("Text is not allowed in <{0}> at '{1}'.", 422 _name.getName(), text)); 423 } 424 425 return null; 426 } 427 428 431 public void addChild(JspNode node) 432 throws JspParseException 433 { 434 node.setParent(this); 435 436 if (node instanceof JspAttribute) { 437 } 438 else if (node instanceof StaticText && 439 ((StaticText) node).isWhitespace()) { 440 } 441 else 442 throw node.error(L.l("<{0}> does not allow any child elements at {1}", 443 getTagName(), node.getTagName())); 444 } 445 446 447 450 public void addChildEnd(JspNode node) 451 throws JspParseException 452 { 453 if (node instanceof JspAttribute) { 454 JspAttribute attr = (JspAttribute) node; 455 456 QName name = attr.getName(); 457 458 addAttribute(name, attr); 459 } 460 } 461 462 465 public void endElement() 466 throws Exception 467 { 468 } 469 470 473 public ArrayList <JspNode> getChildren() 474 { 475 return null; 476 } 477 478 481 public TagInstance getTag() 482 { 483 JspNode parent = getParent(); 484 485 if (parent != null) 486 return parent.getTag(); 487 else 488 return _gen.getRootTag(); 489 } 490 491 496 abstract public void printXml(WriteStream os) 497 throws IOException ; 498 499 502 public void printJspId(WriteStream os) 503 throws IOException 504 { 505 os.print(" jsp:id=\"" + _gen.generateJspId() + "\""); 506 } 507 508 513 public void printXmlText(WriteStream os, String text) 514 throws IOException 515 { 516 os.print(xmlText(text)); 517 } 518 519 522 public String xmlText(String text) 523 { 524 if (text == null) 525 return ""; 526 527 CharBuffer cb = new CharBuffer(); 528 529 for (int i = 0; i < text.length(); i++) { 530 char ch = text.charAt(i); 531 532 switch (ch) { 533 case '<': 534 cb.append("<"); 535 break; 536 case '>': 537 cb.append(">"); 538 break; 539 case '&': 540 cb.append("&"); 541 break; 542 case '"': 543 cb.append("""); 544 break; 545 default: 546 cb.append(ch); 547 break; 548 } 549 } 550 551 return cb.toString(); 552 } 553 554 557 public String xmlAttrText(String text) 558 { 559 if (text == null) 560 return ""; 561 562 CharBuffer cb = new CharBuffer(); 563 564 for (int i = 0; i < text.length(); i++) { 565 char ch = text.charAt(i); 566 567 switch (ch) { 568 case '&': 569 cb.append("&"); 570 break; 571 case '"': 572 cb.append("""); 573 break; 574 default: 575 cb.append(ch); 576 break; 577 } 578 } 579 580 return cb.toString(); 581 } 582 583 584 587 public void generateStartLocation(JspJavaWriter out) 588 throws IOException 589 { 590 out.setLocation(_filename, _startLine); 591 } 592 593 596 public void generateEndLocation(JspJavaWriter out) 597 throws IOException 598 { 599 out.setLocation(_filename, _endLine); 600 } 601 602 605 public void generatePrologue(JspJavaWriter out) 606 throws Exception 607 { 608 generatePrologueChildren(out); 609 } 610 611 614 public void generatePrologueDeclare(JspJavaWriter out) 615 throws Exception 616 { 617 } 618 619 622 public void generatePrologueChildren(JspJavaWriter out) 623 throws Exception 624 { 625 } 626 627 630 public void generateDeclaration(JspJavaWriter out) 631 throws IOException 632 { 633 generateDeclarationChildren(out); 634 } 635 636 639 public void generateDeclarationChildren(JspJavaWriter out) 640 throws IOException 641 { 642 } 643 644 649 abstract public void generate(JspJavaWriter out) 650 throws Exception ; 651 652 657 public void generateChildren(JspJavaWriter out) 658 throws Exception 659 { 660 } 661 662 667 public void generateStatic(JspJavaWriter out) 668 throws Exception 669 { 670 } 671 672 677 public void generateEmpty() 678 throws Exception 679 { 680 generateChildrenEmpty(); 681 } 682 683 688 public void generateChildrenEmpty() 689 throws Exception 690 { 691 } 692 693 696 protected boolean attributeToBoolean(String attr, String value) 697 throws JspParseException 698 { 699 if (value.equals("yes") || value.equals("true")) 700 return true; 701 else if (value.equals("no") || value.equals("false")) 702 return false; 703 else 704 throw error(L.l("'{0}' is an unknown value for {1}. 'true' or 'false' are the expected values.", 705 value, attr)); 706 } 707 708 711 public boolean isInFragment() 712 { 713 for (JspNode node = getParent(); node != null; node = node.getParent()) { 714 if (node instanceof JspAttribute || node instanceof CustomSimpleTag) 715 return true; 716 } 717 718 return false; 719 } 720 721 void generateSetParameter(JspJavaWriter out, 722 String obj, Object objValue, Method method, 723 boolean allowRtexpr, String contextVar, 724 boolean isFragment, TagAttributeInfo attrInfo) 725 throws Exception 726 { 727 Class type = method.getParameterTypes()[0]; 728 729 if (isFragment || JspFragment .class.equals(type)) { 730 generateFragmentParameter(out, obj, objValue, method, 731 allowRtexpr, contextVar); 732 return; 733 } 734 735 if (objValue instanceof JspAttribute) { 736 JspAttribute attr = (JspAttribute) objValue; 737 738 if (attr.isStatic()) 739 objValue = attr.getStaticText(); 740 else { 741 String str = "_jsp_str_" + _gen.uniqueId(); 742 out.println("String " + str + " = " + attr.generateValue() + ";"); 743 out.println(obj + "." + method.getName() + "(" + stringToValue(type, str) + ");"); 744 return; 745 } 746 } 747 else if (objValue instanceof JspNode) 748 throw error(L.l("jsp:attribute may not set this attribute.")); 749 750 String strValue = (String ) objValue; 751 752 String convValue = generateParameterValue(type, 753 strValue, 754 allowRtexpr, 755 attrInfo); 756 757 PropertyEditor editor; 758 759 if (convValue != null) 760 out.println(obj + "." + method.getName() + "(" + convValue + ");"); 761 else if ((editor = PropertyEditorManager.findEditor(type)) != null) { 762 generateSetParameter(out, obj, strValue, method, editor.getClass()); 763 } 764 else 765 throw error(L.l("expected '<%= ... %>' at '{0}' for tag attribute setter '{1}'. Tag attributes which can't be converted from strings must use a runtime attribute expression.", 766 strValue, method.getName() + "(" + type.getName() + ")")); 767 } 768 769 void generateSetParameter(JspJavaWriter out, 770 String obj, String value, Method method, 771 Class editorClass) 772 throws Exception 773 { 774 Class type = method.getParameterTypes()[0]; 775 776 String name = "_jsp_editor" + _gen.uniqueId(); 777 out.print("java.beans.PropertyEditor " + name + " = new " + editorClass.getName() + "();"); 778 out.println(name + ".setAsText(\"" + escapeJavaString(value) + "\");"); 779 out.println(obj + "." + method.getName() + "((" + 780 type.getName() + ") " + name + ".getValue());"); 781 } 782 783 void generateFragmentParameter(JspJavaWriter out, 784 Object obj, Object objValue, Method method, 785 boolean allowRtexpr, String contextVar) 786 throws Exception 787 { 788 out.print(obj + "." + method.getName() + "("); 789 if (objValue instanceof JspFragmentNode) 790 generateFragment(out, (JspFragmentNode) objValue, contextVar); 791 else if (objValue instanceof String ) { 792 String string = (String ) objValue; 793 794 int index = _gen.addExpr(string); 795 796 out.print("new com.caucho.jsp.ELExprFragment(pageContext, _caucho_expr_" + index + ")"); 797 } 798 else { 799 throw error(L.l("can't handle fragment '{0}' of type {1}", 800 objValue, objValue.getClass())); 801 } 802 out.println(");"); 803 } 804 805 String generateFragmentParameter(String string, boolean allowRtexpr) 806 throws Exception 807 { 808 int index = _gen.addExpr(string); 809 810 return ("new com.caucho.jsp.ELExprFragment(pageContext, _caucho_expr_" + index + ")"); 811 } 812 813 816 public JspSegmentNode getSegment() 817 { 818 JspNode parent = getParent(); 819 820 if (parent != null) 821 return parent.getSegment(); 822 else 823 return null; 824 } 825 826 void generateFragment(JspJavaWriter out, JspFragmentNode frag, 827 String contextVar) 828 throws Exception 829 { 830 out.print(generateFragment(frag, contextVar)); 831 } 832 833 void generateParentTag(JspJavaWriter out, TagInstance parent) 834 throws IOException 835 { 836 String parentId = parent.getId(); 837 if (parentId == null || parentId.startsWith("top_")) { 838 out.print("null"); 839 } 840 else if (parent.isSimpleTag()) { 841 out.print("(" + parentId + "_adapter != null ? "); 842 out.print(parentId + "_adapter : "); 843 out.print("(" + parentId + "_adapter = new javax.servlet.jsp.tagext.TagAdapter(" + parentId + ")))"); 844 } 845 else 846 out.print(parentId); 847 } 848 849 String generateRTValue(Class type, Object value) 850 throws Exception 851 { 852 if (value instanceof String ) 853 return generateParameterValue(type, (String ) value, true, null); 854 else { 855 JspAttribute attr = (JspAttribute) value; 856 857 return stringToValue(type, attr.generateValue()); 858 } 859 } 860 861 864 protected String invokeFragment(JspFragmentNode frag) 865 throws Exception 866 { 867 return frag.generateValue(); 868 } 869 870 873 protected String generateFragment(JspFragmentNode frag, String contextVar) 874 throws Exception 875 { 876 int index = _gen.addFragment(frag); 877 878 StringBuffer cb = new StringBuffer (); 879 880 if (frag.isStatic()) { 881 String fragmentVar = frag.getFragmentName(); 882 883 cb.append(fragmentVar + " = com.caucho.jsp.StaticJspFragmentSupport.create(" + fragmentVar + ", " + contextVar + ", \""); 884 885 cb.append(escapeJavaString(frag.getStaticText())); 886 cb.append("\")"); 887 888 return cb.toString(); 889 } 890 891 String fragmentVar = frag.getFragmentName(); 892 893 cb.append(fragmentVar + " = _CauchoFragment.create(" + fragmentVar + ", " + 894 index + ", " + contextVar + ", "); 895 896 JspNode parentTag = getParentTagNode(); 897 898 if (parentTag == null) 899 cb.append("null"); 900 else if (frag.hasCustomTag() && parentTag instanceof CustomSimpleTag) 901 cb.append(parentTag.getCustomTagName() + "_adapter"); 902 else 903 cb.append(parentTag.getCustomTagName()); 904 905 if (_gen instanceof JavaTagGenerator) { 906 JavaTagGenerator tagGen = (JavaTagGenerator) _gen; 907 908 if (tagGen.isStaticDoTag()) cb.append(", _jspBody"); 910 else 911 cb.append(", getJspBody()"); 912 } 913 else 914 cb.append(", null"); 915 916 cb.append(")"); 917 918 return cb.toString(); 919 } 920 921 924 protected String generateParentTag(TagInstance parent) 925 throws IOException 926 { 927 String parentId = parent.getId(); 928 if (parent.isTop()) { 929 return "null"; 930 } 931 else if (parent.isSimpleTag()) { 932 CharBuffer cb = CharBuffer.allocate(); 933 934 cb.append("(" + parentId + "_adapter != null ? "); 935 cb.append(parentId + "_adapter : "); 936 cb.append("(" + parentId + "_adapter = new javax.servlet.jsp.tagext.TagAdapter(" + parentId + ")))"); 937 938 return cb.close(); 939 } 940 else 941 return parentId; 942 } 943 944 947 void generateIncludeParams(JspJavaWriter out, 948 ArrayList params) 949 throws Exception 950 { 951 boolean hasQuery = false; 952 953 for (int i = 0; i < params.size(); i++) { 954 JspParam param = (JspParam) params.get(i); 955 String value = param.getValue(); 956 957 if (hasQuery) 958 out.print("+ \"&\" + "); 959 960 hasQuery = true; 961 out.print("\"" + param.getName() + "=\""); 962 963 String outValue = generateParameterValue(String .class, value); 964 965 if (outValue.equals("null")) { 966 } 967 else if (outValue.startsWith("\"")) 968 out.print(" + (" + outValue + ")"); 969 else 970 out.print(" + com.caucho.el.Expr.toString(" + outValue + ", null)"); 971 } 972 } 973 974 String generateValue(Class type, String value) 975 throws Exception 976 { 977 return generateParameterValue(type, value, true, null); 978 } 979 980 String generateParameterValue(Class type, String value) 981 throws Exception 982 { 983 return generateParameterValue(type, value, true, null); 984 } 985 986 String generateParameterValue(Class type, 987 String value, 988 boolean rtexpr, 989 TagAttributeInfo attrInfo) 990 throws Exception 991 { 992 boolean isEmpty = value == null || value.equals(""); 993 if (isEmpty) 994 value = "0"; 995 996 try { 997 if (JspFragment .class.equals(type)) 998 return generateFragmentParameter(value, rtexpr); 999 else if (type.equals(ValueExpression.class)) { 1000 int exprIndex; 1001 1002 String typeName = attrInfo != null ? attrInfo.getExpectedTypeName() : ""; 1003 1004 if (isEmpty) 1005 exprIndex = _gen.addValueExpr("", typeName); 1006 else 1007 exprIndex = _gen.addValueExpr(value, typeName); 1008 1009 return ("_caucho_value_expr_" + exprIndex); 1010 } 1011 else if (type.equals(MethodExpression.class)) { 1012 int exprIndex; 1013 1014 String sig = attrInfo != null ? attrInfo.getMethodSignature() : ""; 1015 1016 if (isEmpty) 1017 exprIndex = _gen.addMethodExpr("", sig); 1018 else 1019 exprIndex = _gen.addMethodExpr(value, sig); 1020 1021 return ("_caucho_method_expr_" + exprIndex); 1022 } 1023 else if (com.caucho.el.Expr.class.equals(type)) { 1024 int exprIndex; 1025 1026 if (isEmpty) 1027 exprIndex = _gen.addExpr(""); 1028 else 1029 exprIndex = _gen.addExpr(value); 1030 1031 return ("_caucho_expr_" + exprIndex); 1032 } 1033 else if (com.caucho.xpath.Expr.class.equals(type)) { 1034 int exprIndex; 1035 1036 com.caucho.xpath.Expr expr; 1037 if (isEmpty) 1038 expr = XPath.parseExpr(""); 1039 else 1040 expr = XPath.parseExpr(value, getNamespaceContext()); 1041 1042 return _gen.addXPathExpr(expr); 1043 } 1044 else if (rtexpr && hasRuntimeAttribute(value)) { 1045 return getRuntimeAttribute(value); 1046 } 1047 else if (rtexpr && hasELAttribute(value)) { return generateELValue(type, value); 1049 } 1050 else if (! rtexpr && hasELAttribute(value)) { 1051 throw error(L.l("EL expression '{0}' is only allowed for attributes with rtexprvalue='true'.", 1054 value)); 1055 } 1056 else if (! rtexpr 1057 && hasDeferredAttribute(value) 1058 && ! _gen.getParseState().isDeferredSyntaxAllowedAsLiteral()) { 1059 throw error(L.l("Deferred syntax '{0}' is not allowed as a literal.", 1060 value)); 1061 } 1062 else if (type.equals(boolean.class)) 1063 return String.valueOf(Boolean.valueOf(isEmpty ? "false" : value)); 1064 else if (type.equals(Boolean .class)) { 1065 if (isEmpty) 1066 return "java.lang.Boolean.FALSE"; 1067 else 1068 return "new java.lang.Boolean(" + Boolean.valueOf(value) + ")"; 1069 } 1070 else if (type.equals(byte.class)) 1071 return "(byte) " + Byte.valueOf(value); 1072 else if (type.equals(Byte .class)) 1073 return "new java.lang.Byte((byte) " + Byte.valueOf(value) + ")"; 1074 else if (type.equals(char.class)) { 1075 if (isEmpty) 1076 return "'\\0'"; 1077 else 1078 return "'" + value.charAt(0) + "'"; 1079 } 1080 else if (type.equals(Character .class)) { 1081 if (isEmpty) 1082 return "new java.lang.Character('\\0')"; 1083 else 1084 return ("new Character('" + value.charAt(0) + "')"); 1085 } 1086 else if (type.equals(short.class)) 1087 return ("(short) " + Short.valueOf(value)); 1088 else if (type.equals(Short .class)) 1089 return ("new java.lang.Short((short) " + Short.valueOf(value) + ")"); 1090 else if (type.equals(int.class)) 1091 return String.valueOf(Integer.valueOf(value)); 1092 else if (type.equals(Integer .class)) 1093 return ("new java.lang.Integer(" + Integer.valueOf(value) + ")"); 1094 else if (type.equals(long.class)) 1095 return String.valueOf(Long.valueOf(value)); 1096 else if (type.equals(Long .class)) 1097 return ("new java.lang.Long(" + Long.valueOf(value) + ")"); 1098 else if (type.equals(float.class)) 1099 return ("(float) " + Float.valueOf(value)); 1100 else if (type.equals(Float .class)) 1101 return ("new java.lang.Float((float) " + Float.valueOf(value) + ")"); 1102 else if (type.equals(double.class)) 1103 return String.valueOf(Double.valueOf(value)); 1104 else if (type.equals(Double .class)) { 1105 double v = Double.valueOf(value); 1106 1107 if (Double.isNaN(v)) 1108 return ("new java.lang.Double(Double.NaN)"); 1109 else 1110 return ("new java.lang.Double(" + v + ")"); 1111 } 1112 else if (! type.equals(String .class) 1113 && ! type.equals(Object .class)) { 1114 return null; 1115 } 1116 else if (! isEmpty) { 1117 return '"' + escapeJavaString(value) + '"'; 1118 } 1119 else 1120 return "\"\""; 1121 } catch (NumberFormatException e) { 1122 throw error(L.l("parameter format error: {0}", e.getMessage()), e); 1123 } 1124 } 1125 1126 protected String generateELValue(Class type, String value) 1127 throws Exception 1128 { 1129 if (type.equals(com.caucho.el.Expr.class)) { 1130 int exprIndex; 1131 1132 exprIndex = _gen.addExpr(value); 1133 1134 return ("_caucho_expr_" + exprIndex); 1135 } 1136 else if (type.equals(ValueExpression.class)) { 1137 int exprIndex; 1138 1139 exprIndex = _gen.addValueExpr(value, ""); 1140 1141 return ("_caucho_value_expr_" + exprIndex); 1142 } 1143 else if (type.equals(com.caucho.xpath.Expr.class)) { 1144 com.caucho.xpath.Expr expr; 1145 1146 expr = XPath.parseExpr(value, getNamespaceContext()); 1147 1148 return _gen.addXPathExpr(expr); 1149 } 1150 1151 Expr expr = _gen.genExpr(value); 1152 1153 if (expr.isConstant()) { 1154 try { 1155 if (expr.evalObject(null) != null) { 1156 } 1157 else if (Character .class.isAssignableFrom(type)) { 1158 return "new Character((char) 0)"; 1160 } 1161 else if (Boolean .class.isAssignableFrom(type)) { 1162 return "Boolean.FALSE"; 1164 } 1165 else if (String .class.isAssignableFrom(type)) { 1166 return "\"\""; 1168 } 1169 else if (BigInteger .class.isAssignableFrom(type)) { 1170 return "java.math.BigInteger.ZERO"; 1171 } 1172 else if (BigDecimal .class.isAssignableFrom(type)) { 1173 return "java.math.BigDecimal.ZERO"; 1174 } 1175 else if (Number .class.isAssignableFrom(type)) { 1176 return "new " + type.getName() + "((byte) 0)"; 1178 } 1179 else if (Object .class.isAssignableFrom(type)) 1180 return "null"; 1181 1182 if (boolean.class.equals(type)) 1183 return expr.evalBoolean(null) ? "true" : "false"; 1184 else if (Boolean .class.equals(type)) 1185 return expr.evalBoolean(null) ? "java.lang.Boolean.TRUE" : "java.lang.Boolean.FALSE"; 1186 else if (byte.class.equals(type)) 1187 return "(byte) " + expr.evalLong(null); 1188 else if (Byte .class.equals(type)) 1189 return "new java.lang.Byte((byte) " + expr.evalLong(null) + "L)"; 1190 else if (short.class.equals(type)) 1191 return "(short) " + expr.evalLong(null); 1192 else if (Short .class.equals(type)) 1193 return "new java.lang.Short((short) " + expr.evalLong(null) + "L)"; 1194 else if (int.class.equals(type)) 1195 return "(int) " + expr.evalLong(null); 1196 else if (Integer .class.equals(type)) 1197 return "new java.lang.Integer((int) " + expr.evalLong(null) + "L)"; 1198 else if (long.class.equals(type)) 1199 return "" + expr.evalLong(null) + "L"; 1200 else if (Long .class.equals(type)) 1201 return "new java.lang.Long(" + expr.evalLong(null) + "L)"; 1202 else if (float.class.equals(type)) 1203 return "(float) " + expr.evalDouble(null); 1204 else if (Float .class.equals(type)) 1205 return "new java.lang.Float((float) " + expr.evalDouble(null) + ")"; 1206 else if (double.class.equals(type)) { 1207 double v = expr.evalDouble(null); 1208 1209 if (Double.isNaN(v)) 1210 return "Double.NaN"; 1211 else 1212 return "" + v; 1213 } 1214 else if (Double .class.equals(type)) { 1215 double v = expr.evalDouble(null); 1216 1217 if (Double.isNaN(v)) 1218 return "new Double(Double.NaN)"; 1219 else 1220 return "new java.lang.Double(" + v + ")"; 1221 } 1222 else if (char.class.equals(type)) 1223 return "((char) " + (int) expr.evalCharacter(null) + ")"; 1224 else if (Character .class.equals(type)) { 1225 return "new Character((char) " + (int) expr.evalCharacter(null) + ")"; 1227 } 1228 else if (String .class.equals(type)) 1229 return "\"" + escapeJavaString(expr.evalString(null)) + "\""; 1230 else if (BigInteger .class.equals(type)) { 1231 String v = expr.evalBigInteger(null).toString(); 1232 1233 if (v.equals("") || v.equals("0")) 1235 return "java.math.BigInteger.ZERO"; 1236 else 1237 return "new java.math.BigInteger(\"" + v + "\")"; 1238 } 1239 else if (BigDecimal .class.equals(type)) { 1240 String v = expr.evalBigDecimal(null).toString(); 1241 1242 if (v.equals("") || v.equals("0.0")) 1244 return "java.math.BigDecimal.ZERO"; 1245 else 1246 return "new java.math.BigDecimal(\"" + v + "\")"; 1247 } 1248 else if (Object .class.equals(type)) { 1249 Object cValue = expr.evalObject(null); 1250 1251 String result = generateObject(cValue); 1252 1253 if (result != null) 1254 return result; 1255 } 1256 else { 1257 Object cValue = expr.evalObject(null); 1258 1259 if ("".equals(cValue)) 1261 return "null"; 1262 } 1263 } catch (Throwable e) { 1264 log.log(Level.FINER, e.toString(), e); 1267 1268 log.fine(e.getMessage()); 1269 } 1270 } 1271 1272 int exprIndex = _gen.addExpr(expr); 1273 String var = "_caucho_expr_" + exprIndex; 1274 1275 if (boolean.class.equals(type)) 1276 return var + ".evalBoolean(_jsp_env)"; 1277 else if (Boolean .class.equals(type)) 1278 return var + ".evalBoolean(_jsp_env) ? java.lang.Boolean.TRUE : java.lang.Boolean.FALSE"; 1279 else if (byte.class.equals(type)) 1280 return "(byte) " + var + ".evalLong(_jsp_env)"; 1281 else if (Byte .class.equals(type)) 1282 return "new java.lang.Byte((byte) " + var + ".evalLong(_jsp_env))"; 1283 else if (short.class.equals(type)) 1284 return "(short) " + var + ".evalLong(_jsp_env)"; 1285 else if (Short .class.equals(type)) 1286 return "new java.lang.Short((short) " + var + ".evalLong(_jsp_env))"; 1287 else if (int.class.equals(type)) 1288 return "(int) " + var + ".evalLong(_jsp_env)"; 1289 else if (Integer .class.equals(type)) 1290 return "new java.lang.Integer((int) " + var + ".evalLong(_jsp_env))"; 1291 else if (long.class.equals(type)) 1292 return var + ".evalLong(_jsp_env)"; 1293 else if (Long .class.equals(type)) 1294 return "new java.lang.Long(" + var + ".evalLong(_jsp_env))"; 1295 else if (float.class.equals(type)) 1296 return "(float) " + var + ".evalDouble(_jsp_env)"; 1297 else if (Float .class.equals(type)) 1298 return "new java.lang.Float((float) " + var + ".evalDouble(_jsp_env))"; 1299 else if (double.class.equals(type)) 1300 return var + ".evalDouble(_jsp_env)"; 1301 else if (Double .class.equals(type)) 1302 return "new java.lang.Double(" + var + ".evalDouble(_jsp_env))"; 1303 else if (java.math.BigDecimal .class.equals(type)) 1304 return "" + var + ".evalBigDecimal(_jsp_env)"; 1305 else if (java.math.BigInteger .class.equals(type)) 1306 return "" + var + ".evalBigInteger(_jsp_env)"; 1307 else if (char.class.equals(type)) 1308 return var + ".evalCharacter(_jsp_env)"; 1309 else if (Character .class.equals(type)) 1310 return "new Character(" + var + ".evalCharacter(_jsp_env))"; 1311 else if (String .class.equals(type)) 1312 return var + ".evalString(_jsp_env)"; 1313 else if (BigInteger .class.equals(type)) 1314 return var + ".evalBigInteger(_jsp_env)"; 1315 else if (BigDecimal .class.equals(type)) 1316 return var + ".evalBigDecimal(_jsp_env)"; 1317 else if (Object .class.equals(type)) 1318 return var + ".evalObject(_jsp_env)"; 1319 else { 1320 return "(" + classToString(type) + ") " + var + ".evalObject(_jsp_env)"; 1321 } 1322 } 1323 1324 public void convertParameterValue(JspJavaWriter out, String type, String value) 1325 throws IOException 1326 { 1327 if (type.equals("boolean")) 1328 out.print("java.lang.Boolean.TRUE.equals(" + value + ")"); 1329 else if (type.equals("byte")) 1330 out.print("java.lang.Byte.valueOf(" + value + ")"); 1331 else if (type.equals("char")) 1332 out.print("java.lang.Character.valueOf(" + value + ")"); 1333 else if (type.equals("short")) 1334 out.print("java.lang.Short.valueOf(" + value + ")"); 1335 else if (type.equals("int")) 1336 out.print("((java.lang.Integer) " + value + ").intValue()"); 1337 else if (type.equals("long")) 1338 out.print("((java.lang.Long) " + value + ").longValue()"); 1339 else if (type.equals("float")) 1340 out.print("((java.lang.Float) " + value + ").floatValue()"); 1341 else if (type.equals("double")) 1342 out.print("((java.lang.Double) " + value + ").doubleValue()"); 1343 else 1344 out.print("(" + type + ")" + value); 1345 } 1346 1347 protected String classToString(Class cl) 1348 { 1349 if (cl.isArray()) 1350 return classToString(cl.getComponentType()) + "[]"; 1351 else 1352 return cl.getName(); 1353 } 1354 1355 1358 public boolean hasRuntimeAttribute(String value) 1359 throws JspParseException 1360 { 1361 if (_parseState.isScriptingInvalid()) { 1362 return false; 1364 1367 } 1368 1369 if (value.startsWith("<%=") && value.endsWith("%>")) 1370 return true; 1371 else if (value.startsWith("%=") && value.endsWith("%")) 1372 return true; 1373 else if (value.indexOf("<%=") >= 0 && 1374 value.indexOf("<%=") < value.indexOf("%>")) 1375 throw error(L.l("interpolated runtime values are forbidden by the JSP spec at '{0}'", 1376 value)); 1377 else 1378 return false; 1379 } 1380 1381 1384 public boolean hasScripting(String value) 1385 { 1386 try { 1387 return value != null && hasRuntimeAttribute(value); 1388 } catch (Exception e) { 1389 throw new RuntimeException (e); 1390 } 1391 } 1392 1393 1396 public boolean hasScripting(JspAttribute value) 1397 { 1398 return value != null && value.hasScripting(); 1399 } 1400 1401 1404 public boolean hasELAttribute(String value) 1405 { 1406 return ! _parseState.isELIgnored() && value.indexOf("${") >= 0; 1407 } 1408 1409 1412 public boolean hasDeferredAttribute(String value) 1413 { 1414 return ! _parseState.isELIgnored() && value.indexOf("#{") >= 0; 1415 } 1416 1417 1420 public String getRuntimeAttribute(String value) 1421 throws Exception 1422 { 1423 if (value.startsWith("<%=") && value.endsWith("%>")) 1424 return value.substring(3, value.length() - 2); 1425 else if (value.startsWith("%=") && value.endsWith("%")) 1426 return value.substring(2, value.length() - 1); 1427 else 1428 return value; 1429 } 1430 1431 1434 String stringToValue(Class type, String obj) 1435 { 1436 if (boolean.class.equals(type)) 1437 return "com.caucho.jsp.PageContextImpl.toBoolean(" + obj + ")"; 1438 else if (Boolean .class.equals(type)) 1439 return "java.lang.Boolean.valueOf(" + obj + ")"; 1440 else if (byte.class.equals(type)) 1441 return "java.lang.Byte.parseByte(" + obj + ")"; 1442 else if (Byte .class.equals(type)) 1443 return "java.lang.Byte.valueOf(" + obj + ")"; 1444 else if (char.class.equals(type)) 1445 return obj + ".charAt(0)"; 1446 else if (Character .class.equals(type)) 1447 return "new java.lang.Character(" + obj + ".charAt(0))"; 1448 else if (short.class.equals(type)) 1449 return "java.lang.Short.parseShort(" + obj + ")"; 1450 else if (Short .class.equals(type)) 1451 return "java.lang.Short.valueOf(" + obj + ")"; 1452 else if (int.class.equals(type)) 1453 return "java.lang.Integer.parseInt(" + obj + ")"; 1454 else if (Integer .class.equals(type)) 1455 return "java.lang.Integer.valueOf(" + obj + ")"; 1456 else if (long.class.equals(type)) 1457 return "java.lang.Long.parseLong(" + obj + ")"; 1458 else if (Long .class.equals(type)) 1459 return "java.lang.Long.valueOf(" + obj + ")"; 1460 else if (float.class.equals(type)) 1461 return "java.lang.Float.parseFloat(" + obj + ")"; 1462 else if (Float .class.equals(type)) 1463 return "java.lang.Float.valueOf(" + obj + ")"; 1464 else if (double.class.equals(type)) 1465 return "java.lang.Double.parseDouble(" + obj + ")"; 1466 else if (Double .class.equals(type)) 1467 return "java.lang.Double.valueOf(" + obj + ")"; 1468 else if (type.isAssignableFrom(String .class)) 1469 return obj; 1470 else 1471 return null; 1472 } 1473 1474 protected String generateObject(Object obj) 1475 { 1476 if (obj instanceof String ) 1477 return "\"" + escapeJavaString((String ) obj) + "\""; 1478 else if (obj instanceof Long ) 1479 return "new java.lang.Long(" + obj + "L)"; 1480 else if (obj instanceof Integer ) 1481 return "new java.lang.Integer((int) " + obj + "L)"; 1482 else if (obj instanceof Double ) { 1483 double v = (Double ) obj; 1484 1485 if (Double.isNaN(v)) 1486 return "new java.lang.Double(Double.NaN)"; 1487 else 1488 return "new java.lang.Double(" + v + ")"; 1489 } 1490 else if (obj instanceof Boolean ) 1491 return ((Boolean ) obj).booleanValue() ? "java.lang.Boolean.TRUE" : "java.lang.Boolean.FALSE"; 1492 else 1493 return null; 1494 } 1495 1496 public static String toELObject(String expr, Class type) 1497 { 1498 if (boolean.class.equals(type)) 1499 return "((" + expr + ") ? Boolean.TRUE : Boolean.FALSE)"; 1500 else if (byte.class.equals(type)) 1501 return "new Long(" + expr + ")"; 1502 else if (short.class.equals(type)) 1503 return "new Long(" + expr + ")"; 1504 else if (int.class.equals(type)) 1505 return "new Long(" + expr + ")"; 1506 else if (long.class.equals(type)) 1507 return "new Long(" + expr + ")"; 1508 else if (float.class.equals(type)) 1509 return "new Double(" + expr + ")"; 1510 else if (double.class.equals(type)) 1511 return "new Double(" + expr + ")"; 1512 else if (char.class.equals(type)) 1513 return "String.valueOf(" + expr + ")"; 1514 else 1515 return expr; 1516 } 1517 1518 1521 public static String escapeJavaString(String s) 1522 { 1523 if (s == null) 1524 return ""; 1525 1526 CharBuffer cb = CharBuffer.allocate(); 1527 for (int i = 0; i < s.length(); i++) { 1528 if (s.charAt(i) == '\\') 1529 cb.append("\\\\"); 1530 else if (s.charAt(i) == '"') 1531 cb.append("\\\""); 1532 else if (s.charAt(i) == '\n') 1533 cb.append("\\n"); 1534 else if (s.charAt(i) == '\r') 1535 cb.append("\\r"); 1536 else 1537 cb.append(s.charAt(i)); 1538 } 1539 1540 return cb.close(); 1541 } 1542 1543 protected Class loadClass(String type) 1544 throws JspParseException 1545 { 1546 if (type == null) 1547 return null; 1548 else { 1549 try { 1550 return _gen.getBeanClass(type); 1551 } catch (Exception e) { 1552 throw new JspParseException(e); 1553 } 1554 } 1555 } 1556 1557 1560 protected JspParseException error(String msg) 1561 { 1562 return error(msg, null); 1563 } 1564 1565 1568 protected JspParseException error(String msg, Throwable e) 1569 { 1570 if (_filename != null) { 1571 String lines = _gen.getSourceLines(_sourcePath, _startLine); 1572 1573 return new JspLineParseException(_filename + ":" + _startLine + ": " + msg + lines, e); 1574 } 1575 else 1576 return new JspParseException(msg, e); 1577 } 1578 1579 1582 protected JspParseException error(Throwable e) 1583 { 1584 if (e instanceof JspLineParseException) 1585 return (JspParseException) e; 1586 else if (_filename == null || e instanceof LineCompileException) 1587 return new JspLineParseException(e); 1588 else if (e instanceof CompileException) 1589 return new JspLineParseException(_filename + ":" + _startLine + ": " + 1590 e.getMessage(), e); 1591 else 1592 return new JspLineParseException(_filename + ":" + _startLine + ": " + 1593 String.valueOf(e), e); 1594 } 1595 1596 1599 public String toString() 1600 { 1601 if (_name == null) 1602 return "<" + getClass().getName() + ">"; 1603 else 1604 return "<" + _name.getName() + ">"; 1605 } 1606} 1607 | Popular Tags |