KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > util > URLEncoderDecoder


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.util;
27
28 import java.io.UnsupportedEncodingException JavaDoc;
29 import java.util.BitSet JavaDoc;
30
31 /**
32  * Replacement for URLEncoder/URLDecoder of the standard SDK. This was necessary,
33  * as the default encoder/decoder used always the platform encoding, not UTF-8 or
34  * what has been set in the virtual machine.
35  *
36  * @author Matthias L. Jugel
37  * @version $Id: URLEncoderDecoder.java 1743 2004-09-14 12:28:00Z leo $
38  */

39 public class URLEncoderDecoder {
40
41   private static BitSet JavaDoc dontNeedEncoding;
42
43   static {
44     dontNeedEncoding = new BitSet JavaDoc(256);
45     int i;
46     for (i = 'a'; i <= 'z'; i++) {
47       dontNeedEncoding.set(i);
48     }
49     for (i = 'A'; i <= 'Z'; i++) {
50       dontNeedEncoding.set(i);
51     }
52     for (i = '0'; i <= '9'; i++) {
53       dontNeedEncoding.set(i);
54     }
55     //dontNeedEncoding.set('+');
56
dontNeedEncoding.set(' '); /* encoding a space to a + is done in the encode() method */
57     dontNeedEncoding.set('-');
58     dontNeedEncoding.set('_');
59     dontNeedEncoding.set('.');
60     dontNeedEncoding.set('*');
61     dontNeedEncoding.set('/');
62     dontNeedEncoding.set(':');
63   }
64
65
66   /**
67    * Encode a Java String into a Web encoded String using the %xx encoding and the
68    * character encoding enc.
69    */

70   public static String JavaDoc encode(String JavaDoc s, String JavaDoc enc) throws UnsupportedEncodingException JavaDoc {
71     byte[] buf = s.getBytes(enc);
72     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
73     for (int i = 0; i < buf.length; i++) {
74       int c = (int) buf[i];
75       if (dontNeedEncoding.get(c & 0xFF)) {
76         result.append((char) c);
77       } else {
78         result.append('%').append(Integer.toHexString(c & 0xFF).toUpperCase());
79       }
80     }
81     return result.toString();
82   }
83
84   /**
85    * Decode a %xx encoded string into a Java String using the provided encoding.
86    *
87    * @param s the string to decode
88    * @param enc the encoding (default should be UTF8)
89    */

90   public static String JavaDoc decode(String JavaDoc s, String JavaDoc enc) throws UnsupportedEncodingException JavaDoc {
91     byte[] buf = new byte[s.length()];
92     int length = 0;
93     for (int i = 0; i < s.length(); i++) {
94       char c = s.charAt(i);
95       if (c == '%') {
96         buf[length++] = (byte) Integer.parseInt(s.substring(i + 1, i + 3), 16);
97         i += 2;
98       } else {
99         buf[length++] = (byte) c;
100       }
101     }
102     return new String JavaDoc(buf, 0, length, enc);
103   }
104 }
105
Popular Tags