1 17 18 package org.apache.jasper.runtime; 19 20 import java.beans.PropertyEditor ; 21 import java.beans.PropertyEditorManager ; 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.io.OutputStreamWriter ; 25 import java.lang.reflect.Method ; 26 import java.security.AccessController ; 27 import java.security.PrivilegedActionException ; 28 import java.security.PrivilegedExceptionAction ; 29 import java.util.Enumeration ; 30 31 import javax.servlet.RequestDispatcher ; 32 import javax.servlet.ServletException ; 33 import javax.servlet.ServletRequest ; 34 import javax.servlet.ServletResponse ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.jsp.JspWriter ; 37 import javax.servlet.jsp.PageContext ; 38 import javax.servlet.jsp.tagext.BodyContent ; 39 40 import org.apache.jasper.JasperException; 41 import org.apache.jasper.compiler.Localizer; 42 43 55 public class JspRuntimeLibrary { 56 57 private static final String SERVLET_EXCEPTION 58 = "javax.servlet.error.exception"; 59 private static final String JSP_EXCEPTION 60 = "javax.servlet.jsp.jspException"; 61 62 protected static class PrivilegedIntrospectHelper 63 implements PrivilegedExceptionAction { 64 65 private Object bean; 66 private String prop; 67 private String value; 68 private ServletRequest request; 69 private String param; 70 private boolean ignoreMethodNF; 71 72 PrivilegedIntrospectHelper(Object bean, String prop, 73 String value, ServletRequest request, 74 String param, boolean ignoreMethodNF) 75 { 76 this.bean = bean; 77 this.prop = prop; 78 this.value = value; 79 this.request = request; 80 this.param = param; 81 this.ignoreMethodNF = ignoreMethodNF; 82 } 83 84 public Object run() throws JasperException { 85 internalIntrospecthelper( 86 bean,prop,value,request,param,ignoreMethodNF); 87 return null; 88 } 89 } 90 91 100 public static Throwable getThrowable(ServletRequest request) { 101 Throwable error = (Throwable ) request.getAttribute(SERVLET_EXCEPTION); 102 if (error == null) { 103 error = (Throwable ) request.getAttribute(JSP_EXCEPTION); 104 if (error != null) { 105 112 request.setAttribute(SERVLET_EXCEPTION, error); 113 } 114 } 115 116 return error; 117 } 118 119 public static boolean coerceToBoolean(String s) { 120 if (s == null || s.length() == 0) 121 return false; 122 else 123 return Boolean.valueOf(s).booleanValue(); 124 } 125 126 public static byte coerceToByte(String s) { 127 if (s == null || s.length() == 0) 128 return (byte) 0; 129 else 130 return Byte.valueOf(s).byteValue(); 131 } 132 133 public static char coerceToChar(String s) { 134 if (s == null || s.length() == 0) { 135 return (char) 0; 136 } else { 137 return (char)(int) s.charAt(0); 139 } 140 } 141 142 public static double coerceToDouble(String s) { 143 if (s == null || s.length() == 0) 144 return (double) 0; 145 else 146 return Double.valueOf(s).doubleValue(); 147 } 148 149 public static float coerceToFloat(String s) { 150 if (s == null || s.length() == 0) 151 return (float) 0; 152 else 153 return Float.valueOf(s).floatValue(); 154 } 155 156 public static int coerceToInt(String s) { 157 if (s == null || s.length() == 0) 158 return 0; 159 else 160 return Integer.valueOf(s).intValue(); 161 } 162 163 public static short coerceToShort(String s) { 164 if (s == null || s.length() == 0) 165 return (short) 0; 166 else 167 return Short.valueOf(s).shortValue(); 168 } 169 170 public static long coerceToLong(String s) { 171 if (s == null || s.length() == 0) 172 return (long) 0; 173 else 174 return Long.valueOf(s).longValue(); 175 } 176 177 public static Object coerce(String s, Class target) { 178 179 boolean isNullOrEmpty = (s == null || s.length() == 0); 180 181 if (target == Boolean .class) { 182 if (isNullOrEmpty) { 183 s = "false"; 184 } 185 return new Boolean (s); 186 } else if (target == Byte .class) { 187 if (isNullOrEmpty) 188 return new Byte ((byte) 0); 189 else 190 return new Byte (s); 191 } else if (target == Character .class) { 192 if (isNullOrEmpty) 193 return new Character ((char) 0); 194 else 195 return new Character (s.charAt(0)); 196 } else if (target == Double .class) { 197 if (isNullOrEmpty) 198 return new Double (0); 199 else 200 return new Double (s); 201 } else if (target == Float .class) { 202 if (isNullOrEmpty) 203 return new Float (0); 204 else 205 return new Float (s); 206 } else if (target == Integer .class) { 207 if (isNullOrEmpty) 208 return new Integer (0); 209 else 210 return new Integer (s); 211 } else if (target == Short .class) { 212 if (isNullOrEmpty) 213 return new Short ((short) 0); 214 else 215 return new Short (s); 216 } else if (target == Long .class) { 217 if (isNullOrEmpty) 218 return new Long (0); 219 else 220 return new Long (s); 221 } else { 222 return null; 223 } 224 } 225 226 public static Object convert(String propertyName, String s, Class t, 228 Class propertyEditorClass) 229 throws JasperException 230 { 231 try { 232 if (s == null) { 233 if (t.equals(Boolean .class) || t.equals(Boolean.TYPE)) 234 s = "false"; 235 else 236 return null; 237 } 238 if (propertyEditorClass != null) { 239 return getValueFromBeanInfoPropertyEditor( 240 t, propertyName, s, propertyEditorClass); 241 } else if ( t.equals(Boolean .class) || t.equals(Boolean.TYPE) ) { 242 if (s.equalsIgnoreCase("on") || s.equalsIgnoreCase("true")) 243 s = "true"; 244 else 245 s = "false"; 246 return new Boolean (s); 247 } else if ( t.equals(Byte .class) || t.equals(Byte.TYPE) ) { 248 return new Byte (s); 249 } else if (t.equals(Character .class) || t.equals(Character.TYPE)) { 250 return s.length() > 0 ? new Character (s.charAt(0)) : null; 251 } else if ( t.equals(Short .class) || t.equals(Short.TYPE) ) { 252 return new Short (s); 253 } else if ( t.equals(Integer .class) || t.equals(Integer.TYPE) ) { 254 return new Integer (s); 255 } else if ( t.equals(Float .class) || t.equals(Float.TYPE) ) { 256 return new Float (s); 257 } else if ( t.equals(Long .class) || t.equals(Long.TYPE) ) { 258 return new Long (s); 259 } else if ( t.equals(Double .class) || t.equals(Double.TYPE) ) { 260 return new Double (s); 261 } else if ( t.equals(String .class) ) { 262 return s; 263 } else if ( t.equals(java.io.File .class) ) { 264 return new java.io.File (s); 265 } else if (t.getName().equals("java.lang.Object")) { 266 return new Object [] {s}; 267 } else { 268 return getValueFromPropertyEditorManager( 269 t, propertyName, s); 270 } 271 } catch (Exception ex) { 272 throw new JasperException(ex); 273 } 274 } 275 277 public static void introspect(Object bean, ServletRequest request) 279 throws JasperException 280 { 281 Enumeration e = request.getParameterNames(); 282 while ( e.hasMoreElements() ) { 283 String name = (String ) e.nextElement(); 284 String value = request.getParameter(name); 285 introspecthelper(bean, name, value, request, name, true); 286 } 287 } 288 290 public static void introspecthelper(Object bean, String prop, 292 String value, ServletRequest request, 293 String param, boolean ignoreMethodNF) 294 throws JasperException 295 { 296 if( System.getSecurityManager() != null ) { 297 try { 298 PrivilegedIntrospectHelper dp = 299 new PrivilegedIntrospectHelper( 300 bean,prop,value,request,param,ignoreMethodNF); 301 AccessController.doPrivileged(dp); 302 } catch( PrivilegedActionException pe) { 303 Exception e = pe.getException(); 304 throw (JasperException)e; 305 } 306 } else { 307 internalIntrospecthelper( 308 bean,prop,value,request,param,ignoreMethodNF); 309 } 310 } 311 312 private static void internalIntrospecthelper(Object bean, String prop, 313 String value, ServletRequest request, 314 String param, boolean ignoreMethodNF) 315 throws JasperException 316 { 317 Method method = null; 318 Class type = null; 319 Class propertyEditorClass = null; 320 try { 321 java.beans.BeanInfo info 322 = java.beans.Introspector.getBeanInfo(bean.getClass()); 323 if ( info != null ) { 324 java.beans.PropertyDescriptor pd[] 325 = info.getPropertyDescriptors(); 326 for (int i = 0 ; i < pd.length ; i++) { 327 if ( pd[i].getName().equals(prop) ) { 328 method = pd[i].getWriteMethod(); 329 type = pd[i].getPropertyType(); 330 propertyEditorClass = pd[i].getPropertyEditorClass(); 331 break; 332 } 333 } 334 } 335 if ( method != null ) { 336 if (type.isArray()) { 337 if (request == null) { 338 throw new JasperException( 339 Localizer.getMessage("jsp.error.beans.setproperty.noindexset")); 340 } 341 Class t = type.getComponentType(); 342 String [] values = request.getParameterValues(param); 343 if(values == null) return; 345 if(t.equals(String .class)) { 346 method.invoke(bean, new Object [] { values }); 347 } else { 348 Object tmpval = null; 349 createTypedArray (prop, bean, method, values, t, 350 propertyEditorClass); 351 } 352 } else { 353 if(value == null || (param != null && value.equals(""))) return; 354 Object oval = convert(prop, value, type, propertyEditorClass); 355 if ( oval != null ) 356 method.invoke(bean, new Object [] { oval }); 357 } 358 } 359 } catch (Exception ex) { 360 throw new JasperException(ex); 361 } 362 if (!ignoreMethodNF && (method == null)) { 363 if (type == null) { 364 throw new JasperException( 365 Localizer.getMessage("jsp.error.beans.noproperty", 366 prop, 367 bean.getClass().getName())); 368 } else { 369 throw new JasperException( 370 Localizer.getMessage("jsp.error.beans.nomethod.setproperty", 371 prop, 372 type.getName(), 373 bean.getClass().getName())); 374 } 375 } 376 } 377 379 public static String toString(Object o) { 384 return String.valueOf(o); 385 } 386 387 public static String toString(byte b) { 388 return new Byte (b).toString(); 389 } 390 391 public static String toString(boolean b) { 392 return new Boolean (b).toString(); 393 } 394 395 public static String toString(short s) { 396 return new Short (s).toString(); 397 } 398 399 public static String toString(int i) { 400 return new Integer (i).toString(); 401 } 402 403 public static String toString(float f) { 404 return new Float (f).toString(); 405 } 406 407 public static String toString(long l) { 408 return new Long (l).toString(); 409 } 410 411 public static String toString(double d) { 412 return new Double (d).toString(); 413 } 414 415 public static String toString(char c) { 416 return new Character (c).toString(); 417 } 418 420 421 426 public static void createTypedArray(String propertyName, 427 Object bean, 428 Method method, 429 String [] values, 430 Class t, 431 Class propertyEditorClass) 432 throws JasperException { 433 434 try { 435 if (propertyEditorClass != null) { 436 Object [] tmpval = new Integer [values.length]; 437 for (int i=0; i<values.length; i++) { 438 tmpval[i] = getValueFromBeanInfoPropertyEditor( 439 t, propertyName, values[i], propertyEditorClass); 440 } 441 method.invoke (bean, new Object [] {tmpval}); 442 } else if (t.equals(Integer .class)) { 443 Integer []tmpval = new Integer [values.length]; 444 for (int i = 0 ; i < values.length; i++) 445 tmpval[i] = new Integer (values[i]); 446 method.invoke (bean, new Object [] {tmpval}); 447 } else if (t.equals(Byte .class)) { 448 Byte [] tmpval = new Byte [values.length]; 449 for (int i = 0 ; i < values.length; i++) 450 tmpval[i] = new Byte (values[i]); 451 method.invoke (bean, new Object [] {tmpval}); 452 } else if (t.equals(Boolean .class)) { 453 Boolean [] tmpval = new Boolean [values.length]; 454 for (int i = 0 ; i < values.length; i++) 455 tmpval[i] = new Boolean (values[i]); 456 method.invoke (bean, new Object [] {tmpval}); 457 } else if (t.equals(Short .class)) { 458 Short [] tmpval = new Short [values.length]; 459 for (int i = 0 ; i < values.length; i++) 460 tmpval[i] = new Short (values[i]); 461 method.invoke (bean, new Object [] {tmpval}); 462 } else if (t.equals(Long .class)) { 463 Long [] tmpval = new Long [values.length]; 464 for (int i = 0 ; i < values.length; i++) 465 tmpval[i] = new Long (values[i]); 466 method.invoke (bean, new Object [] {tmpval}); 467 } else if (t.equals(Double .class)) { 468 Double [] tmpval = new Double [values.length]; 469 for (int i = 0 ; i < values.length; i++) 470 tmpval[i] = new Double (values[i]); 471 method.invoke (bean, new Object [] {tmpval}); 472 } else if (t.equals(Float .class)) { 473 Float [] tmpval = new Float [values.length]; 474 for (int i = 0 ; i < values.length; i++) 475 tmpval[i] = new Float (values[i]); 476 method.invoke (bean, new Object [] {tmpval}); 477 } else if (t.equals(Character .class)) { 478 Character [] tmpval = new Character [values.length]; 479 for (int i = 0 ; i < values.length; i++) 480 tmpval[i] = new Character (values[i].charAt(0)); 481 method.invoke (bean, new Object [] {tmpval}); 482 } else if (t.equals(int.class)) { 483 int []tmpval = new int[values.length]; 484 for (int i = 0 ; i < values.length; i++) 485 tmpval[i] = Integer.parseInt (values[i]); 486 method.invoke (bean, new Object [] {tmpval}); 487 } else if (t.equals(byte.class)) { 488 byte[] tmpval = new byte[values.length]; 489 for (int i = 0 ; i < values.length; i++) 490 tmpval[i] = Byte.parseByte (values[i]); 491 method.invoke (bean, new Object [] {tmpval}); 492 } else if (t.equals(boolean.class)) { 493 boolean[] tmpval = new boolean[values.length]; 494 for (int i = 0 ; i < values.length; i++) 495 tmpval[i] = (Boolean.valueOf(values[i])).booleanValue(); 496 method.invoke (bean, new Object [] {tmpval}); 497 } else if (t.equals(short.class)) { 498 short[] tmpval = new short[values.length]; 499 for (int i = 0 ; i < values.length; i++) 500 tmpval[i] = Short.parseShort (values[i]); 501 method.invoke (bean, new Object [] {tmpval}); 502 } else if (t.equals(long.class)) { 503 long[] tmpval = new long[values.length]; 504 for (int i = 0 ; i < values.length; i++) 505 tmpval[i] = Long.parseLong (values[i]); 506 method.invoke (bean, new Object [] {tmpval}); 507 } else if (t.equals(double.class)) { 508 double[] tmpval = new double[values.length]; 509 for (int i = 0 ; i < values.length; i++) 510 tmpval[i] = Double.valueOf(values[i]).doubleValue(); 511 method.invoke (bean, new Object [] {tmpval}); 512 } else if (t.equals(float.class)) { 513 float[] tmpval = new float[values.length]; 514 for (int i = 0 ; i < values.length; i++) 515 tmpval[i] = Float.valueOf(values[i]).floatValue(); 516 method.invoke (bean, new Object [] {tmpval}); 517 } else if (t.equals(char.class)) { 518 char[] tmpval = new char[values.length]; 519 for (int i = 0 ; i < values.length; i++) 520 tmpval[i] = values[i].charAt(0); 521 method.invoke (bean, new Object [] {tmpval}); 522 } else { 523 Object [] tmpval = new Integer [values.length]; 524 for (int i=0; i<values.length; i++) { 525 tmpval[i] = 526 getValueFromPropertyEditorManager( 527 t, propertyName, values[i]); 528 } 529 method.invoke (bean, new Object [] {tmpval}); 530 } 531 } catch (Exception ex) { 532 throw new JasperException ("error in invoking method", ex); 533 } 534 } 535 536 541 542 public static String escapeQueryString(String unescString) { 543 if ( unescString == null ) 544 return null; 545 546 String escString = ""; 547 String shellSpChars = "&;`'\"|*?~<>^()[]{}$\\\n"; 548 549 for(int index=0; index<unescString.length(); index++) { 550 char nextChar = unescString.charAt(index); 551 552 if( shellSpChars.indexOf(nextChar) != -1 ) 553 escString += "\\"; 554 555 escString += nextChar; 556 } 557 return escString; 558 } 559 560 565 566 public static String decode(String encoded) { 567 if (encoded == null) return null; 569 if (encoded.indexOf('%') == -1 && encoded.indexOf('+') == -1) 570 return encoded; 571 572 byte holdbuffer[] = new byte[encoded.length()]; 574 575 char holdchar; 576 int bufcount = 0; 577 578 for (int count = 0; count < encoded.length(); count++) { 579 char cur = encoded.charAt(count); 580 if (cur == '%') { 581 holdbuffer[bufcount++] = 582 (byte)Integer.parseInt(encoded.substring(count+1,count+3),16); 583 if (count + 2 >= encoded.length()) 584 count = encoded.length(); 585 else 586 count += 2; 587 } else if (cur == '+') { 588 holdbuffer[bufcount++] = (byte) ' '; 589 } else { 590 holdbuffer[bufcount++] = (byte) cur; 591 } 592 } 593 return new String (holdbuffer,0,bufcount); 596 } 597 598 public static Object handleGetProperty(Object o, String prop) 600 throws JasperException { 601 if (o == null) { 602 throw new JasperException( 603 Localizer.getMessage("jsp.error.beans.nullbean")); 604 } 605 Object value = null; 606 try { 607 Method method = getReadMethod(o.getClass(), prop); 608 value = method.invoke(o, (Object []) null); 609 } catch (Exception ex) { 610 throw new JasperException (ex); 611 } 612 return value; 613 } 614 616 638 public static void handleSetPropertyExpression(Object bean, 639 String prop, String expression, PageContext pageContext, 640 ProtectedFunctionMapper functionMapper ) 641 throws JasperException 642 { 643 try { 644 Method method = getWriteMethod(bean.getClass(), prop); 645 method.invoke(bean, new Object [] { 646 PageContextImpl.proprietaryEvaluate( 647 expression, 648 method.getParameterTypes()[0], 649 pageContext, 650 functionMapper, 651 false ) 652 }); 653 } catch (Exception ex) { 654 throw new JasperException(ex); 655 } 656 } 657 658 public static void handleSetProperty(Object bean, String prop, 659 Object value) 660 throws JasperException 661 { 662 try { 663 Method method = getWriteMethod(bean.getClass(), prop); 664 method.invoke(bean, new Object [] { value }); 665 } catch (Exception ex) { 666 throw new JasperException(ex); 667 } 668 } 669 670 public static void handleSetProperty(Object bean, String prop, 671 int value) 672 throws JasperException 673 { 674 try { 675 Method method = getWriteMethod(bean.getClass(), prop); 676 method.invoke(bean, new Object [] { new Integer (value) }); 677 } catch (Exception ex) { 678 throw new JasperException(ex); 679 } 680 } 681 682 public static void handleSetProperty(Object bean, String prop, 683 short value) 684 throws JasperException 685 { 686 try { 687 Method method = getWriteMethod(bean.getClass(), prop); 688 method.invoke(bean, new Object [] { new Short (value) }); 689 } catch (Exception ex) { 690 throw new JasperException(ex); 691 } 692 } 693 694 public static void handleSetProperty(Object bean, String prop, 695 long value) 696 throws JasperException 697 { 698 try { 699 Method method = getWriteMethod(bean.getClass(), prop); 700 method.invoke(bean, new Object [] { new Long (value) }); 701 } catch (Exception ex) { 702 throw new JasperException(ex); 703 } 704 } 705 706 public static void handleSetProperty(Object bean, String prop, 707 double value) 708 throws JasperException 709 { 710 try { 711 Method method = getWriteMethod(bean.getClass(), prop); 712 method.invoke(bean, new Object [] { new Double (value) }); 713 } catch (Exception ex) { 714 throw new JasperException(ex); 715 } 716 } 717 718 public static void handleSetProperty(Object bean, String prop, 719 float value) 720 throws JasperException 721 { 722 try { 723 Method method = getWriteMethod(bean.getClass(), prop); 724 method.invoke(bean, new Object [] { new Float (value) }); 725 } catch (Exception ex) { 726 throw new JasperException(ex); 727 } 728 } 729 730 public static void handleSetProperty(Object bean, String prop, 731 char value) 732 throws JasperException 733 { 734 try { 735 Method method = getWriteMethod(bean.getClass(), prop); 736 method.invoke(bean, new Object [] { new Character (value) }); 737 } catch (Exception ex) { 738 throw new JasperException(ex); 739 } 740 } 741 742 public static void handleSetProperty(Object bean, String prop, 743 byte value) 744 throws JasperException 745 { 746 try { 747 Method method = getWriteMethod(bean.getClass(), prop); 748 method.invoke(bean, new Object [] { new Byte (value) }); 749 } catch (Exception ex) { 750 throw new JasperException(ex); 751 } 752 } 753 754 public static void handleSetProperty(Object bean, String prop, 755 boolean value) 756 throws JasperException 757 { 758 try { 759 Method method = getWriteMethod(bean.getClass(), prop); 760 method.invoke(bean, new Object [] { new Boolean (value) }); 761 } catch (Exception ex) { 762 throw new JasperException(ex); 763 } 764 } 765 766 public static Method getWriteMethod(Class beanClass, String prop) 767 throws JasperException { 768 Method method = null; 769 Class type = null; 770 try { 771 java.beans.BeanInfo info 772 = java.beans.Introspector.getBeanInfo(beanClass); 773 if ( info != null ) { 774 java.beans.PropertyDescriptor pd[] 775 = info.getPropertyDescriptors(); 776 for (int i = 0 ; i < pd.length ; i++) { 777 if ( pd[i].getName().equals(prop) ) { 778 method = pd[i].getWriteMethod(); 779 type = pd[i].getPropertyType(); 780 break; 781 } 782 } 783 } else { 784 throw new JasperException( 786 Localizer.getMessage("jsp.error.beans.nobeaninfo", 787 beanClass.getName())); 788 } 789 } catch (Exception ex) { 790 throw new JasperException (ex); 791 } 792 if (method == null) { 793 if (type == null) { 794 throw new JasperException( 795 Localizer.getMessage("jsp.error.beans.noproperty", 796 prop, 797 beanClass.getName())); 798 } else { 799 throw new JasperException( 800 Localizer.getMessage("jsp.error.beans.nomethod.setproperty", 801 prop, 802 type.getName(), 803 beanClass.getName())); 804 } 805 } 806 return method; 807 } 808 809 public static Method getReadMethod(Class beanClass, String prop) 810 throws JasperException { 811 812 Method method = null; 813 Class type = null; 814 try { 815 java.beans.BeanInfo info 816 = java.beans.Introspector.getBeanInfo(beanClass); 817 if ( info != null ) { 818 java.beans.PropertyDescriptor pd[] 819 = info.getPropertyDescriptors(); 820 for (int i = 0 ; i < pd.length ; i++) { 821 if ( pd[i].getName().equals(prop) ) { 822 method = pd[i].getReadMethod(); 823 type = pd[i].getPropertyType(); 824 break; 825 } 826 } 827 } else { 828 throw new JasperException( 830 Localizer.getMessage("jsp.error.beans.nobeaninfo", 831 beanClass.getName())); 832 } 833 } catch (Exception ex) { 834 throw new JasperException (ex); 835 } 836 if (method == null) { 837 if (type == null) { 838 throw new JasperException( 839 Localizer.getMessage("jsp.error.beans.noproperty", prop, 840 beanClass.getName())); 841 } else { 842 throw new JasperException( 843 Localizer.getMessage("jsp.error.beans.nomethod", prop, 844 beanClass.getName())); 845 } 846 } 847 848 return method; 849 } 850 851 854 public static Object getValueFromBeanInfoPropertyEditor( 855 Class attrClass, String attrName, String attrValue, 856 Class propertyEditorClass) 857 throws JasperException 858 { 859 try { 860 PropertyEditor pe = (PropertyEditor )propertyEditorClass.newInstance(); 861 pe.setAsText(attrValue); 862 return pe.getValue(); 863 } catch (Exception ex) { 864 throw new JasperException( 865 Localizer.getMessage("jsp.error.beans.property.conversion", 866 attrValue, attrClass.getName(), attrName, 867 ex.getMessage())); 868 } 869 } 870 871 public static Object getValueFromPropertyEditorManager( 872 Class attrClass, String attrName, String attrValue) 873 throws JasperException 874 { 875 try { 876 PropertyEditor propEditor = 877 PropertyEditorManager.findEditor(attrClass); 878 if (propEditor != null) { 879 propEditor.setAsText(attrValue); 880 return propEditor.getValue(); 881 } else { 882 throw new IllegalArgumentException ( 883 Localizer.getMessage("jsp.error.beans.propertyeditor.notregistered")); 884 } 885 } catch (IllegalArgumentException ex) { 886 throw new JasperException( 887 Localizer.getMessage("jsp.error.beans.property.conversion", 888 attrValue, attrClass.getName(), attrName, 889 ex.getMessage())); 890 } 891 } 892 893 894 898 899 906 public static String getContextRelativePath(ServletRequest request, 907 String relativePath) { 908 909 if (relativePath.startsWith("/")) 910 return (relativePath); 911 if (!(request instanceof HttpServletRequest )) 912 return (relativePath); 913 HttpServletRequest hrequest = (HttpServletRequest ) request; 914 String uri = (String ) 915 request.getAttribute("javax.servlet.include.servlet_path"); 916 if (uri != null) { 917 String pathInfo = (String ) 918 request.getAttribute("javax.servlet.include.path_info"); 919 if (pathInfo == null) { 920 if (uri.lastIndexOf('/') >= 0) 921 uri = uri.substring(0, uri.lastIndexOf('/')); 922 } 923 } 924 else { 925 uri = hrequest.getServletPath(); 926 if (uri.lastIndexOf('/') >= 0) 927 uri = uri.substring(0, uri.lastIndexOf('/')); 928 } 929 return uri + '/' + relativePath; 930 931 } 932 933 934 947 public static void include(ServletRequest request, 948 ServletResponse response, 949 String relativePath, 950 JspWriter out, 951 boolean flush) 952 throws IOException , ServletException { 953 954 if (flush && !(out instanceof BodyContent )) 955 out.flush(); 956 957 964 String resourcePath = getContextRelativePath(request, relativePath); 965 RequestDispatcher rd = request.getRequestDispatcher(resourcePath); 966 967 rd.include(request, 968 new ServletResponseWrapperInclude(response, out)); 969 970 } 971 972 981 public static String URLEncode(String s, String enc) { 982 983 if (s == null) { 984 return "null"; 985 } 986 987 if (enc == null) { 988 enc = "ISO-8859-1"; } 990 991 StringBuffer out = new StringBuffer (s.length()); 992 ByteArrayOutputStream buf = new ByteArrayOutputStream (); 993 OutputStreamWriter writer = null; 994 try { 995 writer = new OutputStreamWriter (buf, enc); 996 } catch (java.io.UnsupportedEncodingException ex) { 997 writer = new OutputStreamWriter (buf); 999 } 1000 1001 for (int i = 0; i < s.length(); i++) { 1002 int c = s.charAt(i); 1003 if (c == ' ') { 1004 out.append('+'); 1005 } else if (isSafeChar(c)) { 1006 out.append((char)c); 1007 } else { 1008 try { 1010 writer.write(c); 1011 writer.flush(); 1012 } catch(IOException e) { 1013 buf.reset(); 1014 continue; 1015 } 1016 byte[] ba = buf.toByteArray(); 1017 for (int j = 0; j < ba.length; j++) { 1018 out.append('%'); 1019 out.append(Character.forDigit((ba[j]>>4) & 0xf, 16)); 1021 out.append(Character.forDigit(ba[j] & 0xf, 16)); 1022 } 1023 buf.reset(); 1024 } 1025 } 1026 return out.toString(); 1027 } 1028 1029 private static boolean isSafeChar(int c) { 1030 if (c >= 'a' && c <= 'z') { 1031 return true; 1032 } 1033 if (c >= 'A' && c <= 'Z') { 1034 return true; 1035 } 1036 if (c >= '0' && c <= '9') { 1037 return true; 1038 } 1039 if (c == '-' || c == '_' || c == '.' || c == '!' || 1040 c == '~' || c == '*' || c == '\'' || c == '(' || c == ')') { 1041 return true; 1042 } 1043 return false; 1044 } 1045 1046} 1047 | Popular Tags |