KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > utils > JDK13Utils


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.utils;
14
15 import java.io.UnsupportedEncodingException JavaDoc;
16 import java.lang.reflect.InvocationTargetException JavaDoc;
17 import java.lang.reflect.Method JavaDoc;
18 import java.net.URLEncoder JavaDoc;
19 import java.util.Locale JavaDoc;
20
21 import javax.servlet.ServletException JavaDoc;
22 import javax.servlet.jsp.JspException JavaDoc;
23
24 /**
25  * use 1.4 functions in a 1.3 compatible way
26  */

27 public class JDK13Utils {
28   private JDK13Utils() {
29   }
30
31   private static final Class JavaDoc[] encodeParamTypes = new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class};
32
33   /**
34    * replacement for URLEncoder.encode(String s, String encoding)
35    */

36   public static String JavaDoc urlEncode(String JavaDoc s, String JavaDoc enc) throws UnsupportedEncodingException JavaDoc {
37     try {
38       // try 1.4
39
Method JavaDoc m = URLEncoder JavaDoc.class.getMethod("encode", encodeParamTypes);
40       return (String JavaDoc) m.invoke(null, new String JavaDoc[]{s, enc});
41     } catch (NoSuchMethodException JavaDoc e) {
42       // its 1.3
43
} catch (SecurityException JavaDoc e) {
44       throw new SoftException(e);
45     } catch (IllegalArgumentException JavaDoc e) {
46       throw new SoftException(e);
47     } catch (IllegalAccessException JavaDoc e) {
48       throw new SoftException(e);
49     } catch (InvocationTargetException JavaDoc e) {
50       Throwable JavaDoc x = e.getTargetException();
51       if (x instanceof UnsupportedEncodingException JavaDoc)
52         throw (UnsupportedEncodingException JavaDoc) x;
53       throw new SoftException(e);
54     }
55     return URLEncoder.encode(s);
56   }
57
58   /**
59    * replacement for new Locale(String lang)
60    */

61   public static Locale JavaDoc getLocale(String JavaDoc lang) {
62     return new Locale JavaDoc(lang, lang.toUpperCase());
63   }
64
65   /**
66    * returns the cause exception or null. Postcondition: returnvalue != e.
67    * Usage pattern:
68    * <pre>
69    * Throwable t = ...
70    * while (t != null) {
71    * t.printStackTrace();
72    * t = JDK13Utils.getCause(t);
73    * }
74    * </pre>
75    */

76   public static Throwable JavaDoc getCause(Throwable JavaDoc e) {
77     if (e == null)
78       return null;
79     Throwable JavaDoc prev = e;
80     if (e instanceof JspException JavaDoc)
81       e = ((JspException JavaDoc) e).getRootCause();
82     else if (e instanceof ServletException JavaDoc)
83       e = ((ServletException JavaDoc) e).getRootCause();
84     else {
85       // call e.getCause() in a JDK 1.3 compatible way
86
try {
87         Method JavaDoc m = e.getClass().getMethod("getCause", new Class JavaDoc[0]);
88         e = (Throwable JavaDoc) m.invoke(e, new Object JavaDoc[0]);
89       } catch (Exception JavaDoc ex) {
90       }
91     }
92     if (e == prev)
93       return null;
94     return e;
95   }
96 }
Popular Tags