KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > URLEscape


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util;
11 import org.mmbase.util.logging.*;
12
13 /**
14  * Escapes and Unescapes undesirable characters using % (URLEncoding)
15  *
16  * @deprecated use Encode
17  * @author vpro
18  * @version $Id: URLEscape.java,v 1.1 2005/07/28 09:23:19 pierre Exp $
19  */

20 public class URLEscape {
21
22     // logger
23
private static Logger log = Logging.getLoggerInstance(URLEscape.class.getName());
24
25     /**
26      * List for all ASCII characters whether it can be part of an
27      * URL line.
28      */

29     static boolean isacceptable[] = {
30         false, false, false, false, false, false, false, false, // !"#$%&'
31
false, false, true, true, true, true, true, false, // ()*+,-./
32
true, true, true, true, true, true, true, true, // 01234567
33
true, true, true, false, false, false, false, false, // 89:;<=>?
34
true, true, true, true, true, true, true, true, // @ABCDEFG
35
true, true, true, true, true, true, true, true, // HIJKLMNO
36
true, true, true, true, true, true, true, true, // PQRSTUVW
37
true, true, true, false, false, false, false, true, // XYZ[\]^_
38
false, true, true, true, true, true, true, true, // `abcdefg
39
true, true, true, true, true, true, true, true, // hijklmno
40
true, true, true, true, true, true, true, true, // pqrstuvw
41
true, true, true, false, false, false, false, false // xyz{|}~
42
};
43
44     /**
45      * Hex characters
46      */

47     static char hex[] = {
48         '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
49     };
50
51     /**
52      * Character to use for escaping invalid characters
53      */

54     static char HEX_ESCAPE='%';
55
56     /**
57      * Escape a url.
58      * Replaces 'invalid characters' with their Escaped code, i.e.
59      * the questionmark (?) is escaped with %3F.
60      * @param url the urls to escape
61      * @return the escaped url.
62      */

63     public static String JavaDoc escapeurl(String JavaDoc str) {
64         StringBuffer JavaDoc esc = new StringBuffer JavaDoc();
65         byte buf[];
66         try {
67             buf = str.getBytes("UTF-8");
68         } catch (java.io.UnsupportedEncodingException JavaDoc uee) {
69             return str; // should not happen
70
}
71
72         for (int i = 0; i<buf.length;i++) {
73             int a = (int)buf[i] & 0xff;
74             if (a>=32 && a<128 && isacceptable[a-32]) {
75                 esc.append((char)a);
76             } else {
77                 esc.append(HEX_ESCAPE);
78                 esc.append(hex[a >> 4]);
79                 esc.append(hex[a & 15]);
80             }
81         }
82         return esc.toString();
83     }
84
85     /**
86      * converts a HEX-character to its approprtiate byte value.
87      * i.e. 'A' is returned as '/011'
88      * @param c the Hex character
89      * @return the byte value as a <code>char</code>
90      */

91     private static char from_hex(char c) {
92         return (char)(c >= '0' && c <= '9' ? c - '0'
93             : c >= 'A' && c <= 'F' ? c - 'A' + 10
94             : c - 'a' + 10); /* accept small letters just in case */
95     }
96
97     /**
98      * Unescape a url.
99      * Replaces escapesequenced with the actual character.
100      * i.e %3F is replaced with the the questionmark (?).
101      * @param url the urls to unescape
102      * @return the unescaped url.
103      */

104     public static String JavaDoc unescapeurl(String JavaDoc str) {
105         int i;
106         char j,t;
107         StringBuffer JavaDoc esc=new StringBuffer JavaDoc();
108
109         if (str!=null) {
110             for (i=0;i<str.length();i++) {
111                 t=str.charAt(i);
112                 if (t==HEX_ESCAPE) {
113                     t=str.charAt(++i);
114                     j=(char)(from_hex(t)*16);
115                     t=str.charAt(++i);
116                     j+=from_hex(t);
117                     esc.append(j);
118                 } else {
119                     esc.append(t);
120                 }
121             }
122         } else {
123             log.warn("Unescapeurl -> Bogus parameter");
124         }
125         return esc.toString();
126     }
127
128     /**
129      * Method for testing this class from the command line
130      */

131     public static void main(String JavaDoc args[]) {
132         for (int i=0;i<args.length;i++) {
133             log.info("Original : '"+args[i]+"'");
134             log.info("Escaped : '"+escapeurl(args[i])+"'");
135             log.info("Unescaped again : '"+unescapeurl(escapeurl(args[i]))+"'");
136         }
137
138     }
139 }
140
Popular Tags