KickJava   Java API By Example, From Geeks To Geeks.

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


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) but
15  * keeps param makers alive (needs to be checked if used and if it can't be
16  * combined in URLEscape).
17  * The only difference with URLEscape is that the plus-sign ('+') is escaped.
18  *
19  * @deprecated use Encode
20  * @author vpro
21  * @version $Id: URLParamEscape.java,v 1.1 2005/07/28 09:23:19 pierre Exp $
22  */

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

32     static boolean isacceptable[] =
33     {
34         false, false, false, false, false, false, false, false, // !"#$%&'
35
false, false, true, false, true, true, true, false, // ()*+,-./
36
true, true, true, true, true, true, true, true, // 01234567
37
true, true, true, false, false, false, false, false, // 89:;<=>?
38
true, 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, true, // XYZ[\]^_
42
false, true, true, true, true, true, true, true, // `abcdefg
43
true, true, true, true, true, true, true, true, // hijklmno
44
true, true, true, true, true, true, true, true, // pqrstuvw
45
true, true, true, false, false, false, false, false // xyz{|}~
46
};
47
48     /**
49      * Hex characters
50      */

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

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

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

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

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

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