1 13 package com.tonbeller.wcf.utils; 14 15 import java.io.UnsupportedEncodingException ; 16 import java.lang.reflect.InvocationTargetException ; 17 import java.lang.reflect.Method ; 18 import java.net.URLEncoder ; 19 import java.util.Locale ; 20 21 import javax.servlet.ServletException ; 22 import javax.servlet.jsp.JspException ; 23 24 27 public class JDK13Utils { 28 private JDK13Utils() { 29 } 30 31 private static final Class [] encodeParamTypes = new Class []{String .class, String .class}; 32 33 36 public static String urlEncode(String s, String enc) throws UnsupportedEncodingException { 37 try { 38 Method m = URLEncoder .class.getMethod("encode", encodeParamTypes); 40 return (String ) m.invoke(null, new String []{s, enc}); 41 } catch (NoSuchMethodException e) { 42 } catch (SecurityException e) { 44 throw new SoftException(e); 45 } catch (IllegalArgumentException e) { 46 throw new SoftException(e); 47 } catch (IllegalAccessException e) { 48 throw new SoftException(e); 49 } catch (InvocationTargetException e) { 50 Throwable x = e.getTargetException(); 51 if (x instanceof UnsupportedEncodingException ) 52 throw (UnsupportedEncodingException ) x; 53 throw new SoftException(e); 54 } 55 return URLEncoder.encode(s); 56 } 57 58 61 public static Locale getLocale(String lang) { 62 return new Locale (lang, lang.toUpperCase()); 63 } 64 65 76 public static Throwable getCause(Throwable e) { 77 if (e == null) 78 return null; 79 Throwable prev = e; 80 if (e instanceof JspException ) 81 e = ((JspException ) e).getRootCause(); 82 else if (e instanceof ServletException ) 83 e = ((ServletException ) e).getRootCause(); 84 else { 85 try { 87 Method m = e.getClass().getMethod("getCause", new Class [0]); 88 e = (Throwable ) m.invoke(e, new Object [0]); 89 } catch (Exception ex) { 90 } 91 } 92 if (e == prev) 93 return null; 94 return e; 95 } 96 } | Popular Tags |