/Users/lyon/j4p/src/utils/URLEncoder.java

1    /** 
2     * Created by IntelliJ IDEA. 
3     * User: dlyon 
4     * Date: Dec 15, 2003 
5     * Time: 2:31:48 PM 
6     * To change this template use Options | File Templates. 
7     */ 
8    package utils; 
9     
10   public class URLEncoder { 
11       /** 
12        * Provides a method to encode any string into a URL-safe 
13        * form. 
14        * Non-ASCII characters are first encoded as sequences of 
15        * two or three bytes, using the UTF-8 algorithm, before being 
16        * encoded as %HH escapes. 
17        */ 
18    
19       private final static String[] hex = { 
20           "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07", 
21           "%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f", 
22           "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17", 
23           "%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f", 
24           "%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27", 
25           "%28", "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f", 
26           "%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37", 
27           "%38", "%39", "%3a", "%3b", "%3c", "%3d", "%3e", "%3f", 
28           "%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47", 
29           "%48", "%49", "%4a", "%4b", "%4c", "%4d", "%4e", "%4f", 
30           "%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57", 
31           "%58", "%59", "%5a", "%5b", "%5c", "%5d", "%5e", "%5f", 
32           "%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67", 
33           "%68", "%69", "%6a", "%6b", "%6c", "%6d", "%6e", "%6f", 
34           "%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77", 
35           "%78", "%79", "%7a", "%7b", "%7c", "%7d", "%7e", "%7f", 
36           "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87", 
37           "%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f", 
38           "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97", 
39           "%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f", 
40           "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7", 
41           "%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af", 
42           "%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7", 
43           "%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf", 
44           "%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7", 
45           "%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf", 
46           "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7", 
47           "%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df", 
48           "%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7", 
49           "%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef", 
50           "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7", 
51           "%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff" 
52       }; 
53    
54       /** 
55        * Encode a string to the "x-www-form-urlencoded" form, enhanced 
56        * with the UTF-8-in-URL proposal. This is what happens: 
57        * 
58        * <ul> 
59        * <li><p>The ASCII characters 'a' through 'z', 'A' through 'Z', 
60        *        and '0' through '9' remain the same. 
61        * 
62        * <li><p>The unreserved characters - _ . ! ~ * ' ( ) remain the same. 
63        * 
64        * <li><p>The space character ' ' is converted into a plus sign '+'. 
65        * 
66        * <li><p>All other ASCII characters are converted into the 
67        *        3-character string "%xy", where xy is 
68        *        the two-digit hexadecimal representation of the character 
69        *        code 
70        * 
71        * <li><p>All non-ASCII characters are encoded in two steps: first 
72        *        to a sequence of 2 or 3 bytes, using the UTF-8 algorithm; 
73        *        secondly each of these bytes is encoded as "%xx". 
74        * </ul> 
75        * 
76        * @param s The string to be encoded 
77        * @return The encoded string 
78        */ 
79       public static String encode(String s) { 
80           StringBuffer sbuf = new StringBuffer(); 
81           int len = s.length(); 
82           for (int i = 0; i < len; i++) { 
83               int ch = s.charAt(i); 
84               if ('A' <= ch && ch <= 'Z') {       // 'A'..'Z' 
85                   sbuf.append((char) ch); 
86               } else if ('a' <= ch && ch <= 'z') {    // 'a'..'z' 
87                   sbuf.append((char) ch); 
88               } else if ('0' <= ch && ch <= '9') {    // '0'..'9' 
89                   sbuf.append((char) ch); 
90               } else if (ch == ' ') {         // space 
91                   sbuf.append('+'); 
92               } else if (ch == '-' || ch == '_'       // unreserved 
93                       || ch == '.' || ch == '!' 
94                       || ch == '~' || ch == '*' 
95                       || ch == '\'' || ch == '(' 
96                       || ch == ')') { 
97                   sbuf.append((char) ch); 
98               } else if (ch <= 0x007f) {      // other ASCII 
99                   sbuf.append(hex[ch]); 
100              } else if (ch <= 0x07FF) {      // non-ASCII <= 0x7FF 
101                  sbuf.append(hex[0xc0 | (ch >> 6)]); 
102                  sbuf.append(hex[0x80 | (ch & 0x3F)]); 
103              } else {                    // 0x7FF < ch <= 0xFFFF 
104                  sbuf.append(hex[0xe0 | (ch >> 12)]); 
105                  sbuf.append(hex[0x80 | ((ch >> 6) & 0x3F)]); 
106                  sbuf.append(hex[0x80 | (ch & 0x3F)]); 
107              } 
108          } 
109          return sbuf.toString(); 
110      } 
111   
112  } 
113