View Javadoc

1   /***
2    * Licensed under the Artistic License; you may not use this file
3    * except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    *      http://displaytag.sourceforge.net/license.html
7    *
8    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9    * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10   * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11   */
12  package org.displaytag.util;
13  
14  import java.lang.reflect.Method;
15  import java.net.URLEncoder;
16  
17  import org.displaytag.Messages;
18  
19  
20  /***
21   * Wrapper class to choose between the java 1.4 implementation of UrlEncoder.encode(), when available, or the java 1.3
22   * implementation.
23   * @author Fabrizio Giustina
24   * @version $Revision: 1.6 $ ($Author: fgiust $)
25   */
26  public final class CompatibleUrlEncoder
27  {
28  
29      /***
30       * j2se 1.4 encode method, used by reflection if available.
31       */
32      private static Method encodeMethod14;
33  
34      /***
35       * don't instantiate.
36       */
37      private CompatibleUrlEncoder()
38      {
39          // unused
40      }
41  
42      static
43      {
44          // URLEncoder.encode(String) has been deprecated in J2SE 1.4.
45          // Take advantage of the new method URLEncoder.encode(String, enc) if J2SE 1.4 is used.
46          try
47          {
48              Class urlEncoderClass = Class.forName("java.net.URLEncoder"); //$NON-NLS-1$
49              encodeMethod14 = urlEncoderClass.getMethod("encode", new Class[]{String.class, String.class}); //$NON-NLS-1$
50          }
51          catch (Throwable ex)
52          {
53              // encodeMethod14 will be null if exception
54          }
55      }
56  
57      /***
58       * Called encodeUrl using j2se 1.4 version by reflection if available, or backward compatible version.
59       * @param url url to be encoded
60       * @param encoding encoding to use for jse 1.4
61       * @return encoded url.
62       */
63      public static String encode(String url, String encoding)
64      {
65          if (encodeMethod14 != null)
66          {
67              Object[] methodArgs = new Object[2];
68              methodArgs[0] = url;
69  
70              if (encoding != null)
71              {
72                  methodArgs[1] = encoding;
73              }
74              else
75              {
76                  methodArgs[1] = "UTF8"; //$NON-NLS-1$
77              }
78  
79              try
80              {
81                  return (String) encodeMethod14.invoke(null, methodArgs);
82              }
83              catch (Throwable ex)
84              {
85                  throw new RuntimeException(Messages.getString("CompatibleUrlEncoder.errorinvoking", //$NON-NLS-1$
86                      new Object[]{encoding, ex.getMessage()}));
87              }
88          }
89  
90          // must use J2SE 1.3 version
91          return URLEncoder.encode(url);
92  
93      }
94  }