1 17 18 package org.apache.jasper.compiler; 19 20 import java.io.CharArrayWriter ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.InputStreamReader ; 24 import java.io.UnsupportedEncodingException ; 25 import java.util.Vector ; 26 import java.util.jar.JarFile ; 27 import java.util.zip.ZipEntry ; 28 29 import javax.el.FunctionMapper; 30 import javax.servlet.jsp.el.ELException ; 31 import javax.servlet.jsp.el.ELParseException ; 32 import javax.servlet.jsp.el.ExpressionEvaluator ; 33 34 35 import org.apache.el.ExpressionFactoryImpl; 36 import org.apache.jasper.Constants; 37 import org.apache.jasper.JasperException; 38 import org.apache.jasper.JspCompilationContext; 39 import org.apache.jasper.el.ExpressionEvaluatorImpl; 40 import org.xml.sax.Attributes ; 41 42 53 public class JspUtil { 54 55 private static final String WEB_INF_TAGS = "/WEB-INF/tags/"; 56 private static final String META_INF_TAGS = "/META-INF/tags/"; 57 58 private static final String OPEN_EXPR = "<%="; 60 private static final String CLOSE_EXPR = "%>"; 61 private static final String OPEN_EXPR_XML = "%="; 62 private static final String CLOSE_EXPR_XML = "%"; 63 64 private static int tempSequenceNumber = 0; 65 66 69 private final static ExpressionEvaluator expressionEvaluator = 71 new ExpressionEvaluatorImpl(new ExpressionFactoryImpl()); 72 73 private static final String javaKeywords[] = { 74 "abstract", "assert", "boolean", "break", "byte", "case", 75 "catch", "char", "class", "const", "continue", 76 "default", "do", "double", "else", "enum", "extends", 77 "final", "finally", "float", "for", "goto", 78 "if", "implements", "import", "instanceof", "int", 79 "interface", "long", "native", "new", "package", 80 "private", "protected", "public", "return", "short", 81 "static", "strictfp", "super", "switch", "synchronized", 82 "this", "throws", "transient", "try", "void", 83 "volatile", "while" }; 84 85 public static final int CHUNKSIZE = 1024; 86 87 public static char[] removeQuotes(char []chars) { 88 CharArrayWriter caw = new CharArrayWriter (); 89 for (int i = 0; i < chars.length; i++) { 90 if (chars[i] == '%' && chars[i+1] == '\\' && 91 chars[i+2] == '>') { 92 caw.write('%'); 93 caw.write('>'); 94 i = i + 2; 95 } else { 96 caw.write(chars[i]); 97 } 98 } 99 return caw.toCharArray(); 100 } 101 102 public static char[] escapeQuotes (char []chars) { 103 String s = new String (chars); 105 while (true) { 106 int n = s.indexOf("%\\>"); 107 if (n < 0) 108 break; 109 StringBuffer sb = new StringBuffer (s.substring(0, n)); 110 sb.append("%>"); 111 sb.append(s.substring(n + 3)); 112 s = sb.toString(); 113 } 114 chars = s.toCharArray(); 115 return (chars); 116 117 118 130 } 131 132 141 public static boolean isExpression(String token, boolean isXml) { 142 String openExpr; 143 String closeExpr; 144 if (isXml) { 145 openExpr = OPEN_EXPR_XML; 146 closeExpr = CLOSE_EXPR_XML; 147 } else { 148 openExpr = OPEN_EXPR; 149 closeExpr = CLOSE_EXPR; 150 } 151 if (token.startsWith(openExpr) && token.endsWith(closeExpr)) { 152 return true; 153 } else { 154 return false; 155 } 156 } 157 158 162 public static String getExpr (String expression, boolean isXml) { 163 String returnString; 164 String openExpr; 165 String closeExpr; 166 if (isXml) { 167 openExpr = OPEN_EXPR_XML; 168 closeExpr = CLOSE_EXPR_XML; 169 } else { 170 openExpr = OPEN_EXPR; 171 closeExpr = CLOSE_EXPR; 172 } 173 int length = expression.length(); 174 if (expression.startsWith(openExpr) && 175 expression.endsWith(closeExpr)) { 176 returnString = expression.substring( 177 openExpr.length(), length - closeExpr.length()); 178 } else { 179 returnString = ""; 180 } 181 return returnString; 182 } 183 184 187 public static String getExprInXml(String expression) { 188 String returnString; 189 int length = expression.length(); 190 191 if (expression.startsWith(OPEN_EXPR) 192 && expression.endsWith(CLOSE_EXPR)) { 193 returnString = expression.substring (1, length - 1); 194 } else { 195 returnString = expression; 196 } 197 198 return escapeXml(returnString.replace(Constants.ESC, '$')); 199 } 200 201 213 public static void checkScope(String scope, Node n, ErrorDispatcher err) 214 throws JasperException { 215 if (scope != null && !scope.equals("page") && !scope.equals("request") 216 && !scope.equals("session") && !scope.equals("application")) { 217 err.jspError(n, "jsp.error.invalid.scope", scope); 218 } 219 } 220 221 227 public static void checkAttributes(String typeOfTag, 228 Node n, 229 ValidAttribute[] validAttributes, 230 ErrorDispatcher err) 231 throws JasperException { 232 Attributes attrs = n.getAttributes(); 233 Mark start = n.getStart(); 234 boolean valid = true; 235 236 int tempLength = (attrs == null) ? 0 : attrs.getLength(); 238 Vector temp = new Vector (tempLength, 1); 239 for (int i = 0; i < tempLength; i++) { 240 String qName = attrs.getQName(i); 241 if ((!qName.equals("xmlns")) && (!qName.startsWith("xmlns:"))) 242 temp.addElement(qName); 243 } 244 245 Node.Nodes tagBody = n.getBody(); 247 if( tagBody != null ) { 248 int numSubElements = tagBody.size(); 249 for( int i = 0; i < numSubElements; i++ ) { 250 Node node = tagBody.getNode( i ); 251 if( node instanceof Node.NamedAttribute ) { 252 String attrName = node.getAttributeValue( "name" ); 253 temp.addElement( attrName ); 254 if (n.getAttributeValue(attrName) != null) { 256 err.jspError(n, "jsp.error.duplicate.name.jspattribute", 257 attrName); 258 } 259 } 260 else { 261 break; 264 } 265 } 266 } 267 268 273 String missingAttribute = null; 274 275 for (int i = 0; i < validAttributes.length; i++) { 276 int attrPos; 277 if (validAttributes[i].mandatory) { 278 attrPos = temp.indexOf(validAttributes[i].name); 279 if (attrPos != -1) { 280 temp.remove(attrPos); 281 valid = true; 282 } else { 283 valid = false; 284 missingAttribute = validAttributes[i].name; 285 break; 286 } 287 } 288 } 289 290 if (!valid) 292 err.jspError(start, "jsp.error.mandatory.attribute", typeOfTag, 293 missingAttribute); 294 295 int attrLeftLength = temp.size(); 297 if (attrLeftLength == 0) 298 return; 299 300 String attribute = null; 302 303 for (int j = 0; j < attrLeftLength; j++) { 304 valid = false; 305 attribute = (String ) temp.elementAt(j); 306 for (int i = 0; i < validAttributes.length; i++) { 307 if (attribute.equals(validAttributes[i].name)) { 308 valid = true; 309 break; 310 } 311 } 312 if (!valid) 313 err.jspError(start, "jsp.error.invalid.attribute", typeOfTag, 314 attribute); 315 } 316 } 318 319 public static String escapeQueryString(String unescString) { 320 if ( unescString == null ) 321 return null; 322 323 String escString = ""; 324 String shellSpChars = "\\\""; 325 326 for(int index=0; index<unescString.length(); index++) { 327 char nextChar = unescString.charAt(index); 328 329 if( shellSpChars.indexOf(nextChar) != -1 ) 330 escString += "\\"; 331 332 escString += nextChar; 333 } 334 return escString; 335 } 336 337 340 public static String escapeXml(String s) { 341 if (s == null) return null; 342 StringBuffer sb = new StringBuffer (); 343 for(int i=0; i<s.length(); i++) { 344 char c = s.charAt(i); 345 if (c == '<') { 346 sb.append("<"); 347 } else if (c == '>') { 348 sb.append(">"); 349 } else if (c == '\'') { 350 sb.append("'"); 351 } else if (c == '&') { 352 sb.append("&"); 353 } else if (c == '"') { 354 sb.append("""); 355 } else { 356 sb.append(c); 357 } 358 } 359 return sb.toString(); 360 } 361 362 366 public static String replace(String name, char replace, String with) { 367 StringBuffer buf = new StringBuffer (); 368 int begin = 0; 369 int end; 370 int last = name.length(); 371 372 while (true) { 373 end = name.indexOf(replace, begin); 374 if (end < 0) { 375 end = last; 376 } 377 buf.append(name.substring(begin, end)); 378 if (end == last) { 379 break; 380 } 381 buf.append(with); 382 begin = end + 1; 383 } 384 385 return buf.toString(); 386 } 387 388 public static class ValidAttribute { 389 String name; 390 boolean mandatory; 391 boolean rtexprvalue; 393 public ValidAttribute (String name, boolean mandatory, 394 boolean rtexprvalue ) 395 { 396 this.name = name; 397 this.mandatory = mandatory; 398 this.rtexprvalue = rtexprvalue; 399 } 400 401 public ValidAttribute (String name, boolean mandatory) { 402 this( name, mandatory, false ); 403 } 404 405 public ValidAttribute (String name) { 406 this (name, false); 407 } 408 } 409 410 420 public static boolean booleanValue(String s) { 421 boolean b = false; 422 if (s != null) { 423 if (s.equalsIgnoreCase("yes")) { 424 b = true; 425 } else { 426 b = Boolean.valueOf(s).booleanValue(); 427 } 428 } 429 return b; 430 } 431 432 441 public static Class toClass(String type, ClassLoader loader) 442 throws ClassNotFoundException { 443 444 Class c = null; 445 int i0 = type.indexOf('['); 446 int dims = 0; 447 if (i0 > 0) { 448 for (int i = 0; i < type.length(); i++) { 450 if (type.charAt(i) == '[') 451 dims++; 452 } 453 type = type.substring(0, i0); 454 } 455 456 if ("boolean".equals(type)) 457 c = boolean.class; 458 else if ("char".equals(type)) 459 c = char.class; 460 else if ("byte".equals(type)) 461 c = byte.class; 462 else if ("short".equals(type)) 463 c = short.class; 464 else if ("int".equals(type)) 465 c = int.class; 466 else if ("long".equals(type)) 467 c = long.class; 468 else if ("float".equals(type)) 469 c = float.class; 470 else if ("double".equals(type)) 471 c = double.class; 472 else if (type.indexOf('[') < 0) 473 c = loader.loadClass(type); 474 475 if (dims == 0) 476 return c; 477 478 if (dims == 1) 479 return java.lang.reflect.Array.newInstance(c, 1).getClass(); 480 481 return java.lang.reflect.Array.newInstance(c, new int[dims]).getClass(); 483 } 484 485 493 public static String interpreterCall(boolean isTagFile, 494 String expression, 495 Class expectedType, 496 String fnmapvar, 497 boolean XmlEscape ) 498 { 499 502 String jspCtxt = null; 503 if (isTagFile) 504 jspCtxt = "this.getJspContext()"; 505 else 506 jspCtxt = "_jspx_page_context"; 507 508 513 String targetType = expectedType.getName(); 514 String primitiveConverterMethod = null; 515 if (expectedType.isPrimitive()) { 516 if (expectedType.equals(Boolean.TYPE)) { 517 targetType = Boolean .class.getName(); 518 primitiveConverterMethod = "booleanValue"; 519 } else if (expectedType.equals(Byte.TYPE)) { 520 targetType = Byte .class.getName(); 521 primitiveConverterMethod = "byteValue"; 522 } else if (expectedType.equals(Character.TYPE)) { 523 targetType = Character .class.getName(); 524 primitiveConverterMethod = "charValue"; 525 } else if (expectedType.equals(Short.TYPE)) { 526 targetType = Short .class.getName(); 527 primitiveConverterMethod = "shortValue"; 528 } else if (expectedType.equals(Integer.TYPE)) { 529 targetType = Integer .class.getName(); 530 primitiveConverterMethod = "intValue"; 531 } else if (expectedType.equals(Long.TYPE)) { 532 targetType = Long .class.getName(); 533 primitiveConverterMethod = "longValue"; 534 } else if (expectedType.equals(Float.TYPE)) { 535 targetType = Float .class.getName(); 536 primitiveConverterMethod = "floatValue"; 537 } else if (expectedType.equals(Double.TYPE)) { 538 targetType = Double .class.getName(); 539 primitiveConverterMethod = "doubleValue"; 540 } 541 } 542 543 if (primitiveConverterMethod != null) { 544 XmlEscape = false; 545 } 546 547 550 targetType = toJavaSourceType(targetType); 562 StringBuffer call = new StringBuffer ( 563 "(" + targetType + ") " 564 + "org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate" 565 + "(" + Generator.quote(expression) + ", " 566 + targetType + ".class, " 567 + "(PageContext)" + jspCtxt 568 + ", " + fnmapvar 569 + ", " + XmlEscape 570 + ")"); 571 572 575 if (primitiveConverterMethod != null) { 576 call.insert(0, "("); 577 call.append(")." + primitiveConverterMethod + "()"); 578 } 579 580 return call.toString(); 581 } 582 583 590 public static void validateExpressions(Mark where, 591 String expressions, 592 Class expectedType, 593 FunctionMapper functionMapper, 594 ErrorDispatcher err) 595 throws JasperException { 596 597 } 611 612 616 public static void resetTemporaryVariableName() { 617 tempSequenceNumber = 0; 618 } 619 620 624 public static String nextTemporaryVariableName() { 625 return Constants.TEMP_VARIABLE_NAME_PREFIX + (tempSequenceNumber++); 626 } 627 628 public static String coerceToPrimitiveBoolean(String s, 629 boolean isNamedAttribute) { 630 if (isNamedAttribute) { 631 return "org.apache.jasper.runtime.JspRuntimeLibrary.coerceToBoolean(" + s + ")"; 632 } else { 633 if (s == null || s.length() == 0) 634 return "false"; 635 else 636 return Boolean.valueOf(s).toString(); 637 } 638 } 639 640 public static String coerceToBoolean(String s, boolean isNamedAttribute) { 641 if (isNamedAttribute) { 642 return "(Boolean) org.apache.jasper.runtime.JspRuntimeLibrary.coerce(" + s + ", Boolean.class)"; 643 } else { 644 if (s == null || s.length() == 0) { 645 return "new Boolean(false)"; 646 } else { 647 return "new Boolean(" + Boolean.valueOf(s).toString() + ")"; 649 } 650 } 651 } 652 653 public static String coerceToPrimitiveByte(String s, 654 boolean isNamedAttribute) { 655 if (isNamedAttribute) { 656 return "org.apache.jasper.runtime.JspRuntimeLibrary.coerceToByte(" + s + ")"; 657 } else { 658 if (s == null || s.length() == 0) 659 return "(byte) 0"; 660 else 661 return "((byte)" + Byte.valueOf(s).toString() + ")"; 662 } 663 } 664 665 public static String coerceToByte(String s, boolean isNamedAttribute) { 666 if (isNamedAttribute) { 667 return "(Byte) org.apache.jasper.runtime.JspRuntimeLibrary.coerce(" + s + ", Byte.class)"; 668 } else { 669 if (s == null || s.length() == 0) { 670 return "new Byte((byte) 0)"; 671 } else { 672 return "new Byte((byte)" + Byte.valueOf(s).toString() + ")"; 674 } 675 } 676 } 677 678 public static String coerceToChar(String s, boolean isNamedAttribute) { 679 if (isNamedAttribute) { 680 return "org.apache.jasper.runtime.JspRuntimeLibrary.coerceToChar(" + s + ")"; 681 } else { 682 if (s == null || s.length() == 0) { 683 return "(char) 0"; 684 } else { 685 char ch = s.charAt(0); 686 return "((char) " + (int) ch + ")"; 688 } 689 } 690 } 691 692 public static String coerceToCharacter(String s, boolean isNamedAttribute) { 693 if (isNamedAttribute) { 694 return "(Character) org.apache.jasper.runtime.JspRuntimeLibrary.coerce(" + s + ", Character.class)"; 695 } else { 696 if (s == null || s.length() == 0) { 697 return "new Character((char) 0)"; 698 } else { 699 char ch = s.charAt(0); 700 return "new Character((char) " + (int) ch + ")"; 702 } 703 } 704 } 705 706 public static String coerceToPrimitiveDouble(String s, 707 boolean isNamedAttribute) { 708 if (isNamedAttribute) { 709 return "org.apache.jasper.runtime.JspRuntimeLibrary.coerceToDouble(" + s + ")"; 710 } else { 711 if (s == null || s.length() == 0) 712 return "(double) 0"; 713 else 714 return Double.valueOf(s).toString(); 715 } 716 } 717 718 public static String coerceToDouble(String s, boolean isNamedAttribute) { 719 if (isNamedAttribute) { 720 return "(Double) org.apache.jasper.runtime.JspRuntimeLibrary.coerce(" + s + ", Double.class)"; 721 } else { 722 if (s == null || s.length() == 0) { 723 return "new Double(0)"; 724 } else { 725 return "new Double(" + Double.valueOf(s).toString() + ")"; 727 } 728 } 729 } 730 731 public static String coerceToPrimitiveFloat(String s, 732 boolean isNamedAttribute) { 733 if (isNamedAttribute) { 734 return "org.apache.jasper.runtime.JspRuntimeLibrary.coerceToFloat(" + s + ")"; 735 } else { 736 if (s == null || s.length() == 0) 737 return "(float) 0"; 738 else 739 return Float.valueOf(s).toString() + "f"; 740 } 741 } 742 743 public static String coerceToFloat(String s, boolean isNamedAttribute) { 744 if (isNamedAttribute) { 745 return "(Float) org.apache.jasper.runtime.JspRuntimeLibrary.coerce(" + s + ", Float.class)"; 746 } else { 747 if (s == null || s.length() == 0) { 748 return "new Float(0)"; 749 } else { 750 return "new Float(" + Float.valueOf(s).toString() + "f)"; 752 } 753 } 754 } 755 756 public static String coerceToInt(String s, boolean isNamedAttribute) { 757 if (isNamedAttribute) { 758 return "org.apache.jasper.runtime.JspRuntimeLibrary.coerceToInt(" + s + ")"; 759 } else { 760 if (s == null || s.length() == 0) 761 return "0"; 762 else 763 return Integer.valueOf(s).toString(); 764 } 765 } 766 767 public static String coerceToInteger(String s, boolean isNamedAttribute) { 768 if (isNamedAttribute) { 769 return "(Integer) org.apache.jasper.runtime.JspRuntimeLibrary.coerce(" + s + ", Integer.class)"; 770 } else { 771 if (s == null || s.length() == 0) { 772 return "new Integer(0)"; 773 } else { 774 return "new Integer(" + Integer.valueOf(s).toString() + ")"; 776 } 777 } 778 } 779 780 public static String coerceToPrimitiveShort(String s, 781 boolean isNamedAttribute) { 782 if (isNamedAttribute) { 783 return "org.apache.jasper.runtime.JspRuntimeLibrary.coerceToShort(" + s + ")"; 784 } else { 785 if (s == null || s.length() == 0) 786 return "(short) 0"; 787 else 788 return "((short) " + Short.valueOf(s).toString() + ")"; 789 } 790 } 791 792 public static String coerceToShort(String s, boolean isNamedAttribute) { 793 if (isNamedAttribute) { 794 return "(Short) org.apache.jasper.runtime.JspRuntimeLibrary.coerce(" + s + ", Short.class)"; 795 } else { 796 if (s == null || s.length() == 0) { 797 return "new Short((short) 0)"; 798 } else { 799 return "new Short(\"" + Short.valueOf(s).toString() + "\")"; 801 } 802 } 803 } 804 805 public static String coerceToPrimitiveLong(String s, 806 boolean isNamedAttribute) { 807 if (isNamedAttribute) { 808 return "org.apache.jasper.runtime.JspRuntimeLibrary.coerceToLong(" + s + ")"; 809 } else { 810 if (s == null || s.length() == 0) 811 return "(long) 0"; 812 else 813 return Long.valueOf(s).toString() + "l"; 814 } 815 } 816 817 public static String coerceToLong(String s, boolean isNamedAttribute) { 818 if (isNamedAttribute) { 819 return "(Long) org.apache.jasper.runtime.JspRuntimeLibrary.coerce(" + s + ", Long.class)"; 820 } else { 821 if (s == null || s.length() == 0) { 822 return "new Long(0)"; 823 } else { 824 return "new Long(" + Long.valueOf(s).toString() + "l)"; 826 } 827 } 828 } 829 830 public static InputStream getInputStream(String fname, JarFile jarFile, 831 JspCompilationContext ctxt, 832 ErrorDispatcher err) 833 throws JasperException, IOException { 834 835 InputStream in = null; 836 837 if (jarFile != null) { 838 String jarEntryName = fname.substring(1, fname.length()); 839 ZipEntry jarEntry = jarFile.getEntry(jarEntryName); 840 if (jarEntry == null) { 841 err.jspError("jsp.error.file.not.found", fname); 842 } 843 in = jarFile.getInputStream(jarEntry); 844 } else { 845 in = ctxt.getResourceAsStream(fname); 846 } 847 848 if (in == null) { 849 err.jspError("jsp.error.file.not.found", fname); 850 } 851 852 return in; 853 } 854 855 865 public static String getTagHandlerClassName(String path, 866 ErrorDispatcher err) 867 throws JasperException { 868 869 String className = null; 870 int begin = 0; 871 int index; 872 873 index = path.lastIndexOf(".tag"); 874 if (index == -1) { 875 err.jspError("jsp.error.tagfile.badSuffix", path); 876 } 877 878 888 index = path.indexOf(WEB_INF_TAGS); 889 if (index != -1) { 890 className = "org.apache.jsp.tag.web."; 891 begin = index + WEB_INF_TAGS.length(); 892 } else { 893 index = path.indexOf(META_INF_TAGS); 894 if (index != -1) { 895 className = "org.apache.jsp.tag.meta."; 896 begin = index + META_INF_TAGS.length(); 897 } else { 898 err.jspError("jsp.error.tagfile.illegalPath", path); 899 } 900 } 901 902 className += makeJavaPackage(path.substring(begin)); 903 904 return className; 905 } 906 907 914 public static final String makeJavaPackage(String path) { 915 String classNameComponents[] = split(path,"/"); 916 StringBuffer legalClassNames = new StringBuffer (); 917 for (int i = 0; i < classNameComponents.length; i++) { 918 legalClassNames.append(makeJavaIdentifier(classNameComponents[i])); 919 if (i < classNameComponents.length - 1) { 920 legalClassNames.append('.'); 921 } 922 } 923 return legalClassNames.toString(); 924 } 925 926 932 private static final String [] split(String path, String pat) { 933 Vector comps = new Vector (); 934 int pos = path.indexOf(pat); 935 int start = 0; 936 while( pos >= 0 ) { 937 if(pos > start ) { 938 String comp = path.substring(start,pos); 939 comps.add(comp); 940 } 941 start = pos + pat.length(); 942 pos = path.indexOf(pat,start); 943 } 944 if( start < path.length()) { 945 comps.add(path.substring(start)); 946 } 947 String [] result = new String [comps.size()]; 948 for(int i=0; i < comps.size(); i++) { 949 result[i] = (String )comps.elementAt(i); 950 } 951 return result; 952 } 953 954 961 public static final String makeJavaIdentifier(String identifier) { 962 StringBuffer modifiedIdentifier = 963 new StringBuffer (identifier.length()); 964 if (!Character.isJavaIdentifierStart(identifier.charAt(0))) { 965 modifiedIdentifier.append('_'); 966 } 967 for (int i = 0; i < identifier.length(); i++) { 968 char ch = identifier.charAt(i); 969 if (Character.isJavaIdentifierPart(ch) && ch != '_') { 970 modifiedIdentifier.append(ch); 971 } else if (ch == '.') { 972 modifiedIdentifier.append('_'); 973 } else { 974 modifiedIdentifier.append(mangleChar(ch)); 975 } 976 } 977 if (isJavaKeyword(modifiedIdentifier.toString())) { 978 modifiedIdentifier.append('_'); 979 } 980 return modifiedIdentifier.toString(); 981 } 982 983 986 public static final String mangleChar(char ch) { 987 char[] result = new char[5]; 988 result[0] = '_'; 989 result[1] = Character.forDigit((ch >> 12) & 0xf, 16); 990 result[2] = Character.forDigit((ch >> 8) & 0xf, 16); 991 result[3] = Character.forDigit((ch >> 4) & 0xf, 16); 992 result[4] = Character.forDigit(ch & 0xf, 16); 993 return new String (result); 994 } 995 996 999 public static boolean isJavaKeyword(String key) { 1000 int i = 0; 1001 int j = javaKeywords.length; 1002 while (i < j) { 1003 int k = (i+j)/2; 1004 int result = javaKeywords[k].compareTo(key); 1005 if (result == 0) { 1006 return true; 1007 } 1008 if (result < 0) { 1009 i = k+1; 1010 } else { 1011 j = k; 1012 } 1013 } 1014 return false; 1015 } 1016 1017 1028 public static final String makeXmlJavaIdentifier(String name) { 1029 if (name.indexOf('-') >= 0) 1030 name = replace(name, '-', "$1"); 1031 if (name.indexOf('.') >= 0) 1032 name = replace(name, '.', "$2"); 1033 if (name.indexOf(':') >= 0) 1034 name = replace(name, ':', "$3"); 1035 return name; 1036 } 1037 1038 static InputStreamReader getReader(String fname, String encoding, 1039 JarFile jarFile, 1040 JspCompilationContext ctxt, 1041 ErrorDispatcher err) 1042 throws JasperException, IOException { 1043 1044 InputStreamReader reader = null; 1045 InputStream in = getInputStream(fname, jarFile, ctxt, err); 1046 1047 try { 1048 reader = new InputStreamReader (in, encoding); 1049 } catch (UnsupportedEncodingException ex) { 1050 err.jspError("jsp.error.unsupported.encoding", encoding); 1051 } 1052 1053 return reader; 1054 } 1055 1056 1066 public static String toJavaSourceTypeFromTld(String type) { 1067 if (type == null || "void".equals(type)) { 1068 return "Void.TYPE"; 1069 } 1070 return type + ".class"; 1071 } 1072 1073 1078 public static String toJavaSourceType(String type) { 1079 1080 if (type.charAt(0) != '[') { 1081 return type; 1082 } 1083 1084 int dims = 1; 1085 String t = null; 1086 for (int i = 1; i < type.length(); i++) { 1087 if (type.charAt(i) == '[') { 1088 dims++; 1089 } else { 1090 switch (type.charAt(i)) { 1091 case 'Z': t = "boolean"; break; 1092 case 'B': t = "byte"; break; 1093 case 'C': t = "char"; break; 1094 case 'D': t = "double"; break; 1095 case 'F': t = "float"; break; 1096 case 'I': t = "int"; break; 1097 case 'J': t = "long"; break; 1098 case 'S': t = "short"; break; 1099 case 'L': t = type.substring(i+1, type.indexOf(';')); break; 1100 } 1101 break; 1102 } 1103 } 1104 StringBuffer resultType = new StringBuffer (t); 1105 for (; dims > 0; dims--) { 1106 resultType.append("[]"); 1107 } 1108 return resultType.toString(); 1109 } 1110 1111 1118 public static String getCanonicalName(Class c) { 1119 1120 String binaryName = c.getName(); 1121 c = c.getDeclaringClass(); 1122 1123 if (c == null) { 1124 return binaryName; 1125 } 1126 1127 StringBuffer buf = new StringBuffer (binaryName); 1128 do { 1129 buf.setCharAt(c.getName().length(), '.'); 1130 c = c.getDeclaringClass(); 1131 } while ( c != null); 1132 1133 return buf.toString(); 1134 } 1135} | Popular Tags |